Powershell 从另一台计算机列出回收站中的文件

Powershell 从另一台计算机列出回收站中的文件,powershell,recycle-bin,Powershell,Recycle Bin,我使用此PowerShell代码获取一个txt文件,其中包含回收站中的所有文件及其原始位置(获取自): 而且效果很好。 现在的问题是,我被要求从计算机A启动此.ps1文件,以获取计算机B回收站中的文件(例如\172.00.00.00),进入\172.00.00.00\文件夹并启动启动该.ps1文件的cmd文件 我准备好了 (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items()|select @{n="OriginalLo

我使用此PowerShell代码获取一个txt文件,其中包含回收站中的所有文件及其原始位置(获取自):

而且效果很好。 现在的问题是,我被要求从计算机A启动此.ps1文件,以获取计算机B回收站中的文件(例如\172.00.00.00),进入\172.00.00.00\文件夹并启动启动该.ps1文件的cmd文件

我准备好了

(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
但我如何告诉它检查电脑B的回收站呢


谢谢

将您的代码包装成一个简单的小函数,并在远程传递computername和系统凭据时调用它。如果您使用的是enterprise admin,则不需要通过creds,它将使用您的windows身份验证连接到远程系统

$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 
要传递凭据,您可以使用:

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
注意:我希望配置PSRemoting。如果未配置,请确保首先配置PSRemoting


希望有帮助。

您可以使用Invoke命令在远程系统上执行代码。这在很大程度上取决于您的设置和环境。正如@Patrick所建议的,Invoke命令将是一种选择。如果您有一个计算机列表,可能值得使用PSEXEC:当然可以使用PSEXEC,但我更喜欢powershell,因为您还可以一次调用多台服务器的命令,并且可以在powershell中执行简单的错误处理。谢谢,invoke命令非常完美!
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