A wee bit of help needed.

Hi folks,

I’m using the netapp poershell toolkit. I have an issue with some code:

$node_info = Get-NcNode

PS C:\windows\system32> foreach ($node in $node_info) {write-host "Node:" $node.Node}
Node: netapp_clus_1-01

PS C:\windows\system32> foreach ($node in $node_info) {add-content "C:\output.txt" "Node:" $node.Node}
Add-Content : A positional parameter cannot be found that accepts argument 'netapp_clus_1-01'.
At line:1 char:32
+ foreach ($node in $node_info) {add-content "C:\output.txt" "Node:" $node.Node}
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand

SO you can see the write-host cmdlet works perfectly and prints the Node name to the console. Why does it fail when trying to write that information to the text file?

Hello,

I think the problem is here: add-content “C:\output.txt” “Node:” $node.Node

You are using “Node:” as a positional parameter and the cmdlet add-content does not understand this.

Try:

Add-Content -Path 'C:\output.txt' -Value "Node: $node.Node"

good luck
Albert

SO this it the original code I wrote:

$gather_controller_info = 
{
$netAppSystemInfo = Get-NCNode

        Write-Host `n"Controller information (CDOT)"`n 

        foreach ($node in $netappSystemInfo) {
            
            Write-Host "Node" $node.Node
            Write-Host "Healthy?:" $node.IsNodeHealthy
            Write-Host "Uptime  :" $node.NodeUptime
           
            
        } 

"`n"
}
Invoke-Command $gather_controller_info

And it gives me:

Controller information (CDOT)

Node netapp_clus_1-01
Healthy?: True
Uptime  : 255253

So you see that it is using the GetNcNode cmdlet to retireive the information and write it out to host.

Your solution doesn’t work unfortunately. Essentially, what I need is those three output lines, displaying on the console AND written to a text file that I have defined as $dir. Thanks for your help so far. I’m thinking maybe | out-file is the way fwd, but I cannot get that to work either…

First, you never specified originally that the goal was to print and send the data to a file, so it’s not really fair to say a solution doesn’t work if you don’t define requirements. That said, Albert was trying to explain that if you don’t specify a parameter when you run a Powershell command, it’s going to use positional parameters. Say you have a command asking for First Name, LastName and Age and you say My-Command “John” 24, you’re assuming that the command is going to know where those values map. You might get an error like “Last Name can’t be numeric” because position-ally it is expecting First, Last and Age. Best practice is to specify the parameter so that Powershell know what parameters you are passing to a command.

Next, if you want to display just the data then you can just display the variable (specified as #1). It can be formatted as List which would look like how you are dumping it to a file by piping to Format-List (e.g. $netAppSystemInfo | Format-List). If you want to write what you are putting in the text file, you need to have a line to write it and a line to add the content to a file. Here is an example:

$netAppSystemInfo = Get-NCNode
$netAppSystemInfo #1: One simple way to Write data to the console

#2: or, if you want to print what is going into the file, you can do
#something like this
"`r`nController information (CDOT)`r`n" 
Add-Content -Path 'C:\output.txt' -Value "`r`nController information (CDOT)`r`n" 

foreach ($node in $netappSystemInfo) {
            
    ("Node: {0}" -f $node.Node)
    Add-Content -Path 'C:\output.txt' -Value ("Node: {0}" -f $node.Node)
    ("Healthy: {0}" -f $node.IsNodeHealthy)
    Add-Content -Path 'C:\output.txt' -Value ("Healthy: {0}" -f $node.IsNodeHealthy)
    ("Uptime: {0}`r`n" -f $node.NodeUptime)
    Add-Content -Path 'C:\output.txt' -Value ("Uptime: {0}`r`n" -f $node.NodeUptime)
           
} 

Will, I recommend that you get in the habit of presenting data as PowerShell object not text.

Try this code for example:

#requires -Version 4

$MyOutput = @()
foreach ($Node in (Get-NCNode)) {
    $Props = [ordered]@{
        Node    = $Node.Node
        Healthy = $Node.Node.IsNodeHealthy
        Uptime  = $Node.NodeUptime
    }
    $MyOutput += New-Object -TypeName PSObject -Property $Props
}

# Now that you have the data in the $MyOutput array, you can dsiplay it to the console:
"Controller information (CDOT)"
$MyOutput | sort Node | FT -a 

# or Display it to ISE gridview
$MyOutput | Out-GridView

# or save it to CSV file:
$MyOutput | Export-Csv .\nodes.csv -NoTypeInformation

# or text:
$MyOutput | Out-File .\nodes.txt -Force

# For complex data types (does not apply here), you can export to XML:
$MyOutput | Export-Clixml .\nodes.xml

Let me know if it produces the desired output ( I have no NetApp to test with), and if you like to go over any part of the code…

Hi,

Output is:

Controller information (CDOT)

Node             Healthy Uptime
----             ------- ------
netapp_clus_1-01         179426

Also, I have done:

$MyOutput | Out-File -FilePath "c:\output.txt" -Force
notepad "c:\output.txt"

This gives me:

output.txt

You can see that the formatting has gone a bit wrong as the results aren’t in line and also healthy value is missing from console output and text file.

@Sam Boutros - As you eluded, you should use a PSObject, but Get-NCNode returns the object. So, $MyOuput = Get-NCNode already returns everything in your loop. If you wanted it ordered, you could do $MyOuput = Get-NCNode | Select Node, Healthy, Uptime

@Will M - It appears you are attempting to do a report. If you are going to analyze the data, then putting in a text file isn’t the best place for it. You can do a ton of analysis using a PSObject, but if you are doing a report, then I would recommend HTML which will allow you to make it as nice as you want for email or whatever (check out the eBook above, ‘Creating HTML Reports in PowerShell’). The question really is, what are you doing with the data?

@Rob - your code works great. Thank you very much indeed. I am indeed creating a healthcheck report for some netapp kit we have in our environment. I was using text output because my intention was to use a macro in excel use the text file to create a report. However, perhaps html would be better…I’ll look into your suggestion.
@Sam - thanks for your contribution with this. Appreciate your help.

The best part of Powershell is you don’t need Excel to do analysis, you can do it in Powershell. You can build awesome HTML reports that will look much more presentable than using just text. Good luck and have fun!!