Greetings,
As a followup to a question I posted yesterday, I have the following script:
$content = Get-Content -Path "C:\vms.csv"
foreach ($vm in $content){
# Grabbing VM object based off of name supplied in CSV
$vm = Get-VM -Name $vm
Write-Host "+++++++++++++++++++++++ WORKING ON " $vm.name " +++++++++++++++++++++++"
# Identify the current VLAN, change it, provide verification output
$nic = ($vm | Get-NetworkAdapter)
$net = ($vm | Get-NetworkAdapter).NetworkName
Write-Host $vm.name " is currently on $net"
Set-NetworkAdapter -NetworkAdapter $nic -NetworkName "VM Network Vlan20"
$net = ($vm | Get-NetworkAdapter).NetworkName
Write-Host "Now it's on $net"
# Modifying IP to be on the correct subnet
Write-Host "Retrieving Guest NIC..."
#$nic = $vm | Get-VMGuestNetworkInterface -GuestUser "test" -GuestPassword "test" | where { $_.name -eq "Local Area Connection" }
$guest = Get-VMGuest $vm
$nic = Get-VMGuestNetworkInterface -VMGuest $guest -GuestUser "test" -GuestPassword "test" | where { $_.name -eq "Local Area Connection" }
Write-Host "Setting Guest NIC..."
Set-VMGuestNetworkInterface -VmGuestNetworkInterface $nic -IPPolicy Static -Gateway 10.132.20.1 -ip 10.132.20.28 -Netmask 255.255.252.0 -dns 8.8.8.8 -GuestCredential (Get-Credential)
Write-Host "Done!"
}
Sorry the formatting is all goofy.. Anyway - this is on a total lab setup, uname/pass = test/test. The user is the only local admin to the Win7 box. I've disabled UAC since another post eluded to that being an issue.. I'm at a loss as to why it keeps saying "the requested operation requires elevation (Run as administrator)". Any thoughts? I've tried the set-VM... line with -guestCred, -guestUser/-guestPass - nothing works. Thanks in advance for any insight into this!
As you know these cmdlets are in fact fronts for some scripts that are launched in the guest OS through the same mechanism as Invoke-VMScript uses.
In this specific script (SetVMGuestNetworkInterface_windows7_64Guest.bat or SetVMGuestNetworkInterface_windows7Server64Guest.bat), the netsh command is used to change the NIC settings.
In fact the netsh command needs to be started via the runas command (and this is not the same as running netsh from an admin account).
I would propose to run the netsh commands via an Invoke-VMScript cmdlet, that would make it easier to do this via the runas command.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I've tried that actually (Seen Al's similar script using Invoke-VMScript), but the problem is that even executing with /runas /user:localadmin (and even trying /savecred) - it never prompts me for a password, or just completely hangs altogether. Also, the only user I could use is the local admin, which .. I'm already using w/ the PowerCLI cmdlet. I know this can be done easily with WMI but I'm trying to avoid using it. Any further thoughts?