PowerShell中的Write-Host和Write-Output有什么区别?
喜欢...
Write-Host "Hello World ";
Write-Output "Hello World";
Run Code Online (Sandbox Code Playgroud) 如果我有一个可执行文件out.exe,它stdout被重定向到一个文件,即:
out.exe > $file
现在,如果我这样做,它只输出:
<----------------------------->
80 columns per line to the file
Run Code Online (Sandbox Code Playgroud)
有没有办法让控制台列数中的标准输出更宽?这是在out.exe某种程度上混乱列?就我而言,我正在使用fxcopcmd.exe.
我对PowerShell echo和Write-HostPowerShell 之间的区别感到困惑.我有两个文件,POC.ps1&validatePath.ps1.这些文件在我的本地计算机上,我正在使用远程计算机上运行它们Invoke-Command.我使用的是PowerShell v3.0.
要执行这两个脚本,我使用命令:
.\POC.ps1 -filename C:\Users -user Blaine
Run Code Online (Sandbox Code Playgroud)
这是两个文件:
POC.ps1:
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
Run Code Online (Sandbox Code Playgroud)
这是事情变得奇怪的地方......
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 …Run Code Online (Sandbox Code Playgroud) 我最近在学习PowerShell,发现了一个我无法理解的行为.让我在下面的代码中解释:
function func1()
{
Write-Output "output from func1()"
func2
return $true
}
function func2()
{
Write-Output "output from func2()"
func3
}
function func3()
{
Write-Output "output from func3()"
}
Write-Output "*** 1. run alone ****"
func1
Write-Output "*** 2. run inside if ****"
if (func1) {
#do nothing
}
Run Code Online (Sandbox Code Playgroud)
直接调用func1时很奇怪,它可以按预期输出消息,但是如果放在"if"语句中,它就不会.见输出如下:
*** 1. run alone ****
output from func1()
output from func2()
output from func3()
True
*** 2. run inside if ****
Run Code Online (Sandbox Code Playgroud)
你可以看到它是空的.为什么会这样,我如何像第一个例子那样启用输出?谢谢!
Cmdlet不应使用Console API.
为什么是这样?
如果我写[Console]::Write("test"),它的效果就像
Write-Host "test"
Run Code Online (Sandbox Code Playgroud)
编辑:众所周知Write-Host应该避免.当MSDN说不使用Console API时,可以安全地假设它们暗示我们不应该使用它们Write-Host,因为它在幕后使用Console API吗?