加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – PowerShell中的echo和Write-Host有什么区别?

发布时间:2020-12-13 21:03:00 所属栏目:Windows 来源:网络整理
导读:我对PowerShell中回声和写入主机之间的区别感到困惑。我有两个文件,POC.ps1 validatePath.ps1。这些文件在我的本地机器上,我使用Invoke-Command在远程机器上运行它们。我正在使用PowerShell v3.0。 要执行这两个脚本,我使用命令: .POC.ps1 -filename C:
我对PowerShell中回声和写入主机之间的区别感到困惑。我有两个文件,POC.ps1& validatePath.ps1。这些文件在我的本地机器上,我使用Invoke-Command在远程机器上运行它们。我正在使用PowerShell v3.0。

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

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

以下是两个文件:

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

这里是什么东西变得奇怪?

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,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 the second part
This is the third part
This is user: <---- Notice where this line is now?
C:Users
Blaine
found!

你会注意到,这行“这是用户:”在不同的地方。为什么是这样?为什么回声工作与写入主机不同?

更新:

更奇怪的是,如果我像这样重新运行脚本:

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!
echo是Write-Output的别名,它写入Success输出流。这允许输出通过管道处理或重定向到文件。写入主机直接写入控制台,因此输出无法进一步重定向/处理。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读