In the following code, the $ipAddress stores both the IPV4 and IPV6. I only want the IPV4 displayed, is there anyway this can be done? Maybe with a split?
Also, the subnet mask prints 255.255.255.0 64 - where is this rogue 64 coming from?
Code:
ForEach($NIC in $env:computername) {
$intIndex = 1
$NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
$caption = $NICInfo.Description
$ipAddress = $NICInfo.IPAddress
$ipSubnet = $NICInfo.IpSubnet
$ipGateWay = $NICInfo.DefaultIPGateway
$macAddress = $NICInfo.MACAddress
Write-Host "Interface Name: $caption"
Write-Host "IP Addresses: $ipAddress"
Write-Host "Subnet Mask: $ipSubnet"
Write-Host "Default Gateway: $ipGateway"
Write-Host "MAC: $macAddress"
$intIndex += 1
}
Subnets work differently for IPv6, so the rogue 64 you are seeing is the IPv6's subnet mask - not the IPv4's.
The prefix-length in IPv6 is the equivalent of the subnet mask in IPv4. However, rather than being expressed in 4 octets like it is in IPv4, it is expressed as an integer between 1-128. For example: 2001:db8:abcd:0012::0/64
See here: http://publib.boulder.ibm.com/infocenter/ts3500tl/v1r0/index.jsp?topic=%2Fcom.ibm.storage.ts3500.doc%2Fopg_3584_IPv4_IPv6_prefix_subnet_mask.html
In order to remove it you can try the following (massive assumption made that IPv4 always comes first, but in all my experimenting it hasn't come second yet ;))
ForEach($NIC in $env:computername) {
$intIndex = 1
$NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
$caption = $NICInfo.Description
#Only interested in the first IP Address - the IPv4 Address
$ipAddress = $NICInfo.IPAddress[0]
#Only interested in the first IP Subnet - the IPv4 Subnet
$ipSubnet = $NICInfo.IpSubnet[0]
$ipGateWay = $NICInfo.DefaultIPGateway
$macAddress = $NICInfo.MACAddress
Write-Host "Interface Name: $caption"
Write-Host "IP Addresses: $ipAddress"
Write-Host "Subnet Mask: $ipSubnet"
Write-Host "Default Gateway: $ipGateway"
Write-Host "MAC: $macAddress"
$intIndex += 1
}
Hope this helps!
Related
I pinged the servers and it is working (it shows me the ip address that could be pinged) but I want it to show the hostname and the ip address.
I tried to incorporate [System.Net.Data.Dns]::GetHostName(), but I don't know where to put it. I am a beginner using PowerShell. I also tried to used -and, but it doesn't work.
I understand how to do it python I just don't know how to translate it to PowerShell.
$columnC = "n1-1mon-i3fp04","n1-1mon-i3fp06","n1-1mon-i3fp07","n1-r-1mon-i3fp09","n1-r-1mon-i3fp10","n1-1mon-i3fp08","n1-1mon-i3fp03","n1-1mon-i3fp02","n1-1mon-i3fp111"
$columnC | % $_ {$Device = Resolve-DnsName -Name $_
$Device.[System.Net.Data.Dns]::GetHostName()
if (test-connection $Device.("IPAddress")) {write-host Device.("IPAddress") "Ping succeeded." -foreground green}
else {write-host $Device.("IPAddress") "Ping failed." -foreground red}}
The result shows an error message like the syntax is wrong. I want it to show both ip address and the hostname.
[edit - Theo pointed out that GetHostByName has been deprecated in favor of GetHostEntry. when i tested that, it gave more consistent results, so i swapped them.]
this will get the ComputerName, HostName, and Online status. then save those into a custom object, send the object to the $Results collection, and - finally - show what is in the collection. [grin]
# fake reading in a text file
# in real life use Get-Content
$ComputerList = #'
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
'# -split [environment]::NewLine
$Results = foreach ($CL_Item in $ComputerList)
{
try
{
$HostName = [System.Net.Dns]::GetHostEntry($CL_Item).HostName
}
catch
{
$HostName = '__Not Found__'
}
[PSCustomObject]#{
ComputerName = $CL_Item
HostName = $HostName
Online = Test-Connection -ComputerName $CL_Item -Count 1 -Quiet
}
}
$Results
output ...
ComputerName HostName Online
------------ -------- ------
BetterNotBeThere __Not Found__ False
LocalHost [MySysName] True
10.0.0.1 __Not Found__ False
127.0.0.1 [MySysName] True
I am using RenewDHCPLease() to renew the IP address of the system.
What is the exact difference between RenewDHCPLease() and ipconfig /renew?
Which one is better to use in Powershell?
Is it necessary to use ReleaseDHCPLease() before RenewDHCPLease()? If so why?
Below is my code:
try {
$ips = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} -ErrorAction Stop
if (!$ips) {
Write-Output "`nDHCP is not Enabled on this system"
Exit
}
foreach ($ip in $ips) {
Write-Output "`nRenewing IP Addresses"
$ip.RenewDHCPLease() | Out-Null
if ($?) {
Write-Output "IP address has been renewed for this system"
}
else {
Write-Error "IP Address Could not be renewed"
}
}
}
catch {
$_.Exception.Message
}
ReleaseDHCPLease releases the IP address bound to a specific DHCP enabled network adapter
RenewDHCPLease Renews the IP address on a specific DHCP enabled network adapter
There is no need to use ReleaseDHCPLease before RenewDHCPLease since the old address is released and a new one is automatically acquired when using RenewDHCPLease
Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' | Foreach-Object{
$_.RenewDHCPLease()
}
You could also use the RenewDHCPLeaseAll method to renew them all at once. Hope this helps :)
I want to incorporate a dropdown menu that is populated with the list of available COM ports. I can't find any way to easily get the names of the available COM ports to put in the place of COM4 that creates the $port.
$port = new-Object System.IO.Ports.SerialPort COM4,19200,None,8,one
By using Win32_SerialPort I am able to easily extract COM1 and COM3.
Get-WmiObject Win32_SerialPort | Select-Object deviceid
Results:
deviceid
COM3
COM1
But my device manager shows 16 available ports from a remote serial hub.
Device Manager Snapshot
Here is what I have tried and I am able to narrow down the Name, but can't figure out how to extract just the (COM--) part.
Get-WmiObject Win32_pnpentity -Filter "Name LIKE 'devicemaster port%'" | Select-Object -Property Name
Result Screenshot
Adding a late answer because I just had a need for this...
You can use WMI ClassGuids to get the exact list (COM and LPT) that device manager shows:
$lptAndCom = '{4d36e978-e325-11ce-bfc1-08002be10318}'
get-wmiobject -Class win32_pnpentity | where ClassGuid -eq $lptAndCom | select name
Confirmed to work with a few LPT / COM extension cards (Brain Boxes / Exar), using Windows 8.1 up to server 2019 (Powershell 4 onwards).
The full list of ClassGuids is here:
https://learn.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors
Here is a more up-to-date solution to get the COM-port details:
cls
$portList = get-pnpdevice -class Ports -ea 0
$portCount = 0
if ($portList) {
$now = get-date
foreach($device in $portList) {
$id = $device.InstanceId
if ($device.Present) {
$date = $now
} else {
$info = Get-PnpDeviceProperty -InstanceId $id
$latest = $info | ?{$_.KeyName -eq "DEVPKEY_Device_LastRemovalDate"}
$date = [datetime]$latest.Data
}
$age = $now-$date
if ($age.Days -lt 14) {
"port name : $name"
"last active: $date"
""
$portCount++
}
}
}
"number of active COM-port devices in last 14 days: $portCount"
Leaving some work to you to figure out, but based on your Result Screenshot you can do something like this:
$ports = #()
$ports += 'devicemaster port (COM1)'
$ports += 'devicemaster port (COM2)'
$ports += 'devicemaster port (COM3)'
$ports += 'devicemaster port (COM4)'
$ports | % {
if ($_ -match "devicemaster port \((.*)\)") {
$matches[1]
}
}
with that object, assuming you store that in '$ports'. You may need to use '$ports.Name'...
Possibly see regex101.com for how the regex works.
I'm running a DHCP server in Windows Server 2012 R2 with an active scope and I've made a script which asks the user for a MAC address and will then reserve an available ip from the scope to the user's MAC.
The actual reservation works without a problem but I've introduced an IF ELSE statement, with the hope that the conditional expression is evaluated as TRUE when a reservation is successful and to FALSE otherwise.
But the expression always evaluates to FALSE because storing the execution of the cmdlet Get-DhcpServerv4Reservation into a variable and then printing it, yields a really messed up output: basically it will print "DHCPServerv4Reservation" as many times as reservations present in the scope, instead of the output given when directly executing the cmdlet.
Here's the code:
clear-host
$mac=read-host -prompt "Please type the MAC address for the host "
$freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0
Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip
$reservedips=Get-DhcpServerv4Reservation -ScopeId 10.10.10.0
if ($reservedips -match $freeip) {
write-host "The ip $freeip has been succesfully reserved for the host with the MAC address $mac"
}
else {write-host "I'm sorry but there are no free ip addresses to be reserved"}
# this is just to see what's inside $reservedips
write-host $reservedips
Why is this happening? Thanks
The result of Get-DhcpServerv4Reservation is a reservation object.
The -match operator is for matching a string against a regular expression.
You might be looking to something like:
if (($reservedips | Where-Object { $_.IPAddress -eq $freeip })) {
# success
}
I think you consider restructuring your code to look something like this:
$mac=read-host -prompt "Please type the MAC address for the host "
$freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0
try {
Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip -ErrorAction Stop
} catch {
Write-Host "An error has occurred while trying to add a reservation for '$mac' with IP '$freeip'."
}
Adding -ErrorAction Stop forces all exceptions to be caught by the try/catch block.
This is the alternative I have come up with, which works
clear-host
$mac=read-host -prompt "Please type the MAC address for the host "
$freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0
Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip
$reservedips=Get-DhcpServerv4Reservation -ScopeId 10.10.10.0 | Where-Object ipaddress -eq $freeip
if ($reservedips -eq "") {write-host "I'm sorry but there are no free ip addresses to be reserved"}
else {write-host "The ip $freeip has been succesfully reserved for the host with the MAC address $mac"}
I am creating a script which will run through a lot of the basic tasks when commissioning a new server. Now most servers have multiple NICs these days and I need to question the user (using the script) what NIC they want to assign the IP to.
At the moment I have:
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
Which will put the NICs into $NICs I believe. However from here I want to print them out to the user, and then somehow assign a value to each NIC, like an index, so the user can type in, "1" or "2" so tell the script what NIC to apply the Ip configuration to which will be done like:
If($ipQuestion -eq "Y") {
$ipAddr = Read-Host "Enter IP Address: "
$subnet = Read-Host "Enter Subnet: "
$dns = Read-Host "Enter DNS: "
Write-Host "Multiple DNS servers?" -ForegroundColor Green
$multipleDNSServersQuestion = Read-Host
If($multipleDNSServersQuestion -eq 'Y'){
$dns2 = Read-Host "Enter Secondary DNS: "
}
$dGateway = Read-Host "Enter Default Gateway: "
}
$NIC.EnableStatic($ipAddr, $subnet)
$NIC.SetGateways($dGateway)
$NIC.SetDNSServerSearchOrder($dns, $dns2)
$NIC.SetDynamicDNSRegistration("TRUE")
Any ideas?
If you ensure $NICS is always an array, you can use the array index to specify each NIC. To ensure it is always an array do this:
$NICs = #(Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true})
Then print out the info like so:
PS> $NICS = #(Get-WMIObject Win32_NetworkAdapterConfiguration)
PS> $NICS | Foreach {$i=-1}{$i++;$_} | ft #{n='index';e={$i}},Description,ServiceName
index Description ServiceName
----- ----------- -----------
0 WAN Miniport (L2TP) Rasl2tp
1 WAN Miniport (SSTP) RasSstp
2 WAN Miniport (IKEv2) RasAgileVpn
3 WAN Miniport (PPTP) PptpMiniport
4 WAN Miniport (PPPOE) RasPppoe
5 WAN Miniport (IP) NdisWan
6 WAN Miniport (IPv6) NdisWan
7 WAN Miniport (Network Monitor) NdisWan
8 Microsoft Kernel Debug Network Adapter kdnic
9 RAS Async Adapter AsyncMac
10 Broadcom NetXtreme Gigabit Ethernet b57nd60a
11 Microsoft ISATAP Adapter tunnel
12 Microsoft Teredo Tunneling Adapter tunnel
13 Microsoft 6to4 Adapter tunnel
14 Microsoft ISATAP Adapter tunnel
Then access each NIC like so:
$NICS[$selectedIndex]
$NICs = #(Get-WMIObject Win32_NetworkAdapterConfiguration ...)
will make $NICs an array, which can be accessed by (zero-based) index:
$NICs[0] # <-- first interface
$NICs[1] # <-- second interface
...
The way I would do it. If you have a look to the network connexions panel in the internet connexions. You can see the string the user know for his devices :
So in a dialog with the user I would give this name retreiving it with win32_NetworkAdapter joinning Win32_NetworkAdapterConfiguration with Index.
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
$NICs | % {$i = (Get-WmiObject win32_NetworkAdapter -Filter "index=$($_.index)").NetConnectionID; $_} | ft #
{n='index';e={$i}},Description,ServiceName
index Description ServiceName
----- ----------- -----------
NET1 Intel(R) 82567LM Gigabit Network Conne... e1yexpress