out-file问题(空管元素)

时间:2013-03-15 17:07:32

标签: powershell

我有我想要的输出,但问题是将其输出到文件外。我不关心调用cmdlet是什么,我只需要它能够附加到CSV。

这是我目前的解决方案,它提供了正确的输出(只是不写入文件):

Param(
    [string]$clusterName
    )

Import-Module failoverclusters

get-cluster -name $clusterName | get-clusterresource | where-object {
    $_.ResourceType.name -eq "SQL Server"
        } |
        %{
            $virtualServerName = get-clusterparameter -InputObject $_ VirtualServerName; 
            $instanceName = get-clusterparameter -InputObject $_ InstanceName; 
            $nodeName = $_.OwnerNode;
            $IPAddress = [System.Net.Dns]::GetHostAddresses($virtualServerName.Value) | where-object {$_.IsIPv6LinkLocal -eq $False}
            echo ("{0},{1}\{2},{3},{4},{5},{6}" -f $nodeName,$virtualServerName.Value, $instanceName.Value,$virtualServerName.Value,$instanceName.Value,$IPAddress,$clusterName);
        }

我已将此添加到结尾,这会返回错误:

| out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append

返回:不允许使用空管道元素。

如何将其输出到CSV文件并附加?它会多次运行。

1 个答案:

答案 0 :(得分:2)

您是否在最后一行“}”的同一行添加| out-file行,或者在单独的行中添加?如果你有一个“|”作为新行上的第一个非空白字符,您需要转义前面的换行符,否则会出现“空管元素”错误。

你的代码应该看起来像这样(注意倒数第二行到后面的“}”后面的反引号):

Param(
    [string]$clusterName
    )

Import-Module failoverclusters

get-cluster -name $clusterName | get-clusterresource | where-object {
    $_.ResourceType.name -eq "SQL Server"
        } |
        %{
            $virtualServerName = get-clusterparameter -InputObject $_ VirtualServerName; 
            $instanceName = get-clusterparameter -InputObject $_ InstanceName; 
            $nodeName = $_.OwnerNode;
            $IPAddress = [System.Net.Dns]::GetHostAddresses($virtualServerName.Value) | where-object {$_.IsIPv6LinkLocal -eq $False}
            echo ("{0},{1}\{2},{3},{4},{5},{6}" -f $nodeName,$virtualServerName.Value, $instanceName.Value,$virtualServerName.Value,$instanceName.Value,$IPAddress,$clusterName);
        } `
        | out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append

或者这个,虽然我认为上面的内容更优雅:

Param(
    [string]$clusterName
    )

Import-Module failoverclusters

get-cluster -name $clusterName | get-clusterresource | where-object {
    $_.ResourceType.name -eq "SQL Server"
        } |
        %{
            $virtualServerName = get-clusterparameter -InputObject $_ VirtualServerName; 
            $instanceName = get-clusterparameter -InputObject $_ InstanceName; 
            $nodeName = $_.OwnerNode;
            $IPAddress = [System.Net.Dns]::GetHostAddresses($virtualServerName.Value) | where-object {$_.IsIPv6LinkLocal -eq $False}
            echo ("{0},{1}\{2},{3},{4},{5},{6}" -f $nodeName,$virtualServerName.Value, $instanceName.Value,$virtualServerName.Value,$instanceName.Value,$IPAddress,$clusterName);
        } | out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append

事实上,以下内容也应该有效,与您使用的风格保持一致:

Param(
    [string]$clusterName
    )

Import-Module failoverclusters

get-cluster -name $clusterName | get-clusterresource | where-object {
    $_.ResourceType.name -eq "SQL Server"
        } |
        %{
            $virtualServerName = get-clusterparameter -InputObject $_ VirtualServerName; 
            $instanceName = get-clusterparameter -InputObject $_ InstanceName; 
            $nodeName = $_.OwnerNode;
            $IPAddress = [System.Net.Dns]::GetHostAddresses($virtualServerName.Value) | where-object {$_.IsIPv6LinkLocal -eq $False}
            echo ("{0},{1}\{2},{3},{4},{5},{6}" -f $nodeName,$virtualServerName.Value, $instanceName.Value,$virtualServerName.Value,$instanceName.Value,$IPAddress,$clusterName);
        } |
        out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append

底线是“|”除非你已经转义了前面的换行符,否则它不能成为第一个字符,因为它被解释为一个新命令,因此缺少第一个管道元素,因此出现“空管元素”错误。