VMware Cloud Community
goAtsy
Contributor
Contributor
Jump to solution

Behaviour of Write-Output after using the Get-VIServer CmdLet

Hi,

I'm trying to write a table to the console, from the content of an object (example below).

As Write-Output is the last command in the pipeline it should (according to PS documentation) display the output in the console, and it does. But not after making a successful connection with Get-VIServer. If I comment out the Get-VIServer line it works.

Is this a problem in the Get-VIServer CmdLet or am i not thinking of something, and are there any workarounds.

add-PSSnapin VMware.VimAutomation.Core

Get-VIServer "Servername"

$strings = "one","two","three"

ForEach ($string in $strings) {

$obj = New-Object PSObject

$obj | Add-Member NoteProperty Original $string

$obj | Add-Member NoteProperty Uppercase $string.ToUpper()

$obj | Add-Member NoteProperty Lowercase $string.ToLower()

Write-Output $obj

}

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Hi,

I'm trying to write a table to the console, from the content of an object (example below).

As Write-Output is the last command in the pipeline it should (according to PS documentation) display the output in the console, and it does. But not after making a successful connection with Get-VIServer. If I comment out the Get-VIServer line it works.

Is this a problem in the Get-VIServer CmdLet or am i not thinking of something, and are there any workarounds.

add-PSSnapin VMware.VimAutomation.Core

Get-VIServer "Servername"

$strings = "one","two","three"

ForEach ($string in $strings) {

$obj = New-Object PSObject

$obj | Add-Member NoteProperty Original $string

$obj | Add-Member NoteProperty Uppercase $string.ToUpper()

$obj | Add-Member NoteProperty Lowercase $string.ToLower()

Write-Output $obj

}

The Beta Get-VIServer has some unwanted output that interferes with PowerShell's default formatting. To work around it, do Get-VIServer <your arguments> > $null.

View solution in original post

Reply
0 Kudos
6 Replies
halr9000
Commander
Commander
Jump to solution

It works for me.

PS C:\Documents and Settings\hrottenberg\My Documents\WindowsPowerShell\scripts\> ./test2

Name                              Port                             User
----                              ----                             ----
mojito                            443                              <user>

Original  : one
Uppercase : ONE
Lowercase : one


Original  : two
Uppercase : TWO
Lowercase : two


Original  : three
Uppercase : THREE
Lowercase : three

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

For me it doesn't.

Powershell 1.0 v 6.0.5430.0.

VI Toolkit build build 81531.

Tried it from the VI Toolkit shell and from PowershellPlus.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

I'm running a later build, it's possible this is related to a fixed bug.

To the original poster...just don't put add-pssnapin and get-viserver in your script. Run your scripts from a session with the snapin already loaded, and invoke get-viserver before executing scripts.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi,

I'm trying to write a table to the console, from the content of an object (example below).

As Write-Output is the last command in the pipeline it should (according to PS documentation) display the output in the console, and it does. But not after making a successful connection with Get-VIServer. If I comment out the Get-VIServer line it works.

Is this a problem in the Get-VIServer CmdLet or am i not thinking of something, and are there any workarounds.

add-PSSnapin VMware.VimAutomation.Core

Get-VIServer "Servername"

$strings = "one","two","three"

ForEach ($string in $strings) {

$obj = New-Object PSObject

$obj | Add-Member NoteProperty Original $string

$obj | Add-Member NoteProperty Uppercase $string.ToUpper()

$obj | Add-Member NoteProperty Lowercase $string.ToLower()

Write-Output $obj

}

The Beta Get-VIServer has some unwanted output that interferes with PowerShell's default formatting. To work around it, do Get-VIServer &lt;your arguments&gt; &gt; $null.

Reply
0 Kudos
goAtsy
Contributor
Contributor
Jump to solution

Thank you,

These both seem to work

Get-VIServer "servername" > $null

Get-VIServer "servername" | Out-Null

@halr9000:

Is there a good reason for not adding snapins and connecting to the server from the script? I like better to start from a basic PowerShell Console and add whichever extensions i want to use.

This is what i use for adding the snapin:

If (-Not(Get-PSSnapin -registered | Where-Object {$_.name -eq "VMware.VimAutomation.Core"}))

{Write-Error "VI Toolkit isn't installed, the script cannot continue"; Break}

Else

{Write-Host "VI Toolkit is installed, found snap-in"}

If (-Not(Get-PSSnapin | Where-Object {$_.name -eq "VMware.VimAutomation.Core"}))

{Write-Host "Loading VI Toolkit snap-in"; add-PSSnapin VMware.VimAutomation.Core}

Else

{Write-Host "Snap-in is already loaded"}

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

Is there a good reason for not adding snapins and connecting to the server from the script? I like better to start from a basic PowerShell Console and add whichever extensions i want to use.

Well I guess for the most part its just a matter of preference. However, RAM is cheap, so I do things like the below in my $profile:

# ---------------------------------------------------------------------------
# Add third-party snapins
# ---------------------------------------------------------------------------

$snapins = "psmsi", # Windows Installer PowerShell Extensions
	"PshX-SAPIEN", # AD cmdlets from Sapien
	"Quest.ActiveRoles.ADManagement", # more AD stuff
	"PowerGadgets",
	"VMware.VimAutomation.Core",
	"PoshHttp",
	"NetCmdlets"

$snapins | ForEach-Object { 
  if ( Get-PSSnapin -Registered $_ -ErrorAction SilentlyContinue ) {
    Add-PSSnapin $_
  }

# ---------------------------------------------------------------------------
# Load function / filter definition library
# ---------------------------------------------------------------------------
Get-ChildItem scripts:\lib-*.ps1 | % { 
  . $_
  write-host "Loading library file:`t$($_.name)"
}

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos