3

我有我想要的输出,但我的问题是把它放到一个输出文件中。我不在乎叫什么 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 文件并追加?它将运行多次。

4

1 回答 1

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

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

于 2013-03-15T17:39:42.067 回答