VMware Cloud Community
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Changing static Guest NIC settings with PowerCLI

So since the set-vmguestnetworkinteface seems to be deprecated and not working well I've been trying to run invoke-vmscript to get my final result, which is to change a NIC from dynamic to static and populate IP/Gateway/Subnet/DNS

The script I'm running is to automate a swap from E1000 to VMXNET3 with static IP's.

The script is taking all the initial network info info from the specific VM Guest with a GWMI query and making them variables. (works fine)

the script then will remove the E1000 and add a VMXNET3. (works fine)

The VMXNET3 will load up as DHCP which is undesired for the specific VM's im dealing with. (working as intended)

The script then needs to run an invoke-vmscript and elevate the PS window to be able to either do netsh or a number of cmdlets to set the correct NIC info via VMtools.

I am having issues trying to elevate a PS prompt on a guest OS and inject variables into it from my powercli session

if i didn't need elevated privs, this would work fine:

Invoke-VMScript -VM $vm -ScriptText "netsh interface ipv4 set address ""$nicName"" static $IPAddress $subnetMask $defaultGateway" -GuestUser test\user -GuestPassword Password

As you can see I'm trying to add the NIC variables of the old E1000 into the VMXNET3

Anyone ran into an issue like this before and know how to solve?

thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can escape the dollar sign with a back tick, so the variable will not be substituted.

Then run the resulting string on the target station.


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

Yes, and afaik, you need to disable UAC to be able to do this.


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

CenturaBro
Enthusiast
Enthusiast
Jump to solution

not sure if the MSFT guys will do that happily heh

0 Kudos
LucD
Leadership
Leadership
Jump to solution

He better not :smileygrin:

There is something strange I don't understand either.

That you need elevation when a remote session wants to do something in the system I understand, but a process started via Invoke-VMScript should be, imho, be considered as a local process (since VMware Tools is running local).

Does this method where the local Administrator (not the same as an account the local Administrators group) account is used, work for you?

For testing I don't think you need to rename the local Administrator account.

See VMware: Execute scripts by Invoke-VMScript as Administrator


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

0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Id expect you're right about VMtools running as local admin but for some reason there still needs to be elevation on the PS prompt....

I'm going to try to use the local admin acct on the box but it looks like the disable UAC is being controlled by a GPO that I'm in the middle of tracking down.

this is all on a test box so it really wont matter but in prod i still think that changing a GPO for the whole environment so i can run my "measly automation script" will be a rough battle to fight.

Nonetheless I'm going to try the link you sent and see where it gets me in our lab.

Sucks when you're about 90% done with a script and the last hurdle is the biggest

0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

95% there....

After i got UAC diasabled i was able to combine New-NetIPAddress and Set-dnsclientserveraddress in 2012 to configure the static NIC info.

problem is those don't exist in 2008... so i decided to apply it to the WMI win32_networkadapterconfiguration with a big one liner below:

Invoke-VMScript -VM $vm -ScriptText "$wmi1 = Get-wmiobject win32_networkadapterconfiguration -filter ""ipenabled = 'true'""; $wmi1.EnableStatic(""$IPAddress"",""$defaultGateway""); $wmi1.SetGateways(""$defaultGateway"" 1); $Array = @(""$primaryDns"",""$secondaryDns""); $wmi1.SetDNSServerSearchOrder($array)"

Problem is there are some variables i want filled in here from the PS i run it from like  $ipaddress  and some i want defined in the script that i run on the guest OS... like $wmi1

if i run this script as is, it thinks I'm trying to call $wmi1 before passing it to the guest OS... which is blank so it errors out

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can escape the dollar sign with a back tick, so the variable will not be substituted.

Then run the resulting string on the target station.


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

CenturaBro
Enthusiast
Enthusiast
Jump to solution

that almost makes sense except the back tick is also substituted on the variable in the GuestOS as well when passed through.

for Example:

Invoke-VMScript -VM $vm -ScriptText "$wmi1 = Get-wmiobject win32_networkadapterconfiguration -filter ""ipenabled = 'true'"""

ScriptOutput

-----------------------------------------------------------------------------------------------------------------------|  = : The term '=' is not recognized as the name of a cmdlet, function, script

|  file, or operable program. Check the spelling of the name, or if a path was

|  included, verify that the path is correct and try again.

|  At line:1 char:5

|  + & { = Get-wmiobject win32_networkadapterconfiguration -filter "ipenabled =

|  'true ...

|  +     ~

|      + CategoryInfo          : ObjectNotFound: (=:String) [], CommandNotFoundEx

|     ception

|      + FullyQualifiedErrorId : CommandNotFoundException

|  

-----------------------------------------------------------------------------------------------------------------------

and

Invoke-VMScript -VM $vm -ScriptText "`$wmi1 = Get-wmiobject win32_networkadapterconfiguration -filter ""ipenabled = 'true'"""

ScriptOutput

-----------------------------------------------------------------------------------------------------------------------| 

-----------------------------------------------------------------------------------------------------------------------

The difference being the back tick in the $wmi1

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you display the content in $wmi1? Just add a line displaying the content of the variable.
Otherwise the lack of output would be normal.


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

0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

I obviously haven't had my coffee yet... should have known that haha

looks good now.  thanks!

0 Kudos