PowerShell远程操作与系统管理全解
1. 远程执行PowerShell表达式
在远程机器上执行PowerShell表达式时,需要收集输出和错误输出。以下是具体的操作步骤:
1.收集输出和错误输出:
$errorOutput = [IO.Path]::GetTempFileName() $output = psexec /acceptEula $computer cmd /c $commandLine 2>$errorOutput- 检查是否有错误:
$errorContent = Get-Content $errorOutput Remove-Item $errorOutput if($errorContent -match "Access is denied") { $OFS = "`n" $errorMessage = "Could not execute remote expression. " $errorMessage += "Ensure that your account has administrative " + "privileges on the target machine.`n" $errorMessage += ($errorContent -match "psexec.exe :