VMware Cloud Community
krishnaprasad
Hot Shot
Hot Shot

how to get the status of a cmdlet

Hello list,

i have a query regarding the return value of a cmdlet. To make it clear, suppose i have to add a host to datacenter. and i have given wrong password as a parameter to Add-VMHost as below

Add-VMHost -Name <IPaddress of ESX> -Location (Get-Datacenter datacentername ) -User root -Password password

suppose i have given wrong password. it returns error. when i see $? after this command, its "true". I need to see the correct status of the command executed. so how to see the command status? how to see whether the command executed successfully or not?

Thanks a lot for your help !

Regards

Krishna

0 Kudos
8 Replies
LucD
Leadership
Leadership

Do you also see this when you add $? to the script immediatly on the next line ?

When I try this, it returns $false when I use an incorrect password.

Add-VMHost -Name <IPaddress of ESX> -Location (Get-Datacenter datacentername ) -User root -Password password
$?

You could also use a trap for intercepting errors.

Something like this

Trap {
  Write-Host $_
  Continue
}

Get-Datacenter | Add-VMHost -Name esx3.test.local -User root -Password test


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

0 Kudos
krishnaprasad
Hot Shot
Hot Shot

I dont know what i am doing wrong. $? returns True even after the previous command fails.

- Krishnaprasad

0 Kudos
krishnaprasad
Hot Shot
Hot Shot

Lucd,

The trap method, i can see the error like Bad user name and password even if i get True for $?

How can I redirect the error message to a log file?

0 Kudos
LucD
Leadership
Leadership

Could you show us the session (TXT file, screenshot...) where you get this behavior ?

For redirecting the error messages to a log file you could do something like this.

trap {
  $_ | Out-File "C:\\Myscript.log" -append
  continue
}

Add-VMHost -Name <IPaddress of ESX> -Location (Get-Datacenter datacentername ) -User root -Password password


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

0 Kudos
krishnaprasad
Hot Shot
Hot Shot

LucD,

First of all thanks a ton for helping me !!

I have attached the screenshot of the failure. I have tried other commands like, Get-Datacenter, Get-Folder etc. these returns correct status. I dont know whats going wrong for Add-VMHost command.

Thanks,

krishnaprasad

0 Kudos
LucD
Leadership
Leadership

I'm at my wits end here Smiley Sad

When I try this I always get the correct status (False) returned.

Could you stop/start your PowerShell session ? Just type "exit" at the prompt and start it again from the shortcut.

What VITK version are you using ?

Do a Get-ViToolkitVersion, this should say "build 113525".

And are you using PowerShell v1 or v2 ?

If v2 what CTP release (1 or 2) ?


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

0 Kudos
krishnaprasad
Hot Shot
Hot Shot

I have already done exiting from the PS console. but still facing the same issue.

Vmware VI Toolkit version for windows 1.0 build 113525

I think from the version, i am using Toolkit 1.0

Thanks,

Krishnaprasad

0 Kudos
krishnaprasad
Hot Shot
Hot Shot

I reinstalled powershell and VIToolkit . but still i am getting the status "true" for both Add-VMHost and Remove-VMHost for faulty conditions.

Has any one faced this issue before? I may try it on a different system :_| no other way it seems !!

Any way thanks a lot for all your help !

Thanks,

krishnaprasad

0 Kudos