Compress-Archive
不支持远程计算机上的操作。要远程执行相同的操作,您应该使用Invoke-Command
or PS-Session
。这是您可以使用的示例:
Read-host -assecurestring | convertfrom-securestring | out-file C:\path\to\the\file\Credentials_encrypted.txt
$user = "domain\username"
$pass = Get-ChildItem "C:\path\to\the\file\Credentials_encrypted.txt" | ConvertTo-SecureString
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass
## Below command will execute the command in the remote system. Means that Compress-Archive is now running locally on the remote machine.
Invoke-Command -ComputerName Remote_Computer_IP -ScriptBlock { Compress-Archive -Path C:\Reference\* -DestinationPath C:\Destination\Destination_file.zip } -credential $cred
## Once the compression is done, you can simply use `copy-item` to pull the same file into your local system.
Copy-Item -Path \\Remote_Computer_IP\C$\Destination\Destination_file.zip -Destination C:\localsystem\path\destination_file.zip
除了所有这些,请记住在远程计算机上调用命令,您需要启用PSRemoting。因此,请通过我在问题最后的回答:ENABLE-PSRemoting以查看详细信息。
注意:如果您@param
在脚本块内部使用并像-argumentlist
在Invoke-command