回声和写主机在PowerShell中之间的区别是什么? [英] What is the difference between echo and Write-Host in PowerShell?

查看:365
本文介绍了回声和写主机在PowerShell中之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的 在PowerShell中的不同回声和写主机。我有两个文件,​​ POC.ps1 &放大器; validatePath.ps1 。这些文件是在我的本地机器,我使用在远程机器上运行它们调用指令。我使用PowerShell的3.0版。

要执行这两个脚本我使用命令:

  \ POC.ps1 -filename C:\用户 - 用户布莱恩
 

下面是两个文件:

POC.ps1:

 参数($文件名,$用户)

回声$文件名
回声这
回声$用户

$ responseObject =调用命令testcomputer -FilePath。\ validatePath.ps1 -ArgumentList($文件名,$用户)-AsJob

而($ responseObject.State -ne已完成)
{

}

$结果=接收在职-Id $ responseObject.Id  - 保持
回声$结果
 

下面的情况变得有些怪异......

validatePath.ps1:

 参数([字符串] $文件名,
      [字符串] $用户)

功能ValidatePath($文件名,$用户,$的fileType =容器)
{
    写主机这是文件名:$文件名
    写主机这是用户:$用户< ---请注意,我用写主机在这里
    $ fileExist = $空
    如果(-not(测试路径$文件名-PathType $的fileType))
    {
        扔$用户,路径$文件名不存在!

    }
    其他
    {
         写主机这是第二部分
         回声$找到的文件名!
    }
    写主机这是第三部分
    回报$ fileExist
}


尝试
{

    ValidatePath($文件名,$用户)
}
抓住
{
    $ E = $ _。异常
    回声$ê
}
 

当我运行上面的脚本,这是输出:

  C:\用户
本
布莱恩
这是文件名:C:\用户布莱恩
这是用户:< ---注意这条线是?
这是第二部分
这是第三部分
C:\用户
布莱恩
找到了!
 

但是,如果我改变了 validatePath.ps1 来这样的:

 参数([字符串] $文件名,
      [字符串] $用户)

功能ValidatePath($文件名,$用户,$的fileType =容器)
{
    写主机这是文件名:$文件名
    回声这是用户:$用户< ---通知我使用的回声在这里
    $ fileExist = $空
    如果(-not(测试路径$文件名-PathType $的fileType))
    {
        扔$用户,路径$文件名不存在!

    }
    其他
    {
         写主机这是第二部分
         回声$找到的文件名!
    }
     写主机这是第三部分
    回报$ fileExist
}


尝试
{

    ValidatePath($文件名,$用户)
}
抓住
{
    $ E = $ _。异常
    回声$ê
}
 

这是输出:

  C:\用户
本
布莱恩
这是文件名:C:\用户布莱恩
这是第二部分
这是第三部分
这是用户:< ----注意这条线是现在?
C:\用户
布莱恩
找到了!
 

您会发现,行这是用户:在不同的地方。为什么是这样?为什么回声的工作方式不同写主机

更新:

什么是更奇怪的是,如果我重新运行该脚本两次这样的:

POC.ps1:

 参数($文件名,$用户)

回声$文件名
回声这
回声$用户

$ responseObject =调用命令CAPTESTPK01 -FilePath。\ validatePath.ps1 -ArgumentList $文件名,$用户-AsJob

而($ responseObject.State -ne已完成)
{

}

$结果=接收在职-Id $ responseObject.Id  - 保持
回声$结果


$文件名=C:\ saddfasdfj

#Here我再次运行该命令,使用不同的文件名
$ responseObject =调用命令CAPTESTPK01 -FilePath。\ validatePath.ps1 -ArgumentList $文件名,$用户-AsJob

而($ responseObject.State -ne已完成)
{
   如果($ responseObject.State -eq失败)
   {
        回声失败
        $结果=接收在职-Id $ responseObject.Id  - 保持
        回声$结果
        打破
   }
}

$结果=接收在职-Id $ responseObject.Id  - 保持
回声$ resul
 

这在使用使我这个输出回声 validatePath.ps1

  C:\用户
本
布莱恩
这是文件名:C:\用户
这是第二部分
这是第三部分
这是用户:布莱恩< ----这条线就在这里
C:\用户
找到了!
这是文件名:C:\ saddfasdfj
这是用户:布莱恩< ----但现在它在这里,它应该是什么?问心无愧?
布莱恩,路径C:\ saddfasdfj不存在!
 

解决方案

回声的别名写输出,它写入成功输出流。这使得输出被通过管道或加工重定向到文件。 写主机直接写入到控制台,这样的输出不能重定向/作进一步处理。

I'm confused about the difference between echo and Write-Host in PowerShell. I have two files, POC.ps1 & validatePath.ps1. These files are on my local machine, and I'm running them on a remote machine using Invoke-Command. I am using PowerShell v3.0.

To execute both of these scripts I use the command:

.\POC.ps1 -filename C:\Users  -user Blaine

Here are the two files:

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

Here is where things get weird...

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
}

When I run the above script, this is the output:

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!

But if I change the validatePath.ps1 to this:

Param([string] $filename,
      [string] $user)

function ValidatePath( $filename, $user, $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, 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
}

This is the output:

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!

You will notice that the line "This is the user:" is in different spots. Why is this? Why does echo work differently than Write-Host?

UPDATE:

What is even more strange is that if I rerun the script twice like this:

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

It gives me this output when using echo in validatePath.ps1:

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!

解决方案

echo is an alias for Write-Output, which writes to the Success output stream. This allows output to be processed through pipelines or redirected into files. Write-Host writes directly to the console, so the output can't be redirected/processed any further.

这篇关于回声和写主机在PowerShell中之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆