how to get unidentified network adaptor name with batch - windows

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.

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 can i find the DHCP server? [duplicate]

I got a client who asked me to find all of his Dhcp and DNS servers with some additional info like DC servers and operationsystem
so i decided to try sharpen my powershell skills but im still new to this so i wrote this script but i guess something is still missing because it doesnt work
EDIT: i managed to find a way to get the info i want which is the OS but it gets me back ALL the servers in the company
$servers = get-dhcpserverindc
foreach($server in $Servers){
get-adcomputer -filter {Operatinsytem -like "*windows server*"} -properties
Operatingsystem | sort name | format-table name,Operatinsytem
}
This is not too tricky. First off, you connect to a machine with the RSAT Tools installed, like an admin server, jump box, or Domain Controller, and get a list of all DHCP Servers.
$DHCPServers = Get-DhcpServerInDC
Then we use PowerShell's built in looping logic to step through each server, and check for the OS info you need.
ForEach ($DHCPServer in $DHCPServers){
$OSInfo = Get-CIMInstance -ComputerName $DHCPServer.DnsName -ClassName Win32_OperatingSystem
}
Finally, we'll modify this above to return the info you're looking for, namely the IP Address, Name, and OS Version
ForEach ($DHCPServer in $DHCPServers){
$OSInfo = Get-CIMInstance -ComputerName $DHCPServer.DnsName -ClassName Win32_OperatingSystem
[pscustomobject]#{
ServerName = $DHCPServer.DnsName;
IPAddress=$DHCPServer.IpAddress;
OS=$OSInfo.Caption
}
}
ServerName IPAddress OS
---------- --------- --
dc2016 192.168.10.1 Microsoft Windows Server 2016 Standard
From there, you can store it in a variable, make it a spreadsheet, do whatever you need to do.
Hope this helps.
If this isn't working, make sure you have enabled PowerShell Remoting first.

How to get only the uptime of the server on Powershell?

I have the following code, It's work when I use directly within powershell:
Get-WmiObject win32_operatingsystem | select #{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)
And it returns:
LastBootUpTime
--------------
14/09/2019 10:41:50
But when I use the command within a Powershell script .ps1 with invoke-command the output returns more informations:
LastBootUpTime PSComputerName RunspaceId
-------------- -------------- ----------
9/14/2019 10:41:50 AM 192.168.0.20 af08d2d8-c4f1-4f85-9d6c-e3f4ffe475c6
Why this happen?
If possible, I'd like without the header LastBootUpTime too.
Invoke-Command will always return additional information, in this case where the command was run and the runspace id. You can always get results into a variable and simply print out the property you want.E.g.
$result = invoke-command {your-command}
$result.LastBootUpTime
or for short
(invoke-command {your-command}).LastBootupTime
Note that when you are using wmi, you do not need to necessarily use invoke-command, you can also directly pass -computer parameter to it to run the command against a remote computer:
Get-WmiObject win32_operatingsystem -computer "remote_computer_name"
Since you're ultimately only interested in the (transformed) property value, there's no need to use Select-Object (whose alias is select) at all - use ForEach-Object instead:
Get-WmiObject Win32_OperatingSystem |
ForEach-Object { $_.ConvertToDateTime($_.LastBootUpTime) }
Note: The extra properties you saw, added by a remote Invoke-Command call with a -ComputerName argument (described below), are still technically present on the result, but they won't display.
That said, the WMI cmdlets were deprecated in PowerShell version 3. Using Get-CimInstance in lieu of Get-WmiObject actually makes the .ConvertToDateTime() call unnecessary (the .LastBootUpTime now directly contains a [datetime] instance), in which case you can simply use Select-Object's -ExpandProperty parameter in order to return the property value only (rather than a [pscustomobject] instance with the requested property):
Get-CimInstance CIM_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime
Note: Get-CimInstance directly supports a -ComputerName argument, so you don't need Invoke-Command -ComputerName for the invocation; unlike the firewall-unfriendly DCOM protocol that the WMI cmdlets use, the CIM cmdlets use the same, firewall-friendly transport as PowerShell remoting.
Or, more succinctly and efficiently, especially in a case such as this where the command returns only a single object, use direct property access:
(Get-CimInstance CIM_OperatingSystem).LastBootUpTime
This answer contrasts the pros and cons of these two approaches and shows other alternatives.
As for what you tried, which generally relates to:
Managing the origin properties automatically added by remote operations:
In remoting scenarios, PowerShell decorates the objects returned with additional properties that provide origin information. These properties are (mostly) of type NoteProperty and are added:
when PowerShell remoting is involved - such as via Invoke-Command -ComputerName in your case.
when CIM cmdlets such as Get-CimInstance are directly used remotely, such as with the -ComputerName parameter.
These properties are:
.PSComputerName (the name of the remote computer on which the code was executed)
Note: On objects returned from remote CIM calls, .PSComputerName appears as a regular property (type Property), not a NoteProperty.
The associated hidden .PSShowComputerName property, which defaults to $true, which explains why you saw a PSComputerName column in the display output.
If you capture the objects before printing them to the screen, you can set the property to $false on them, in which case their .PSComputerName property won't show (but will still be there) - however, the .RunspaceId property may - situationally - still show, and would have to be explicitly excluded - see below.
PowerShell remoting only (not remote CIM calls): .RunspaceId (the ID of the remote runspace)
To exclude these from local display / presence on the object, use the following techniques:
If you're only interested in select properties, make the Select-Object call locally, which, by virtue of locally constructing new [pscustomobject] instances with the properties of interest only, implicitly excludes the remoting-added properties:
Invoke-Command -ComputerName ... { ... } |
Select-Object Name, LastBootUpTime # LOCAL call to Select-Object
If you're interested in all properties except the remoting-added ones, use Select-Object -ExcludeProperty to eliminate them explicitly:
# Get remote output, then locally exclude the remoting-added properties.
Invoke-Command -ComputerName ... { ... } |
Select-Object * -ExcludeProperty PSComputerName, PSShowComputerName, RunSpaceId
Note: Select-Object generally returns [pscustomobject] instances whose properties are static copies of the input objects and which lack the input type's methods.
I found one way! if someone to need here is:
Get-WmiObject win32_operatingsystem | select #{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}|Select-Object -ExpandProperty lastbootuptime
Here is how I used (I'm creating a report in HTML for my database)
write-output "<p> Horario do Ultimo boot: $(Get-WmiObject win32_operatingsystem | select #{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}|Select-Object -ExpandProperty lastbootuptime)</p>"
The output was (in my language and region):
Horario do Ultimo boot: 09/14/2019 10:41:50

Register this connection's addresses in DNS in Windows 10

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

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

Resources