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
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
I dont know what i am doing wrong. $? returns True even after the previous command fails.
- Krishnaprasad
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?
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
I'm at my wits end here
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
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
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