Write-Host在我的脚本中工作,但Write和Write-Output会破坏它

时间:2015-01-23 14:21:36

标签: powershell

我一直在我的脚本中使用Write-Host,但我希望此文本显示在输出文件中,因此我尝试将其更改为WriteWrite-Output。当我这样做时,我的部分代码崩溃了。我不明白为什么。

我还希望在脚本运行时看到控制台上的输出,而不仅仅是在文件中。

此代码有效

function Load-Configuration([string] $ConfigurationFileName)
{
    $ConfigurationFilePath = Join-Path -Path $ScriptFileFolder -ChildPath $ConfigurationFileName
    Write-Host "Configuration file is located here: $ConfigurationFilePath"
    if(Test-Path -Path $ConfigurationFilePath)
    {
        . $ConfigurationFilePath
    }
    else
    {
        Throw "Configuration file `"$ConfigurationFilePath`" not found."
    }
}

但是使用Write-Output崩溃,

function Load-Configuration([string] $ConfigurationFileName)
{
    $ConfigurationFilePath = Join-Path -Path $ScriptFileFolder -ChildPath $ConfigurationFileName
    Write-Output "Configuration file is located here: $ConfigurationFilePath"
    if(Test-Path -Path $ConfigurationFilePath)
    {
        . $ConfigurationFilePath
    }
    else
    {
        Throw "Configuration file `"$ConfigurationFilePath`" not found."
    }
}

错误:

Property 'BuildDestination' cannot be found on this object. Make sure that it exists.
At C:\Users\<user>\Desktop\eSignAutoDeploy\eSignUpgradeScript.ps1:43 char:4
+ if(!(Test-Path -Path $Config.BuildDestination)){
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

1 个答案:

答案 0 :(得分:0)

您是否以任何方式处理Load-Configuration的输出?如果是这样,您只需将一个字符串添加到您需要考虑的函数的输出中。对于这种类型的输出,我通常使用Write-Verbose。大多数时候我不想看到冗长的信息,但是当我这样做时,我只是拍了一个-Verbose参数。而这种详细的输出并没有与std输出混淆。为了使其工作,请声明您的函数:

function Load-Configuration([Parameter()][string] $ConfigurationFileName)

然后你可以这样称呼它:

Load-Configuration config.xml -verbose