How to Write-Verbose from Invoke-Command?

ε祈祈猫儿з 提交于 2019-11-28 13:17:57

Try it this way:

Invoke-Command -ComputerName MY-COMPUTERNAME  {$VerbosePreference='Continue'; Write-Verbose "blah" }

Best thing I think you could do is create a function that has [CmdletBinding()] so it supports the -verbose switch. Then you would be able to capture the verbose state of the local function using the local $VerbosePreference and pass it along in the invoke command. This will only work in Powershell 3.0 and higher, since you will need to use the $Using scope modifier.

Function write-blah{
[CmdletBinding()]
Param()
Invoke-Command -ComputerName MY-COMPUTERNAME  {$VerbosePreference=$Using:VerbosePreference; Write-Verbose "blah" }
}

Then you would call your function like this.

Write-Blah -verbose

In testing this has worked for me. I believe your function must support parameters, hence the empty Param() block.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!