Reputation: 418
I have the output I want, but my problem is with getting it to an out-file. I don't care what cmdlet is called, I just need it to be able to append to a CSV.
Here is my current solution, which provides the proper output (just doesn't write to a 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);
}
I've added this to then end, which returns an error:
| out-file -filepath "D:\Path\To\File\ClusterInstances.txt" -Encoding ASCII -append
returns: An empty pipe element is not allowed.
How can I get this to output to a CSV file and Append? It will be run multiple times.
Upvotes: 3
Views: 9464
Reputation: 12323
Are you adding the line with the | out-file
on the same line as the last "}" or on a separate line? If you have a "|" as the first non-whitespace character on a new line, you need to escape the preceding newline, otherwise you get the "empty pipe element" error.
Your code should look like this (note the backtick after the "}" on the second to last line):
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
Or this, though I think the above is more elegant:
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
In fact, the following should work too, in keeping with the style you're using:
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
The bottom line is that "|" can't be the first character unless you've escaped the preceding newline, because then it's interpreted as a new command, so the first pipe element is missing, hence the "empty pipe element" error.
Upvotes: 2