Register this connection's addresses in DNS in Windows 10 - windows

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

Related

Is a domain joined windows machine's objectGUID or ObjectSID as stored by Active Directory also stored locally?

I need to link a computer to an object in Active Directory.
Basically, I need to link a locally available property to a property that can be found in AD so I can positively link the device to the computer object in Active Directory. The device may be off network, and not able to access a domain controller when it is queried.
From research, the computer's objectGUID or objectSID attributes stored locally do not match the AD ones.
ie:
get-adcomputer -identity ComputerName -property MachineGUID,SID
returns different GUID and SID than the ones found via
wmic path win32_computersystemproduct get uuid
and PSTools's
PSGetSID
..so, is there an indentifier that can be retrieved both locally and in AD that can allow to match a computer?
Thanks
Try locally:
Get-WmiObject -class SoftwareLicensingService | Select-object ClientMachineID
and then on AD Domain controller:
Import-module activedirectory
$ComputerName="enter computername here"
$Computer = Get-ADComputer –Identity $ComputerName –property *
$ComputerCMID = Get-WmiObject –computer $ComputerName -class SoftwareLicensingService | Select-object ClientMachineID
Write-Output "$ComputerName has the CMID: $ComputerCMID "

how to get unidentified network adaptor name with batch

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.

how to change a disabled network adapter configuration by powershell

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

Powershell 3.0 : Alternative to "Get-Volume"

I'm trying to get various properties for each hdd-volume on the computer.
I was using the cmdlet get-volume and then walking through it via foreach, but that cmdlet does not exist in Windows Server 2008. :(
Does anybody know an alternative?
I just need the drive letter, objectId/guid, free space, total space, and the name of each volume.
The WMI class Win32_Volume has the information you are looking for
Get-WMIObject -Class Win32_Volume | Select DriveLetter,FreeSpace,Capacity,DeviceID,Label
Which a little fancy footwork you can make the drive space properties looking a little more appealing.
Get-WmiObject -Class Win32_Volume |
Select DriveLetter,
#{Label="FreeSpace (In GB)";Expression={$_.Freespace/1gb}},
#{Label="Capacity (In GB)";Expression={$_.Capacity/1gb}},
DeviceID,Label |
Format-Table -AutoSize
Get-Volume is only in Powershell 4.
You can do this tho:
Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID, Size, FreeSpace, VolumeName

How to add more than one machine to the trusted hosts list using winrm

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"}

Resources