I have the following code I use to try to change the settings on our servers from dhcp to static. It gets "hung" up on the enablestatic() peice, I know its because after it changes the ip it looses the connection but how do I get Powershell to either fork off this command or time out earler. BTW it makes the changes it just stays hung for a long time before moving onto other machines. Any help appreciated (there are a few extral lines in there that I was playing around with to see if I could get it to work)
# Connect-VIServer server
function set-ip { # param ( # [Parameter(Mandatory=$True, HelpMessage="Enter an CSV filename, eg. servers.xml", ParameterSetName = "One")]`# [string]$File ) $Global:newcsv = Import-CSV iptest1.csv
$NewCsv |ForEach-Object {
$Global:NAME = $_.NAME [string]$Global:ipaddr = $_.ipaddr [string]$Global:subnet = $_.subnet $Global:NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $NAME | where{$_.IPEnabled -eq “TRUE”} #Get-WmiObject -list win32_networkadapterconfiguration -ComputerName $NAME foreach($Global:NIC in $NICs) {
$gway = $_.gway $DNSServers = $_.DNS1,$_.DNS2 $NIC.SetWINSServer($_.Wins1,$_.Wins2) Write-Host "WINS Set" $_.Wins1,$_.Wins2
$NIC.SetGateways($gway) Write-Host "GateWay Set" $_.gway $NIC.SetDNSServerSearchOrder($DNSServers) Write-Host "DNS Set" $DNSServers $NIC.SetDynamicDNSRegistration($TRUE,$TRUE) Write-Host "DNS Registration Set" $NIC.SetTcpipNetbios($TRUE) Write-Host "Netbios Over TCPIP" $DNSSuffix = @("lion.com"),@("cat.com"),@("rat.loc"),@("bat.com") #$nic1 = [wmiclass] "win32_networkadapterconfiguration" $Global:NIC1 = Get-WmiObject -list win32_networkadapterconfiguration -ComputerName $_.Name $nic1.SetDNSSuffixSearchOrder($DNSSuffix) #Invoke-WmiMethod -path Win32_NetworkAdapterConfiguration -Name SetDNSSuffixSearchOrder -ArgumentList $DNSSuffix
#$global:nicST = Gwmi win32_NetworkAdapterConfiguration# Start-Job -ScriptBlock {#$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $NAME # $Global:NICw = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $NAME # $NICs.psbase.scope.options.timeout = 5 * 10000000# $NICw.EnableStatic("10.4.39.37","255.255.255.0") $NIC.EnableStatic($ipaddr,$subnet) Write-Host "IP and SubNet Set" $_.IPaddr $_.Subnet #}
# $NIC.SetDNSDomain($_.DNSDom)# $_.DNSDom
#$NIC.SetDynamicDNSRegistration($TRUE,$TRUE)
# $VLAN = $_.VLAN# Get-VM $_.Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $VLAN# $_.VLAN } }}set-ip
I am facing the same issue. did you get any wayout.
Any help is appreciated.
There isn't an elegant solution to this problem. As you have surmised when the IP address is chanaged the connection is broken but it doesn't timeout quickly.
The only way to handle this that I have been able to come up with is to wrap the change in a PowerShell job. That way the timeout happens within the job but you can carry on working
if you get a copy of PowerShell and WMI
http://www.manning.com/siddaway2/
This is covered in chapter 13