我对PowerShell中的echo和Write-Host之间的区别感到困惑。 我有两个文件, POC.ps1和validatePath.ps1 。 这些文件在我的本地机器上,我正在使用Invoke-Command在远程机器上运行它们。 我正在使用PowerShell v3.0。
要执行这两个脚本,我使用下面的命令:
.POC.ps1 -filename C:Users -user Blaine
这是两个文件:
POC.ps1:
在启动时dynamic决定Winforms或Wpf?
可以检测声卡上的分贝水平吗?
创buildXML文件,赋予所有Windows帐户用户以写权限
获取当前login的远程机器上的交互式用户?
DirectXinput是游戏中键盘处理的正确select吗?
param($filename,$user) echo $filename echo "This" echo $user $responseObject = Invoke-Command testcomputer -FilePath .validatePath.ps1 -ArgumentList($filename,$user) -AsJob while($responseObject.State -ne "Completed") { } $result = Receive-Job -Id $responseObject.Id -Keep echo $result
这里是奇怪的地方…
validatePath.ps1:
Param([string] $filename,[string] $user) function ValidatePath( $filename,$user,$fileType = "container" ) { Write-Host "This is the file name: $filename" Write-Host "This is user: $user" <--- Notice I'm using Write-Host here $fileExist = $null if( -not (test-path $filename -PathType $fileType) ) { throw "$user,the path $filename does not exist!" } else { Write-Host "This is the second part" echo $filename found! } Write-Host "This is the third part" return $fileExist } try { ValidatePath($filename,$user) } catch { $e = $_.Exception echo $e }
当我运行上面的脚本,这是输出:
C:Users This Blaine This is the file name: C:Users Blaine This is user: <--- Notice where this line is? This is the second part This is the third part C:Users Blaine found!
但是,如果我将validatePath.ps1更改为:
Param([string] $filename,$fileType = "container" ) { Write-Host "This is the file name: $filename" echo "This is user: $user" <---notice I'm using Echo here $fileExist = $null if( -not (test-path $filename -PathType $fileType) ) { throw "$user,$user) } catch { $e = $_.Exception echo $e }
这是输出:
C:Users This Blaine This is the file name: C:Users Blaine This is the second part This is the third part This is user: <---- Notice where this line is now? C:Users Blaine found!
您会注意到“这是用户:”这一行在不同的地方。 为什么是这样? 为什么echo工作与Write-Host不同?
更新:
更奇怪的是,如果我像这样重新运行脚本两次:
POC.ps1:
param($filename,$user) echo $filename echo "This" echo $user $responseObject = Invoke-Command CAPTESTPK01 -FilePath .validatePath.ps1 -ArgumentList $filename,$user -AsJob while($responseObject.State -ne "Completed") { } $result = Receive-Job -Id $responseObject.Id -Keep echo $result $filename = "C:saddfasdfj" #Here I run the command again,using a different file name $responseObject = Invoke-Command CAPTESTPK01 -FilePath .validatePath.ps1 -ArgumentList $filename,$user -AsJob while($responseObject.State -ne "Completed") { if($responseObject.State -eq "Failed") { echo "Failed" $result = Receive-Job -Id $responseObject.Id -Keep echo $result break } } $result = Receive-Job -Id $responseObject.Id -Keep echo $resul
在validatePath.ps1使用echo时,它给了我这个输出:
C:Users This Blaine This is the file name: C:Users This is the second part This is the third part This is user: Blaine <---- This line is here C:Users found! This is the file name: C:saddfasdfj This is user: Blaine <---- But now it's here,where it should be? Wth? Blaine,the path C:saddfasdfj does not exist!
在Windows 8中检测重启 – 未调用SessionEnding
.NET中是否有CalDAV + CardDAV服务器实现?
任何方式立即触发WaitOne()的超时?
Mono应用程序在尝试更改控制台前景色时失败,并出现ArgumentNullException
如何从远程计算机获取CommonApplicationData?
echo是Write-Output的别名,写入成功输出流。 这允许输出通过管道进行处理或重定向到文件。 Write-Host直接写入控制台,所以输出不能被重定向/进一步处理。
echo是写入输出的别名。 写主机直接写入“屏幕”,写输出写入管道。 如果流水线没有输入到其他命令中,它最后也会在“屏幕”上结束。 您所看到的差异是写入主机直接写入输出写入输出首先通过管道,并在写入主机后结束在屏幕上。
使用写入输出可让您将输出重定向到文件或另一个命令,其中写入主机不会。 他们应该根据你想要的使用。
看到这里更多。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。