VMware Cloud Community
pfuhli
Enthusiast
Enthusiast
Jump to solution

problems after establishing connection to VIServer

Hi there,

in my first steps getting in touch with the VIToolkit I hang on an error.

My tiny script looks like this:

Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password

Get-VM | Get-Snapshot | ft { $_.vm.name },name,created

This will run if I execute the two lines separated on the CLI.

If I put it together in a script the script returns an error.

Does anyone know where this comes from?

0 Kudos
1 Solution

Accepted Solutions
Niket
Enthusiast
Enthusiast
Jump to solution

Hi Pfuhli,

We looked into the script provided by you. We made some minor changes in the same. Try to execute the below script which would work and resolve your error.

$server = Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password

Get-VM | Get-Snapshot | ft { $_.vm.name },name,created

Please let us know, if you still facing the issue.

Thanks

Niket

View solution in original post

0 Kudos
8 Replies
Niket
Enthusiast
Enthusiast
Jump to solution

Hi Pfuhli,

We looked into the script provided by you. We made some minor changes in the same. Try to execute the below script which would work and resolve your error.

$server = Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password

Get-VM | Get-Snapshot | ft { $_.vm.name },name,created

Please let us know, if you still facing the issue.

Thanks

Niket

0 Kudos
admin
Immortal
Immortal
Jump to solution

The script also works if you use select rather than format-table. I'm at a loss for why though, does anyone know?

pfuhli
Enthusiast
Enthusiast
Jump to solution

Yes that works.

Thnx!

0 Kudos
admin
Immortal
Immortal
Jump to solution

It has to do with how PowerShell formats output. It gets a little bit into some of the details so if you don't care, feel free to skip down to the punchline below.

Details here:

The story goes something like this: The PowerShell console adds a "| Out-Default" to everything you enter in the console. So when you do things a line at a time, there's a single "| Out-Default" added to each line behind the scenes. When you don't specify the formatting, it figures things out. When you do specify the formatting as in "| Format-Table", it uses the specified formatting. So now let's think about your script. The console is going to add a "| Out-Default" to your script invocation. We can sort of unfold the script invocation to see what the console is actually executing. It's something like this:


&{Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password; Get-VM | Get-Snapshot | ft { $_.vm.name },name,created} | Out-Default

In case you haven't gotten that far in PowerShell yet, the &{...} construct is defining a script block and evaluating it. This is the logical equivalent of what happens when you invoke the script. The result of all that then gets piped to Out-Default and we've now duplicated the failure on the command-line.

The reason this fails is that the Out-Default cmdlet is getting confused. It's getting an unformatted object (the result of Get-VIServer) along with some formatted objects coming from Format-Table (the object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" that you see in the error message). And it doesn't know how to resolve the combination of formatted and unformatted objects.

Punch line here:

So you have a few choices:

1. don't try to format in which case the default should be able to do something reasonable (this is probably not what you want but it is an option)

2. don't let the result of Get-VIServer into the output stream. That's what Niket's solution does by assigning it to a variable. You could achieve the same by redirecting it to $null:


Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password > $null
Get-VM | Get-Snapshot | ft { $_.vm.name },name,created

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm a bit confused now.

As you said, the "| out-default" is added to each line.

So also to the Get-ViServer line (since there is no semi-column in the original script).

In fact if you add that explicitely the script will run as well.

Get-VIServer -Server xxx.xxx.xxx.xxx -User username -Password password | Out-default

Get-VM | Get-Snapshot | ft { $_.vm.name} ,name,created

So how can the Out-default of the 2nd line of the script get confused ?

Btw I think the "| Out-Null" does the same as "> $null"


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

0 Kudos
admin
Immortal
Immortal
Jump to solution

When you're in the console and do:

PS> foo

PS> bar

there are two invocations. For each one, the console has to display output and it appends the "| out-default." When you do

PS> ./foo.ps1

there is one invocation (from the point of view of the console) regardless of how many lines are in the script. So when you execute that script, the console appends "| out-default" to the entire invocation. Which is the scriptblock I showed above. Does that make more sense?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

OK, that made it clear.

Thanks.


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

0 Kudos
halr9000
Commander
Commander
Jump to solution

So you have a few choices:

1. don't try to format in which case the default should be able to do something reasonable (this is probably not what you want but it is an option)

Great answer, Antonio. Smiley Happy

I just wanted to add that people should be careful when doing any formatting inside of a script. I personally almost never do that, instead just doing the "gathering" part of my task inside of a script, and let the out-default come to the output stream (aka STDOUT in cmd.exe / unix terms). This way you have the flexibility of further filternig or altering the output while it is still "clean" without the formatting instructions, as well as being able to send the output to the screen, a file, an export or convert cmdlet, whatever, all without changing your script.

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
0 Kudos