Hello,
I'm trying to replicate vicfg-route.pl functionality via New-TkeVMHostRoute VI toolkit extension. Problem is i can't seem to figure out how to script it. if i open a new powerCLI window and do the following manually it works:
Connect-VIServer hostname
Get-VMHost hostname | New-TKeVMHostRoute -TableRoute -Network 10.10.10.0 -PrefixLength 24 -Gateway 10.10.10.1
That works perfectly but here is what i have in the script and this fails to run:
$vmhost = "hostname"
Connect-VIServer $vmhost
Get-VMhost $vmhost | New-TKeVMHostROute -TableRoute -Network 10.10.10.0 -PrefixLength 24 -Gateway 10.10.10.1
Any ideas of why script is failing to run but running it manually from PowerCLI window works great
Thanks
Alex
Neither, the Get-VmHost cmdlet returns a VMHostImpl object (which is also the object the Get-TKEVMHostRoute function expects as input).
The $global:defaultviserver is the ESX/ESXi (or vCenter) to which the last Connect-ViServer connected.
In your script you connected to an ESX/ESXi host.
The subsequent Get-VmHost cmdlet knows, through the $global:defaultviserver, where you are connected and returns the correct VMHostImpl object.
You can see the type of an object that is returned by a cmdlet by doing
Get-VmHost | Get-Member
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
My guess is that the VI Toolkit Extensions module is not loaded when you run your script.
What specifc error are you seeing? That would help a lot.
=====
Carter Shanklin
Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]
The first line works and connection to host is established when i run my script. here is the error:
Get-View : 11/13/2009 5:27:01 PM Get-View You are not currently conne
cted to any servers. Please connect first using Connect-VIServer or one of its
aliases.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\viToolkitExtensions\viToo
lkitExtensions.psm1:843 char:18
+ $hns = get-view <<<< $hView.ConfigManager.NetworkSystem
+ CategoryInfo : ResourceUnavailable: ( , ViServerCon
nectionException
+ FullyQualifiedErrorId : Core_BaseCmdlet_NotConnectedError,VMware.VimAuto
mation.Commands.DotNetInterop.GetVIView
You cannot call a method on a null-valued expression.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\viToolkitExtensions\viToo
lkitExtensions.psm1:868 char:33
+ $hns.UpdateIpRouteTableConfig <<<< ($config)
+ CategoryInfo : InvalidOperation: (UpdateIpRouteTableConfig:Stri
ng) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Are you connecting in a subroutine? If so that causes problems if you don't set the $global:defaultVIServer variable. There's an example of how to do that in Connect-Recent within the VI Toolkit Extensions.
=====
Carter Shanklin
Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]
That is above my knowledge of powershell
3 lines that i listed in teh first post are seprate script that i created while troubleshooting it and took it out of my main config script. most likely its not global and is causing aproblem. do you happen to have a link to where Connect-Recent is discussed? i can't seem to find it
Alex
So i think i got it but i have a question:
here is what i was able to do:
$vmhost = "hostname"
Connect-viserver $vmhost -user root -password 12345
$global:defaultviserver = $defaultVIserver
get-vmhost | Get-TKEVMHostRoute
My quesiton is this. what parameter does get-vmhost get $vmhost or $global:defaultviserver?
Neither, the Get-VmHost cmdlet returns a VMHostImpl object (which is also the object the Get-TKEVMHostRoute function expects as input).
The $global:defaultviserver is the ESX/ESXi (or vCenter) to which the last Connect-ViServer connected.
In your script you connected to an ESX/ESXi host.
The subsequent Get-VmHost cmdlet knows, through the $global:defaultviserver, where you are connected and returns the correct VMHostImpl object.
You can see the type of an object that is returned by a cmdlet by doing
Get-VmHost | Get-Member
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD - Thank you for explanation. makes perfect sense and makes my life easier