How to loop "ipconfig /renew" until it gets IP address on remote PC to test DHCP using PowerShell
The script below will loop until it received IP address from DHCP server.
If id does not receive IP it will loop indefinitely or until you stop manually or reboot PC.
I used this script on remote PC, while it had and IP address but I needed to test if DHCP server is working or could reach client via site-to-site VPN.
If PC did not come up I knew I had to fix VPN or server, etc.
1. Open command prompt by right-clicking on the "command prompt" or PowerShell icon and select "Run as administrator"
2. If you open command prompt, type PowerShell to start power shell
Microsoft Windows [Version 10.0.18362.535]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
4. While PC is alive let's find Ethernet adapted we want.
5. Run "Get-NetIPAddress" command below to get list of network adapters you have on remote PC.
PS C:\WINDOWS\system32> Get-NetIPAddress | Select InterfaceAlias, IPAddress
InterfaceAlias IPAddress
-------------- ---------
Local Area Connection* 2 fe80::1936:a04e:b4a9:b074%3
Ethernet fe80::206a:8d1d:a4e3:db15%6
Local Area Connection* 1 fe80::d9a5:9e52:9985:2512%9
Bluetooth Network Connection fe80::1c6f:c604:17c8:b573%5
Wi-Fi fe80::a41d:12dc:8ef2:6d03%10
Loopback Pseudo-Interface 1 ::1
Local Area Connection* 2 169.254.176.116
Ethernet 10.27.193.80
Local Area Connection* 1 169.254.37.18
Bluetooth Network Connection 169.254.181.115
Wi-Fi 169.254.109.3
Loopback Pseudo-Interface 1 127.0.0.1
6. Let break it down. Only show adapter with name "Ethernet" and only show IPv4 information:
PS C:\WINDOWS\system32> Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4
IPAddress : 10.27.193.80
InterfaceIndex : 6
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Dhcp
SuffixOrigin : Dhcp
AddressState : Preferred
ValidLifetime : 7.23:21:37
PreferredLifetime : 7.23:21:37
SkipAsSource : False
PolicyStore : ActiveStore
7. It looks like we only need "IPAddress" field from this output.
PS C:\WINDOWS\system32> (Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress
10.27.193.80
8. Let's build condition that will return "True" if ip matches to the one we need: Last part of IP address may change to we only keeping first three octets. "*" will ensure the rest of the string could be anyting.
PS C:\WINDOWS\system32>
((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress) -like "10.27.193.*"
True
9. Now we could use while -not (condition) { run_command } to get our task completed.
IMPORTANT: The whole command below should be one liner. Once you press enter, your computer will release IP address and you will be disconnected (if you are connected remotely). So you can copy this command into the text editor and make sure there are no line breaks.
PS C:\WINDOWS\system32> Invoke-Command -Command {ipconfig /release}; while (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress -like "10.27.193.*")) {Invoke-Command -Command {ipconfig /renew}}
Output:
Windows IP Configuration
No operation can be performed on Wi-Fi while it has its media disconnected.
No operation can be performed on Local Area Connection* 1 while it has its media disconnected.
No operation can be performed on Local Area Connection* 2 while it has its media disconnected.
No operation can be performed on Bluetooth Network Connection while it has its media disconnected.
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::206a:8d1d:a4e3:db15%6
Default Gateway . . . . . . . . . :
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Comment: Ignore this error. If you are remote you will not see it anyways and will be booted from remote PC.
Get-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress class on
the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((InterfaceAlias LIKE 'Ethernet')) AND ((AddressFamily = 2)). Verify query parameters
and retry.
At line:1 char:60
+ ... ile (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Get-NetIPAddress], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Get-NetIPAddress
Comment: We got IP address renewed.
Windows IP Configuration
No operation can be performed on Wi-Fi while it has its media disconnected.
No operation can be performed on Local Area Connection* 1 while it has its media disconnected.
No operation can be performed on Local Area Connection* 2 while it has its media disconnected.
No operation can be performed on Bluetooth Network Connection while it has its media disconnected.
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : MYDOMAIN.local
Link-local IPv6 Address . . . . . : fe80::206a:8d1d:a4e3:db15%6
IPv4 Address. . . . . . . . . . . : 10.27.193.83
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, March 15, 2020 9:47:01 AM
Lease Expires . . . . . . . . . . : Monday, March 23, 2020 9:47:02 AM
Default Gateway . . . . . . . . . : 10.27.193.1
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
PS C:\WINDOWS\system32>
10. In t his case, everything worked fine. IP address changed and remember that this info is on the screen of the computer that you cannot see, because it disconnected you. That's why we saved computer and domain name. You can ping computer by name of by full name.domain and it should respond.
If it does not respond, you will not be able to reconnect to it. This means that it did not pick up the IP address from the server.
The good news if will be trying to renew IP address until it gets one. So once you fix your VPN, network, server issues it will pick up new IP address.
Invoke-Command -Command {ipconfig /release}; # release IP
# Run loop while IP address we read does not look like this 10.27.193.*
# if it does, while will end and PC should have correct IP
while (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress -like "10.27.193.*"))
{
Invoke-Command -Command {ipconfig /renew} # run ipconfig command to force PC
# to request new IP address from DHCP
}
If id does not receive IP it will loop indefinitely or until you stop manually or reboot PC.
I used this script on remote PC, while it had and IP address but I needed to test if DHCP server is working or could reach client via site-to-site VPN.
If PC did not come up I knew I had to fix VPN or server, etc.
1. Open command prompt by right-clicking on the "command prompt" or PowerShell icon and select "Run as administrator"
2. If you open command prompt, type PowerShell to start power shell
Microsoft Windows [Version 10.0.18362.535]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
3. Run these two command and save the computer name and domain name for later, we might need it.
PS C:\WINDOWS\system32> $env:computername
MYPC-TEST
PS C:\WINDOWS\system32> $env:userdomain
MYDOMAIN.local
PS C:\WINDOWS\system32>
4. While PC is alive let's find Ethernet adapted we want.
5. Run "Get-NetIPAddress" command below to get list of network adapters you have on remote PC.
PS C:\WINDOWS\system32> Get-NetIPAddress | Select InterfaceAlias, IPAddress
InterfaceAlias IPAddress
-------------- ---------
Local Area Connection* 2 fe80::1936:a04e:b4a9:b074%3
Ethernet fe80::206a:8d1d:a4e3:db15%6
Local Area Connection* 1 fe80::d9a5:9e52:9985:2512%9
Bluetooth Network Connection fe80::1c6f:c604:17c8:b573%5
Wi-Fi fe80::a41d:12dc:8ef2:6d03%10
Loopback Pseudo-Interface 1 ::1
Local Area Connection* 2 169.254.176.116
Ethernet 10.27.193.80
Local Area Connection* 1 169.254.37.18
Bluetooth Network Connection 169.254.181.115
Wi-Fi 169.254.109.3
Loopback Pseudo-Interface 1 127.0.0.1
6. Let break it down. Only show adapter with name "Ethernet" and only show IPv4 information:
PS C:\WINDOWS\system32> Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4
IPAddress : 10.27.193.80
InterfaceIndex : 6
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Dhcp
SuffixOrigin : Dhcp
AddressState : Preferred
ValidLifetime : 7.23:21:37
PreferredLifetime : 7.23:21:37
SkipAsSource : False
PolicyStore : ActiveStore
PS C:\WINDOWS\system32> (Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress
10.27.193.80
8. Let's build condition that will return "True" if ip matches to the one we need: Last part of IP address may change to we only keeping first three octets. "*" will ensure the rest of the string could be anyting.
PS C:\WINDOWS\system32>
((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress) -like "10.27.193.*"
True
9. Now we could use while -not (condition) { run_command } to get our task completed.
IMPORTANT: The whole command below should be one liner. Once you press enter, your computer will release IP address and you will be disconnected (if you are connected remotely). So you can copy this command into the text editor and make sure there are no line breaks.
PS C:\WINDOWS\system32> Invoke-Command -Command {ipconfig /release}; while (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress -like "10.27.193.*")) {Invoke-Command -Command {ipconfig /renew}}
Output:
Windows IP Configuration
No operation can be performed on Wi-Fi while it has its media disconnected.
No operation can be performed on Local Area Connection* 1 while it has its media disconnected.
No operation can be performed on Local Area Connection* 2 while it has its media disconnected.
No operation can be performed on Bluetooth Network Connection while it has its media disconnected.
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::206a:8d1d:a4e3:db15%6
Default Gateway . . . . . . . . . :
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Comment: Ignore this error. If you are remote you will not see it anyways and will be booted from remote PC.
Get-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress class on
the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((InterfaceAlias LIKE 'Ethernet')) AND ((AddressFamily = 2)). Verify query parameters
and retry.
At line:1 char:60
+ ... ile (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Get-NetIPAddress], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Get-NetIPAddress
Comment: We got IP address renewed.
Windows IP Configuration
No operation can be performed on Wi-Fi while it has its media disconnected.
No operation can be performed on Local Area Connection* 1 while it has its media disconnected.
No operation can be performed on Local Area Connection* 2 while it has its media disconnected.
No operation can be performed on Bluetooth Network Connection while it has its media disconnected.
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : MYDOMAIN.local
Link-local IPv6 Address . . . . . : fe80::206a:8d1d:a4e3:db15%6
IPv4 Address. . . . . . . . . . . : 10.27.193.83
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, March 15, 2020 9:47:01 AM
Lease Expires . . . . . . . . . . : Monday, March 23, 2020 9:47:02 AM
Default Gateway . . . . . . . . . : 10.27.193.1
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
PS C:\WINDOWS\system32>
10. In t his case, everything worked fine. IP address changed and remember that this info is on the screen of the computer that you cannot see, because it disconnected you. That's why we saved computer and domain name. You can ping computer by name of by full name.domain and it should respond.
If it does not respond, you will not be able to reconnect to it. This means that it did not pick up the IP address from the server.
The good news if will be trying to renew IP address until it gets one. So once you fix your VPN, network, server issues it will pick up new IP address.
Clean script code:
IMPORTANT: DO NOT USE CODE BELOW TO PASTE TO THE COMMAND LINE.
The whole command below should be one-liner. Once you press enter, your computer will release IP address and you will be disconnected (if you are connected remotely). So you can copy this command into the text editor and make sure there are no line breaks.
# Run loop while IP address we read does not look like this 10.27.193.*
# if it does, while will end and PC should have correct IP
while (-not ((Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress -like "10.27.193.*"))
{
Invoke-Command -Command {ipconfig /renew} # run ipconfig command to force PC
# to request new IP address from DHCP
}
Comments
Post a Comment