我试图找出是否有任何新的moverequest问题。 所以我正在使用-WhatIf开启New-MoveRequestfunction。 我注意到,如果moverequest没有问题,那么这个函数会输出类似于What If: Creating New-MoveRequest for 'John Doe' Do What If: Creating New-MoveRequest for 'John Doe' 。 问题是,我无法将此消息分配给任何variables。 这个消息(输出)来自哪里?
New-MoveRequest -Remote -Identity $userAddress -RemoteHostName $rhn -RemoteCredential $SourceCredential -WhatIf
你的答案是返回types是无效的。 它不会返回任何东西,因为-WhatIf开关不能运行任何东西。 它只是testing看看命令会做什么,并将这些信息写入主机。
写主机
$a = Write-Host "Test string" # Test String will output to console # $a will be null because the output will go to the console $a = Write-Host "Test string" | Write-Output "C:\output.txt" # Test String will output to console # $a will be null, and output.txt will be empty because the output will go to the console
写主机的输出被明确地发送到控制台。 它将不能被存储到一个variables或pipe道到另一个命令,因为这是标准输出stream的用途。
写输出
写输出将把信息放入pipe道中。 从那里它可以被传送到另一个命令。 如果输出到达的命令没有输出到其他地方,那么如果用$var =语句启动该行,它将被存储到一个variables中。 如果在这一点上没有处理,输出将写入主机控制台。
$a = Write-Output "Test string" # Console will be empty # $a will contain Test string $a = Write-Host "Test string" | Out-File "C:\output.txt" # Console will be empty # $a will be null, and output.txt will contain "Test String"
完整的输出stream参考: https : //blogs.technet.microsoft.com/heyscriptingguy/2014/03/30/understanding-streams-redirection-and-write-host-in-powershell/
在PowerShell中,您可以使用GetType()来获取对象的types。 所以把你的命令的结果放在一个variables中,并使用GetType() :
[PS] C:\Users\username\Desktop> $x = New-MoveRequest ... -WhatIf [PS] C:\Users\username\Desktop> $x.GetType() You cannot call a method on a null-valued expression. At line:1 char:11 + $x.GetType <<<< () + CategoryInfo : InvalidOperation: (GetType:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
结果:在cmdlet上使用-WhatIf参数将导致null 。
要捕获命令的所有输出,包括WhatIf -Output,您可以使用Start-Transcript :
[PS] C:\Users\username\Desktop> Start-Transcript tmpfile [PS] C:\Users\username\Desktop> New-MoveRequest ... -WhatIf [PS] C:\Users\username\Desktop> Stop-Transcript
这将捕获所有的输出并将其写入到tmp文件中,尽pipe还有很多意想不到的附加信息:
[PS] C:\Users\username\Desktop> Get-Content tmpfile ********************** Windows PowerShell Transcript Start Start time: 20170510090649 Username : username Machine : hostname (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is x [PS] C:\Users\username\Desktop>$x = New-MoveRequest ... -WhatIf What if: ... [PS] C:\Users\username\Desktop>Stop-Transcript ********************** Windows PowerShell Transcript End End time: 20170510090701 **********************
我还没有find一种方法来删除所有冗长的东西,所以你将不得不再次parsing这个文件。
您不能使用“-whatif”参数排除故障。 正如一些评论者所指出的那样,这只从开关,命令和对象的angular度来validation命令是正确的。
例如New-MoveRequest testusername -TargetDatabase databasename -WhatIf只会在用户名不存在或数据库'databasename'不存在的情况下发生错误。 非常适合确保您没有拼写错误。 这将通过一个 – 即使即使有问题,会导致失败的邮箱。
如果您对实际移动请求有任何疑问,请发布您收到的错误。
一个常规故障排除注意事项是,如果移动失败,则可以使用Get-MoveRequestStatistics获取更多详细信息,然后查看以下属性: