我有我想要的输出,但我的问题是把它放到一个输出文件中。我不在乎调用什么 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);
        }

我已将其添加到 then end,它返回一个错误:
| out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append

返回: 空管道元素是不允许的。

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

最佳答案

您是将带有 | 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

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

关于powershell - 输出文件问题(空管道元素),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15438221/

10-12 19:59