VMware Cloud Community
kalex
Enthusiast
Enthusiast
Jump to solution

help with vmkernel route scripting

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
7 Replies
admin
Immortal
Immortal
Jump to solution

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]

Reply
0 Kudos
kalex
Enthusiast
Enthusiast
Jump to solution

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: (Smiley Happy , 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

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

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]

kalex
Enthusiast
Enthusiast
Jump to solution

That is above my knowledge of powershell Smiley Happy

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

Reply
0 Kudos
kalex
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
kalex
Enthusiast
Enthusiast
Jump to solution

LucD - Thank you for explanation. makes perfect sense and makes my life easier Smiley Happy

Reply
0 Kudos