question

rallemay-6431 avatar image
0 Votes"
rallemay-6431 asked rallemay-6431 published

Change interface from static IP to DHCP via GPO startscript/Powershell

I want to change the interfaces of all computers of two AD OUs. Currently all interfaces have static IPs and DNS servers. I wrote this Powershell Script:

 $getIP = get-netipaddress | Where-Object IPAddress -Like "192.168.18.*"
 $index = $getip.InterfaceIndex
 Get-NetIPInterface | where-Object InterfaceIndex -like "$index" | Set-NetIPInterface -Dhcp Enabled
 $rep = Get-NetIPInterface | where-Object InterfaceIndex -like "$index"
 $rep1 = $rep.InterfaceAlias
 $Hostn = hostname
 $Test = Get-NetIPInterface -InterfaceIndex $index | Where-Object {$_.Dhcp -like "Enabled"}
 if ($Test){
 $DHCPSTATUS = "DHCP active"
 $report = "The Interface $Rep1 on $Hostn was set to DHCP . The DNS Server are resettet"
 Set-DnsClientServerAddress -InterfaceIndex "$index" -ResetServerAddresses
 }
 else{
 $DHCPSTATUS = "DHCP_not_active"
 $report = "The Interface $Rep1 on $Hostn was NOT set to DHCP . The DNS Server are NOT resettet"
 }
 $report | Out-File \\Server\d$\DHCP\Result\$Hostn"_"$DHCPSTATUS.txt

For the two OUs I enabled a GPO with a Startscript: https://i.stack.imgur.com/ZSZbV.png

When I run gpresult, It shows that the GPO was used, but nothing happened to the interface, neither was a txt written.

windows-server-powershellwindows-group-policy
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers