Author Posts

February 18, 2015 at 7:21 am

Hi Guys,

I am new to CIM technology and its usability in Powershell. I am trying to copy all txt files from FolderA to FolderB. Both files are located on the root of C.

The script runs successfully with return code 2 with is "Access denied". I have checked the folder permissions to which full control is granted to administrator. Also I am running PowerShell ISE as admin. Following is my scripting

$query= "SELECT * FROM CIM_LogicalFile WHERE Path= '\\folderA\\' AND Extension = 'txt' "
$obj = Get-WmiObject -ComputerName "localhost" -Query $query
$obj.Copy("C:\\folderB")

Your help is highly appreciated.

Regards
Faraz Amjad

February 18, 2015 at 7:26 am

I'm pretty sure you have to specify the complete destination path and file name.

February 18, 2015 at 11:05 am

You can simplify your WMI call a bit
$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

$file.Copy($newfile)

}

As Don said you need to give the full path to the new file

You can also do this

$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

Invoke-WmiMethod -InputObject $file -Name Copy -ArgumentList $newfile

}

OR

use the new CIM cmdlets

$files = Get-CimInstance -ClassName CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

Invoke-CimMethod -InputObject $file -MethodName Copy -Arguments @{Filename = $newfile}

}

February 19, 2015 at 2:39 am

THANKS A MILLION GUYS!!!!!!

Both of you are very right. I was missing file name from my script and after filling up the file name(s), the return code is 0 which is success 🙂

Regards

February 19, 2015 at 5:25 am

Guys,

Can I change the destination to network shared folder i.e. move or copy the folder to network shared folder. I have tried this with following script, the script runs successfully but return code is 9 i.e. invalid object.

$query= "SELECT * FROM CIM_LogicalFile WHERE Path= '\\folderA\\' AND Extension = 'txt' "

$files = Get-CimInstance -Query $query -ComputerName "localhost"

foreach($file in $files)
{
$newfile = "\\Server\\$($file.FileName).$($file.Extension)"
Invoke-CimMethod -InputObject $file -MethodName Copy -Arguments @{Filename = $newfile}

}

Appreciated

Faraz Amjad

February 19, 2015 at 7:59 am

I don't believe you can. Your UNC path would look more like this

$newfile = "\\\\Server\\C$\\$($file.FileName).$($file.Extension)"

I think.

if you mapped a drive to the remote machine it should work

February 19, 2015 at 8:35 am

Sadly, this didn't work 🙁 , any other suggestion?