Copying file using CIM_logical file in PS

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Faraz Amjad Faraz Amjad 1 year, 9 months ago.

  • Author
    Posts
  • #22697
    Profile photo of Faraz Amjad
    Faraz Amjad
    Participant

    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

  • #22698
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #22725
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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}

    }

  • #22739
    Profile photo of Faraz Amjad
    Faraz Amjad
    Participant

    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

  • #22741
    Profile photo of Faraz Amjad
    Faraz Amjad
    Participant

    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

  • #22746
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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

  • #22748
    Profile photo of Faraz Amjad
    Faraz Amjad
    Participant

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

You must be logged in to reply to this topic.