0

I'm using this PowerShell code for getting a txt file with all the files in Recycle bin with their original location (got from Listing files in recycle bin):

(New-Object -ComObject Shell.Application).NameSpace(0x0a).Items()|select @{n="OriginalLocation";e={$_.ExtendedProperty("{9B174B33-40FF-11D2-A27E-00C04FC30871} 2")}},Name| export-csv -delimiter "\" -path C:\Desktop\file_list_$(get-date -f yyyyMMdd).txt -NoTypeInformation

(gc C:\Desktop\file_list_$(get-date -f yyyyMMdd).txt | select -Skip 1)| % {$_.Replace('"','')}| set-content C:\Desktop\file_list_$(get-date -f yyyyMMdd).txt

and it works great. The problem now is that I've been asked to launch this .ps1 file from computer A for getting the files in recycle bin of computer B (for example \172.00.00.00), going in \172.00.00.00\folder and launching a cmd file that launch that .ps1 file.

I've set

(New-Object -ComObject Shell.Application).NameSpace(0x0a).Items()|select @{n="OriginalLocation";e={$_.ExtendedProperty("{9B174B33-40FF-11D2-A27E-00C04FC30871} 2")}},Name| export-csv -delimiter "\" -path \\172.00.00.00\C\Desktop\file_list_$(get-date -f yyyyMMdd).txt -NoTypeInformation

(gc \\172.00.00.00\C\Desktop\file_list_$(get-date -f yyyyMMdd).txt | select -Skip 1)| % {$_.Replace('"','')}| set-content \\172.00.00.00\C\Desktop\file_list_$(get-date -f yyyyMMdd).txt

but how do I tell it to check recycle bin of computer B?

Thanks!

solquest
  • 65
  • 10
  • 2
    You could use Invoke-Command to execute your code on a remote system. https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-6 – Patrick Oct 15 '19 at 08:18
  • This very much depends on your set up and environment. Like @Patrick suggests, Invoke-Command would be one option. If you have a list of computers, it might be worth using PSEXEC: https://learn.microsoft.com/en-us/sysinternals/downloads/psexec – SolidSid Oct 15 '19 at 08:21
  • Sure you could use PSEXEC but I would prefere powershell because you can also invoke the command to more then one server at once and you could do the error handling simple inside powershell. – Patrick Oct 15 '19 at 08:54
  • Thanks, the Invoke-Command is perfect! – solquest Oct 22 '19 at 13:45

1 Answers1

0

Wrap your code into a straight forward mini function and call it in the remotely passing the computername and system credentials. If you are using enterprise admin, then you don't need to pass the creds and it will use your windows authentication to connect to the remote systems.

$scriptblock = {
    function Get-RecycleBinItems 
    {
        (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |
            Select-Object Name, Size, Path
    }
    Get-RecycleBinItems
}

Invoke-Command -ComputerName $computer -ScriptBlock $scriptblock 

For passing credentials, you can use :

Read-Host -assecurestring | convertfrom-securestring | out-file C:\path\username-password-encrypted.txt
$username = 'domain\username'
$password = 'password' | convertto-securestring
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
Invoke-Command -ComputerName Hostname -ScriptBlock $ScriptBlock -Credential $creds

Note: I am expecting PSRemoting is configured. If not configured, make sure that you configure PSRemoting first.

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45