我使用这个PowerShell代码来获取一个txt文件,其中包含回收站中的所有文件及其原始位置(从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它工作得很好。现在的问题是,我被要求从计算机A启动此cmd文件,以获取计算机B的回收站中的文件(例如\172.00.00.00),进入\172.00.00.00\文件夹并启动一个启动该.ps1文件的.ps1文件。
我设置好了
(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的回收站?
谢谢!
发布于 2019-10-15 19:23:11
将您的代码封装到一个简单的迷你函数中,并在远程传递计算机名和系统凭据时调用它。如果您使用的是企业管理员,那么您不需要通过证书,它将使用您的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。
希望能有所帮助。
https://stackoverflow.com/questions/58390006
复制相似问题