Powershell - Remove files on remote machines script

Hi All,

I have this script to delete files on remote machines, problem is that it is deleting files on my own machine rather than deleting it of the remote machines???

$computer=Get-ADComputer -filter 'Name -like "c8764"'

$computer | % { Get-WmiObject -ComputerName $_.name win32_logicaldisk -Filter "DriveType = 3" | Select-Object DeviceID | ForEach-Object {Get-ChildItem ($_.DeviceID + "\") -Include *.aaa,*.abc,restore_files_pcigy*.* -Recurse | remove-item -force -verbose } }

Open in new window


Regards,
Kay
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry CunneyCommented:
Hi Kabir,
To execute something on a remote machine you could try 'Invoke-Command'

Invoke-Command -Computer $COMPUTER -ScriptBlock {Remove-Item $args -force } -ArgumentList 'c:\FileToRemove'

Open in new window


First I would test the above powershell statement with an actual computer name that you  know instead of $COMPUTER just to first prove that it works.

Also I would first test that you are getting a valid list of computers with the Get-ADComputer command - so maybe something like the following to test
Get-ADComputer -filter 'Name -like "c8764"' | Select -Expand Name

Open in new window

0
footechCommented:
Using PS Remoting (using Invoke-Command) would be the most straight-forward way of executing the commands and removing files on the remote machines.

If you wanted to stick with using WMI, you could use that to find files and delete them on the remote machine as well.
$computer=Get-ADComputer -filter 'Name -like "c8764"'

$computer | % `
{
    $comp = $_.name
    Get-WmiObject -ComputerName $comp win32_logicaldisk -Filter "DriveType = 3" |
     Select-Object DeviceID | ForEach-Object `
     {
         Get-WmiObject Cim_DataFile -Filter "drive = '$($_.DeviceID)' AND ((extension = 'aaa' OR extension = 'abc') OR (filename LIKE 'restore_files_pcigy%'))" -ComputerName $comp |
         ForEach-Object { $_.Delete() } | Out-Null
     }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rroncmeCommented:
This one will blast all computers it finds at once:
*****
$computer=Get-ADComputer -filter 'Name -like "lakrdr*"'

icm -computername $computer.DNSHostName -scriptblock {
$ENV:Computername
Get-WmiObject win32_logicaldisk -Filter "DriveType = 3" | Select-Object DeviceID | ForEach-Object {Get-ChildItem ($_.DeviceID + "\") -Include *.aaa,*.abc,restore_files_pcigy*.* -Recurse | remove-item -force -verbose -whatif  }
}
*****

This one will loop trough each one slower:
*****
$computers=Get-ADComputer -filter 'Name -like "lakrdr*"'

Foreach ($computer in $computers){
icm -computername $computer.DNSHostName -scriptblock {
$ENV:Computername
Get-WmiObject win32_logicaldisk -Filter "DriveType = 3" | Select-Object DeviceID | ForEach-Object {Get-ChildItem ($_.DeviceID + "\") -Include *.aaa,*.abc,restore_files_pcigy*.* -Recurse | remove-item -force -verbose -whatif  }
}
}
*****
Remove the -Whatif when ready to remove files
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.