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