Automation

 View Only
  • 1.  problems after establishing connection to VIServer

    Posted Mar 28, 2008 10:26 AM

    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?



  • 2.  RE: problems after establishing connection to VIServer
    Best Answer

    Posted Mar 28, 2008 11:56 AM

    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



  • 3.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 03:07 PM

    Yes that works.

    Thnx!



  • 4.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 02:50 PM

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



  • 5.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 04:48 PM

    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
    
    



  • 6.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 05:52 PM

    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"



  • 7.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 06:05 PM

    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?



  • 8.  RE: problems after establishing connection to VIServer

    Posted Mar 28, 2008 06:16 PM

    OK, that made it clear.

    Thanks.



  • 9.  RE: problems after establishing connection to VIServer

    Posted Mar 29, 2008 01:19 AM

    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. :smileyhappy:

    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)