Microsoft Virtualization Discussions

Hash Table after Connect to a Controller

mcgue
3,128 Views

I have a feeling I'm missing something obvious, but this is what is going on.  If I make a hash table and output before connecting to a controller, like this:

$hash=@{}

$hash.Add("this","that")

$hash.GetEnumerator() | Sort-Object -Property key

Connect-NaController $Controller -Credential $cred

My output looks like this:

Name                           Value                            

----                           -----                            

this                           that  

However, if I keep all the lines the same and move the "Connect-NaController" first, I get this output:

Key   : this

Value : that

Name  : this

Any idea on how I can get the table output I would expect?

1 ACCEPTED SOLUTION

beam
3,128 Views

Since the HashTable and output of Connect-NaController are of different types, PowerShell formats the output with the object that is passed through the pipeline first.  There are two ways to change this:

1) Don't let the result of Connect-NaController get passed down the pipeline.  You can accomplish this by capturing the output in a variable, or by piping the result into Out-Null:

Script:

$controller = Connect-NaController controllerA

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash

Or:

Connect-NaController controllerA | Out-Null

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash

Output:

Name                           Value

----                           -----

Key1                           Value

Key2                           Value

Key3                           Value

2) Explicitly pass the results of the HashTable and Connect-NaController to Format-Table.  This will output tables for both types.

Script:

Connect-NaController controllerA | Format-Table

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash | Format-Table

Output:

Name                 Address           Ontapi   Version

----                 -------           ------   -------

controllerA          192.168.0.102     1.14     NetApp Release 7.3.5: Mon Nov 22 20:37:51 PST 2010

Name                           Value

----                           -----

Key1                           Value

Key2                           Value

Key3                           Value

Thanks,

Steven

View solution in original post

2 REPLIES 2

beam
3,129 Views

Since the HashTable and output of Connect-NaController are of different types, PowerShell formats the output with the object that is passed through the pipeline first.  There are two ways to change this:

1) Don't let the result of Connect-NaController get passed down the pipeline.  You can accomplish this by capturing the output in a variable, or by piping the result into Out-Null:

Script:

$controller = Connect-NaController controllerA

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash

Or:

Connect-NaController controllerA | Out-Null

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash

Output:

Name                           Value

----                           -----

Key1                           Value

Key2                           Value

Key3                           Value

2) Explicitly pass the results of the HashTable and Connect-NaController to Format-Table.  This will output tables for both types.

Script:

Connect-NaController controllerA | Format-Table

$hash = @{Key1="Value";Key2="Value";Key3="Value"}

$hash | Format-Table

Output:

Name                 Address           Ontapi   Version

----                 -------           ------   -------

controllerA          192.168.0.102     1.14     NetApp Release 7.3.5: Mon Nov 22 20:37:51 PST 2010

Name                           Value

----                           -----

Key1                           Value

Key2                           Value

Key3                           Value

Thanks,

Steven

mcgue
3,128 Views

Thanks Steven for the fast and thorough response.  Works perfectly as you described.

Public