Geo*_*uer 16 powershell powershell-remoting
在powershell中
Write-Verbose并且该-Verbose标志用于在详细模式下运行命令时提供详细信息.我正在远程运行一些脚本,并希望捕获详细的输出输出.但是,Invoke-Command似乎没有捕捉到冗长的频道.
PS:> Invoke-Command -ComputerName MY-COMPUTERNAME -Verbose { Write-Verbose "blah" }
PS:>
Run Code Online (Sandbox Code Playgroud)
如何在运行远程脚本时捕获详细输出?
小智 17
我认为你能做的最好的事情是创建一个具有[CmdletBinding()]的函数,因此它支持-verbose开关.然后,您将能够使用本地$ VerbosePreference捕获本地函数的详细状态,并在invoke命令中传递它.这只适用于Powershell 3.0及更高版本,因为您需要使用$ Using scope修饰符.
Function write-blah{
[CmdletBinding()]
Param()
Invoke-Command -ComputerName MY-COMPUTERNAME {$VerbosePreference=$Using:VerbosePreference; Write-Verbose "blah" }
}
Run Code Online (Sandbox Code Playgroud)
那你就像这样调用你的函数.
Write-Blah -verbose
Run Code Online (Sandbox Code Playgroud)
在测试中,这对我有用.我相信你的函数必须支持参数,因此空的Param()块.
mjo*_*nor 11
试试这种方式:
Invoke-Command -ComputerName MY-COMPUTERNAME {$VerbosePreference='Continue'; Write-Verbose "blah" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6733 次 |
| 最近记录: |