I want to changed an networking adapter ip address. For some security reason, we should change it when it is disabled.
I used the following:
Set-NetIPAddress -InterfaceAlias "Ethernet 3" -PrefixLength 20 -IPAddress 10.91.62.201
But that failed with following message:
Set-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 ((IPAddress LIKE '10.91.62.201')) AND ((InterfaceAlias LIKE 'Ethernet 3'))
Is there anyone who know how to changed a disabed networking adapter configuration. Thanks.
You can update it through the registry:
Get the adapter object (We need the GUID as the registry identifies the adapters by guid):
$adapter = Get-WmiObject Win32_NetworkAdapter | WHERE {$_.DeviceID -eq "1"}
Update the registry accordingly:
New-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$($adapter.guid)" -Name IPAddress -Value "10.91.62.201" -Force
Related
A powershell script should enable/disable the wireless network adapter.
So i make use of the NetAdaper cmdlet.
Get-NetAdapter
Enable-NetAdapter -Name 'Wi-Fi'-Confirm:$false
Disable-NetAdapter -Name 'Wi-Fi'-Confirm:$false
Its works fine on english language systems.
On system with other languages configured it fails due to the wireless adapter does not have "Wi-Fi" as name.
Example would be german.
Enable-NetAdapter -Name 'WLAN'-Confirm:$false
Disable-NetAdapter -Name 'WLAN'-Confirm:$false
How to archive this script running on all languages?
Get-NetAdapter has a field named InterfaceType which contains a value based on this enum:
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterfacetype?view=net-5.0
According to that documentation, type 71 is a Wireless80211 device.
Just to show it's there:
[int][System.Net.NetworkInformation.NetworkInterfaceType]::Wireless80211
So you could do something like this:
Get-NetAdapter | Where-Object InterfaceType -eq 71
Which will return a list of all wireless adapters.
I already knew how to get all network adaptors names, but I can't tell the difference between normally using and not identified
By using
netsh interface ip show interfaces
or other commands ,you may get all adaptors names.
The necessary information can be obtained using the Win32_NetworkAdapter class.For example:
Get-CimInstance -ClassName Win32_NetworkAdapter -Filter #'
NetConnectionID = "Local Area Connection" AND
NetConnectionStatus=2 AND NetEnabled = True AND PhysicalAdapter = True
'# | Format-List *
The Get-WmiObject cmdlet can also be used, but starting in PowerShell 3.0, it has been superseded by Get-CimInstance.Starting with Windows 8, you can use the Get-NetAdapter cmdlet.
I need a way to programatically (powershell, batch, c#, ...) tell my VPN adapter that it has to register address in DNS.
There seems to be no parameter for this using PowerShell to create the VPN entry:
Powershell.exe -executionpolicy bypass -command Add-VpnConnection -AllUserConnection -Name "ProfileName" -ServerAddress "xyz.com" -TunnelType L2tp -EncryptionLevel Optional -L2tpPsk "password" -AuthenticationMethod MSChapv2 -Force
We had no success doing this by GPO, netsh or c# (DotRas -> VpnEntry.Options.RegisterIPWithDns = true;)
Thanks in advance!
As far as I know there are no way of doing it with the Add-VpnConnection command.
Instead you have to edit the Remote Access Phonebook.
If it is a -alluserconnection VPN the phonebook can be found here:
C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk
If it is a per user VPN adapter the phonebook can be found here:
C:\Users\USERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk
Now use this Powershell command to set the setting:
$RASPhoneBook =
“C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk”
(Get-Content $RASPhoneBook) -Replace ‘IpDnsFlags=0’, ‘IpDnsFlags=3’ |
Set-Content $RASPhoneBook
“Register this connection’s addresses in DNS” is now set for your VPN adapter.
Try something like this
Get-NetIPConfiguration | where {$_.some.identifying.vpn.feature -eq 'name'} | Get-NetConnectionProfile | Set-DnsClient -RegisterThisConnectionsAddress:$True
I'd suggest using a different approach for your use case.
Example:
Using PowerShell to Get or Set NetworkAdapterConfiguration-View and Change Network Settings Including DHCP, DNS, IP Address and More (Dynamic AND Static) Step-By-Step
Following commands can be useful to configure the settings:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "FullDNSRegistrationEnabled=true" –ComputerName . | Get-Member
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DomainDNSRegistrationEnabled=true" –ComputerName . | Get-Member
I'm trying to write a script that requires turning off the Internet entirely and then turning it back on. I'd like it to work in as many cases as possible...
support for Window 7 and up
if multiple Internet connections are on (like WiFi and LAN)
regardless of how these connections are named
limited user account, UAC?
ipconfig /release and ipconfig /renew, as suggested in this answer, do not work with 2 internet connections. /release disables the active connection (say WLAN) but the computer falls back on the LAN connection and you're still online.
$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
$connectedAdapters | Invoke-WmiMethod -Name disable
My questions are:
Is NetConnectionStatus = 2 a reliable proxy for internet access, and is it available on Windows 7 and up, regardless of the brand of your NIC?
Is this compatible with a limited user account with UAC on? I think so...
On my machine this query also catches VirtualBox Host-Only Ethernet Adapter. Is it a problem if I disable/enable it too?
Are Get-WmiObject and Invoke-WmiMethod are available on Windows 7 and up, right?
The questions are not entirely settled, but I've developed a script that works:
PowerShell Internet Connection helper functions: Go-Offline, Go-Online, Test-InternetAccess
#Requires -Version 2.0
function Test-InternetAccess {
<#
.SYNOPSIS
Tests connectivity by pinging Google DNS servers once
.DESCRIPTION
Uses Test-Connection to ping a host, with -Quiet for returning a boolean. The default is a highly available Google DNS server (8.8.4.4)
.EXAMPLE
Test-InternetAccess
.EXAMPLE
Test-InternetAccess example.com
.INPUTS
None.
.OUTPUTS
Boolean
#>
param (
[String]
$RemoteHost = "google-public-dns-b.google.com"
)
Test-Connection -Computer $RemoteHost -BufferSize 16 -Count 1 -Quiet
}
function Go-Offline {
<#
.SYNOPSIS
Disables your internet connection
.DESCRIPTION
Finds all network adapters that appear connected and disables them, taking you offline. Later on you can re-enable just those adapters, because they've been stored in an XML file. Connected adapters are detected through WMI. A NetConnectionStatus value of 2 means Connected. 7 means Media Disconnected.
.EXAMPLE
Go-Offline
.INPUTS
None.
.OUTPUTS
None.
.LINK
https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
.LINK
https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param()
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
if (Test-InternetAccess) {
$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
# Go offline
$connectedAdapters | Invoke-WMIMethod -Name disable 1>$null
# Save which adapters were connected at the time
$connectedAdapters | Select Name, DeviceID | Export-Clixml -Path $XMLLocation -Force
Write-Output "You've been taken offline!"
Sleep 1
} else {
Write-Output "Connection already down..."
Sleep 1
}
}
function Go-Online {
<#
.SYNOPSIS
Re-enables your internet connection
.DESCRIPTION
Finds all network adapters that were previously disabled by Go-Offline and enables them. This information is persisted in a temp file.
.EXAMPLE
Go-Online
.INPUTS
None.
.OUTPUTS
None.
.LINK
https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param()
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
if (!(Test-InternetAccess)) {
# Get the NICs that have been previously disabled
$connectedAdapters = Import-Clixml "$env:TEMP\Disabled-NICs.xml" | Select -ExpandProperty DeviceID | ForEach {Get-WMIObject -Class Win32_NetworkAdapter -Filter "DeviceID = $_"}
# Get back online
$connectedAdapters | Invoke-WMIMethod -Name enable | Out-Null
Write-Output "Internet access restored!" # Triggers early, before actual re-connection
Sleep 1
}
}
Tested on Windows 7.
To run powershell commands on a machine from a remote machine we have to add the remote machine to the trusted hosts list of the host machine.
I am adding machine A to machine B's trusted hosts using the following command :
winrm set winrm/config/client ‘#{TrustedHosts="machineA"}’
How to add more machines say machine C, machine D to trusted hosts list of machine B?
I prefer to work with the PSDrive WSMan:\.
Get TrustedHosts
Get-Item WSMan:\localhost\Client\TrustedHosts
Set TrustedHosts
provide a single, comma-separated, string of computer names
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineA,machineB'
or (dangerous) a wild-card
Set-Item WSMan:\localhost\Client\TrustedHosts -Value '*'
to append to the list, the -Concatenate parameter can be used
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineC' -Concatenate
winrm set winrm/config/client '#{TrustedHosts="machineA,machineB"}'
The suggested answer by Loïc MICHEL blindly writes a new value to the TrustedHosts entry.
I believe, a better way would be to first query TrustedHosts.
As Jeffery Hicks posted in 2010, first query the TrustedHosts entry:
PS C:\> $current=(get-item WSMan:\localhost\Client\TrustedHosts).value
PS C:\> $current+=",testdsk23,alpha123"
PS C:\> set-item WSMan:\localhost\Client\TrustedHosts –value $current
I created a module to make dealing with trusted hosts slightly easier, psTrustedHosts. You can find the repo here on GitHub. It provides four functions that make working with trusted hosts easy: Add-TrustedHost, Clear-TrustedHost, Get-TrustedHost, and Remove-TrustedHost. You can install the module from PowerShell Gallery with the following command:
Install-Module psTrustedHosts -Force
In your example, if you wanted to append hosts 'machineC' and 'machineD' you would simply use the following command:
Add-TrustedHost 'machineC','machineD'
To be clear, this adds hosts 'machineC' and 'machineD' to any hosts that already exist, it does not overwrite existing hosts.
The Add-TrustedHost command supports pipeline processing as well (so does the Remove-TrustedHost command) so you could also do the following:
'machineC','machineD' | Add-TrustedHost
Same as #Altered-Ego but with txt.file:
Get-Content "C:\ServerList.txt"
machineA,machineB,machineC,machineD
$ServerList = Get-Content "C:\ServerList.txt"
$currentTrustHost=(get-item WSMan:\localhost\Client\TrustedHosts).value
if ( ($currentTrustHost).Length -gt "0" ) {
$currentTrustHost+= ,$ServerList
set-item WSMan:\localhost\Client\TrustedHosts –value $currentTrustHost -Force -ErrorAction SilentlyContinue
}
else {
$currentTrustHost+= $ServerList
set-item WSMan:\localhost\Client\TrustedHosts –value $currentTrustHost -Force -ErrorAction SilentlyContinue
}
The "-ErrorAction SilentlyContinue" is required in old PS version to avoid fake error message:
PS C:\Windows\system32> get-item WSMan:\localhost\Client\TrustedHosts
WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client
Type Name SourceOfValue Value
---- ---- ------------- -----
System.String TrustedHosts machineA,machineB,machineC,machineD
winrm set winrm/config/client '#{TrustedHosts="ServerA"}'
Generates this error:
Syntax Error: input must be of the form {KEY="VALUE"[;KEY="VALUE"]}
This worked for me (Server 2016):
winrm set winrm/config/client #{TrustedHosts="ServerA"}