adrianallan
Contributor
Contributor

Change Network Settings

I have to change all the network settings after cloning multiple machines

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 virtualcenter

function set-ip {

  1. param (

  2. [Parameter(Mandatory=$True,

HelpMessage="Enter an CSV filename, eg. servers.xml", ParameterSetName =

"One")]`

  1. $File )

$Global:newcsv = Import-CSV iptest1.csv

$NewCsv |ForEach-Object {

$Global:NAME = $_.NAME

$Global:ipaddr =

$_.ipaddr

$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 =

"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

  1. Start-Job

-ScriptBlock {

#$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration

-ComputerName $NAME

  1. $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

#}

  1. $NIC.SetDNSDomain($_.DNSDom)

  2. $_.DNSDom

#$NIC.SetDynamicDNSRegistration($TRUE,$TRUE)

$VLAN = $_.VLAN

Get-VM $_.Name | Get-NetworkAdapter |

Set-NetworkAdapter -NetworkName $VLAN

$_.VLAN

}

}

}

set-ip

Tags (2)
0 Kudos
4 Replies
LucD
Leadership
Leadership

You can run the Start-Job cmdlet with the -AsJob parameter, that way you can build in a loop (with Wait-Job) to wait till the background job returns a result. Or your script can continue executing the rest of the code.

Could you place your PowerShell script between code markup ? See the "Plain Text Markup Help" button at the top right of the editor.

That makes it easier to see what you are trying to do.

Or attach the script as a file.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
adrianallan
Contributor
Contributor

I tried the start-job it does not seem to work, it will either start up and end immediately or will run forever without making any changes (not sure I'm using start-job correctly but I played with simple commands like get-services and it worked).

I’ve attached the script, I’ve been trying different things so it’s a bit messy with the comments

0 Kudos
adrianallan
Contributor
Contributor

When I do this (Put suff in a script block) it does not work just stays running

Start-Job -ScriptBlock {

$server = "iptest1"

$searcher=[wmisearcher]"select * from Win32_NetworkAdapterConfiguration where IPEnabled = 'TRUE'"

$searcher.scope.Path.Server = $server

$searcher.Options.TimeOut = new-timespan -sec 5

$nics=$searcher.Get()

foreach($NIC in $nics){

$nic.EnableStatic("10.4.39.59","255.255.255.0")

}

}

When I run it withou the start-job it work immediatly but of course takes a long time to time out

0 Kudos
adrianallan
Contributor
Contributor

When I do this (Put suff in a script block) it does not work just stays running

Start-Job -ScriptBlock {

$server = "iptest1"

$searcher=[wmisearcher]"select * from Win32_NetworkAdapterConfiguration where IPEnabled = 'TRUE'"

$searcher.scope.Path.Server = $server

$searcher.Options.TimeOut = new-timespan -sec 5

$nics=$searcher.Get()

foreach($NIC in $nics){

$nic.EnableStatic("10.4.39.59","255.255.255.0")

}

}

When I run it withou the start-job it work immediatly but of course takes a long time to time out

0 Kudos