I am very new to powershell and trying to write a script where I BIND my Websites and FTP sites on a server to the Ip addresses present on the specific adapter of a multihome server.
The server has two adapters: Primary and Secondary
I am using the following command:
$AvailableIPs = #( Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $Null } | Select-Object -ExpandProperty IPAddress )
and this gets me all the IP addresses on the machine from primary as well as secondary.
I need to get the list if IP address only from the secondary adapter.
The situation that the Name of the secondary adapter will not be constant but the name of primary adapter will be constant so I would need to do a -ve match on the name of primary adapter.
How can I put two -ve matches in the same command?
Something like this does not work:
$AvailableIPs = #( Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $Null } | Where-Object { $_.Description -ne "<Primary Adapter Name>"} | Select-Object -ExpandProperty IPAddress )
What would be the syntax for getting the list from
Win32_NetworkAdapterConfiguration Class which is not Null and which does not have the description of "Some name"?
Change the "sometext" to whatever you want...
$AvailableIPs = Get-WmiObject Win32_NetworkAdapterConfiguration |
? {$_.IPEnabled -and $_.Description -notmatch "sometext"} |
Select -ExpandProperty IPAddress
You don't need to use more then one Where-Object (? Alias) Clause, you can join them like in the above example,
Also you you can change the operator from -notmatch to -ne (Not Equal) if you have constant specific name
Related
relatively new to Powershell.
I'm trying to output the Description and IPAddress values from the following code, without it outputing the IPV6 addresses with it.
I want the Description and the IPAddress values, I've managed to get it to output just IPV4 by doing Select -Expand IPAddress and a Like command, but I want the description to, when I try to add Description via select it just breaks the entire thing.
Below is as close as I've come, shows me Description + IP Address, but I want to hide the IPV6 to keep it neat.
gwmi Win32_NetworkAdapterConfiguration -computername $endpointip |
Where { $_.IPAddress -notlike '*::*' } | Where {$_.Description -like '*GbE*' -or $_.Description -like '*Ethernet*' -or $_.Description -like '*Wi-Fi*'-or $_.Description -like '*Wireless*' -or $_.Description -like '*Cisco*'}| # filter the objects where an address actually exists
Select Description, IPAddress| Out-String # retrieve only the property *value*
Also tried it
$_.IPAddress -like '*.*.*'
However that just outputs like the below, I don't want the IPV6 Addresses
`Description IPAddress
----------- ---------
Intel(R) Wireless-AC 9560 160MHz {192.168.0.16, fe80::50d0:fda6:44ee:237d}
Realtek PCIe GbE Family Controller
Microsoft Wi-Fi Direct Virtual Adapter
Microsoft Wi-Fi Direct Virtual Adapter
Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64 {x.x.x.x, xxxx::xxxx:xxxxx:xxxx, xxxx::xxxx:xxxx:xxxx:xxxx}
Realtek USB GbE Family Controller `
Or like this
Description IPAddress
----------- ---------
Intel(R) Wireless-AC 9560 160MHz {192.168.0.16, fe80::50d0:fda6:44ee:237d}
Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64 {xx.xx.xx.xx, xxxx::xxxx:xxxxx:xxxx, xxxx::xxxx:xxxx:xxxx:xxxx}
Any help would be appreciated, thanks
What you're trying to filter by is working as it's suppose to, in regard to how PowerShell "thinks". *.*.* will get the objects that have a value in IPAddress matching *.*.*, but it's not excluding the other values. The immediate fix would be using a calculated property to extract that IP address:
gwmi Win32_NetworkAdapterConfiguration -computername $endpointip |
Where {$_.Description -like '*GbE*' -or $_.Description -like '*Ethernet*' -or $_.Description -like '*Wi-Fi*'-or $_.Description -like '*Wireless*' -or $_.Description -like '*Cisco*'} |
Select Description, #{
Name = 'IPAddress'
Expression = { $_.IPAddress | Select -First 1}
}
Here, you tell it to select just the first object value in IPAddress which is almost always the IPv4 Address.
You can also use a filter against it to ensure that IP Address is matched using $_.IPAddress | ? { $_ -like '*.*' } instead.
Without knowing your PS version, Get-CimInstance superseded Get-WMIObject as of PS 3.0+ where it's not supported in PS core either.
So I am using this:
$IPA = (Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName").IPv4Address
and then I want to use the following to grab the Subnet Mask for that IP:
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match $IPA)
Followed by:
$SubMask = $IPInfo.IPSubnet[0]
But this fails with $IPInfo being blank. If I hard code the IPAddress it works:
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match 10.45.22.100)
But the port IP will always be different. Why is it not taking the $IPA as a match parameter?
EDIT: The IP Address being reported has both the IPv4 and IPv6 which is why I am trying to do -Match against the IPv4.
As we have discovered in comments you are getting an array returned for your $IPA. It looks fine in console as PowerShell unrolls the array to display all elements. Since there is only one it was misleading.
((Get-NetIPAddress | Where-Object InterfaceAlias -eq "Local Area Connection").IPv4Address).gettype().fullname
System.Object[]
Likely it was trying to match "System.Object[]" which is why you did not get the result you wanted.
Few ways around this. A simple one would be to always return the -First result in your query.
(Get-NetIPAddress | Where-Object InterfaceAlias -eq "Local Area Connection").IPv4Address | select -first 1).gettype().fullname
So just use | select -first 1 and you should get the results you expect.
I caution the use of -match here. Understand that -match and -replace support regex pattern strings. So if you have regex meta characters in your strings you can get unexpected results.
This happens because $IPA is really an array of objects.
When you run Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName" it returns an array of CIM instances of type MSFT_NetIPAddress.
When you ask for IPv4Address member by running $IPA = (Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName").IPv4Address the array is still there but the elements of the array that don't have IPv4Address are not shown.
Take a look at the following example.
$NetIPAddresses = (Get-NetIPAddress | where {$_.InterfaceAlias -eq "Ethernet" -and $_.AddressFamily -eq "IPv4"})
foreach ($NetIPAddress in $NetIPAddresses) {
$IPA = $NetIPAddress.IPv4Address
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match $IPA)
$IPInfo.IPSubnet[0]
}
This will show subnet mask for each IPv4 address on the given interface.
I am working on a script to get the IP address information from several computers using the ForEach statement. Problem is that the IP address string returns extra info, so in order to get rid of that info, I run this...
Get-WmiObject win32_networkadapterconfiguration |
where { $_.ipaddress -like "1*" } |
select -ExpandProperty ipaddress |
select -First 1}
Which is fine for one computer but when I add it to the whole ForEach part of my script below, it returns the IP Address for my machine attached to all machine queried in the script.
Get-Content $MultiActivePing | foreach {
Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
where { $_.ipaddress -ne $null} |
select #{Expression={$_.__SErver};Label="Computer"}
,#{Expression={Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1};Label="IPAddress"} `
,#{Expression={$_.DefaultIPGateway};Label="Gateway"}
}
So I was wondering if there was a way to expand the property of the IP address without having to run another GWMI inside the SELECT IP address portion.
Admitting I am a little confused since I don't know what you are expecting exactly however I can venture a guess. Mostly I'm not sure what you want the IP Address output to look like.
Get-Content $MultiActivePing | ForEach-Object {
# Save the current computer name so we can use down the line.
$computer = $_
Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
where { $_.ipaddress -ne $null} |
Select #{l="IPAddress";e={$_.ipaddress | Select -First 1}},#{l="Computer";e={$computer}},DHcpenabled,macaddress
}
The part about this that will vary depending on what you are trying to accomplish:
Select #{l="IPAddress";e={$_.ipaddress | Select -First 1}}
You already know the IP so there is no need to acquire it again. This takes the first IP in the array and outputs it alone. This is the part I think you are having issues with but I don't know how you choose what information to drop. The caveat obviously is that you could lose IP information that you actually want depending on the order it appears. I would suggest this then as well so nothing is lost:
Change the calculated property for IPAddress highlighted above to this:
#{l="IPAddresses";e={$_.ipaddress -Join ";"}}
That would take all IP's and convert them into a semicolon delimited string
So thanks to Matt's logic, I just added this to the script...
,#{E={$_.ipaddress | Select -First 1};L="IPAddress"}
Here's what it looks like now, something so simple. Took care of my issue...
ForEach {
Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
where { $_.ipaddress -ne $null} |
Select #{E={$_.__SErver};L="Computer"}
,#{E={$_.ipaddress | Select -First 1};L="IPAddress"}
,#{E={$_.DefaultIPGateway | Select -First 1};L="Gateway"}
I want to write a PowerShell script to get all the print servers in a network.
I have used the following LDAP query, but it returns only servers with network printers attached to it. But not other print servers that have a remote printer attached to it.
Here's the code I used to get the print servers (But getting only the servers with n/w printers)
Import-Module ActiveDirectory
[array]$testarray = Get-ADObject -LDAPFilter "(&(&(&(uncName=*)(objectCategory=printQueue))))" -properties *|Sort-Object -Unique -Property servername |select servername
$testarray
You could try something like Get-WMIObject win32_printer | select name,local wrapped in a foreach-object loop like this:
$servers | ForEach-Object {
Get-WMIObject Win32_Printer -computername $_ | Where-Object {$_.local -like 'False'} | select Name,local,SystemName | format-table -a
}
The $servers can be what scoped to whatever servers you need to check.
Use Get-WMIObject Win32_Printer | select * to see what properties you want to report back on and include them in the | select Name,local,SystemName section of the script
You could use a combination of Get-ADComputer and Get-Printer to list all shared printer queues on computers joined to your domain:
Get-ADComputer -Filter * | % {
$computer = $_.Name
Get-Printer -Computer $computer | ? { $_.Shared } | select -Expand Name
}
This doesn't cover computers that aren't domain members, though.
I would like to use Get-WmiObject Win32_NetworkAdapterConfiguration to return the ip address of a network card. Unfortunately, I cannot figure out how to format the output to display only the IPv.4 address.
Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}
Displays:
IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}
Using the above output as an example, I would like it to only return 192.168.56.1 (Some clients have multiple NIC's, hence the "Where-Object")
The IPAddress property is a string[], so the following should do it:
gwmi Win32_NetworkAdapterConfiguration |
Where { $_.IPAddress } | # filter the objects where an address actually exists
Select -Expand IPAddress | # retrieve only the property *value*
Where { $_ -like '192.168.*' }
Adding a quicker answer (avoiding Where-Object and using -like operation on a list):
#(#(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]
Hope this Helps
Thought I would share my own variation on the above, in case it helps someone. Just one line:
Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1
Cheers.
(Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1).IPAddress[0]
Returns the IP-address of network connection with default gateway.
This is exactly what you need in most cases :)
Compatible with Powershell 2.0 (Windows XP) and newer.
(Get-WmiObject win32_Networkadapterconfiguration | Where-Object{$_.ipaddress -notlike $null}).IPaddress | Select-Object -First 1
Hope that this will help !
(Get-WMIObject -Class Win32_NetworkAdapterConfiguration).IPAddress