How to get the property name set to false value - powershell

I am still new to powershell. I have been trying to get only the Nic properties that have been set to false.
So when I run:
Get-NetAdapter | fl * it brings up all the results for each property of the Nic. I only want to see the properties that have been set to false.
I have tried Get-NetAdapter | where {$_ -like $false} and Get-NetAdapter | where {$_ -like "*false*"}. Have also tried sorting but also cant get working
Much Appreciated

(Get-NetAdapter).PSObject.Properties | Where-Object Value -eq $false | Select-Object Name
If you want the results as string array instead of an object array, use Select-Object -ExpandProperty Name
Note: This only works as intended if you have a single NetAdapter. The code in the comment from PetSerAl will give you all properties from all adapters, but you don't see from which adapter.
If you want to test multiple adapters, you should write an additional loop for example like this
$adapters = Get-NetAdapter
foreach ($adapter in $adapters)
{
Write-Host -ForegroundColor Green "$($adapter.Name)"
$adapter.PSObject.Properties | Where-Object Value -eq $false | Select-Object -ExpandProperty Name
Write-Host ""
}

Related

PowerShell - Use a string in a foreach-object against the Description AD property of a computer object

I am trying to send a user name (SamAccountName) down the PowerShell Pipeline to find a computer based on the Description property in Active Directory:
The Description property is always "something-UserName"
I know I don't need to send the variable down the pipeline and can simply express it in the filter but I have s specific use case where I need to do this.
This is what I have tried:
"bloggsJ" | %{Get-ADComputer -server domain.com -Filter * -Properties Description | ?{$_.Description -eq "something-$_"}} | select Name
This produces nothing even though there is a computer with a description property of "Something-bloggsJ" on that domain.
Any advice please.
Instead of using the -eq operator, I would use -like.
Something like this:
"bloggsJ", "IanB" | ForEach-Object {
$name = $_
Get-ADComputer -Filter * -Properties Description |
Where-Object {$_.Description -like "*-$name"}
} | Select-Object Name
Inside the ForEach-Object loop, the $_ automatic variable is one of the usernames. Inside the Where-Object clause, this $_ variable represents one ADComputer object, so in order to have the username to create the -like string, you need to capture that name before entering the Where-Object clause.
I believe you are missing the underscore for $_ variable:
"ivan" | ForEach-Object -Process { Get-ADComputer -Filter * -properties description | Where-Object -Property description -eq "something-$_"}
this one is working ...

Get-WmiObject Win32_NetworkAdapterConfiguration -Match failing

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.

Want to put IP address v4 of my specifically named adapter into a variable using Powershell

I have several network adapters on my PC.
I want to simply get the IP Address v4 (with no headers or extras) of the adapter called a specific name and store it into a variable for further use.
I am able to return the IP Address with headers but not what I want.
Please Help - thanks.
Here is what I tried:
$ipa = Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } |
Select-Object { $_.IPAddress }
Edit:
$ipa = Get-NetIPAddress | where {$_.InterfaceAlias -eq "LAN2" -and
$_.AddressFamily -eq "IPv4"} | select -expandproperty ipaddress
The above code is returning both my wired adaptors' addresses but it is returning just the Ip address at least and nothing else (thanks Anthony)
There is ONLY one called "LAN2" - i only need that one - so still stuck
Update :
Austin Frenchs second solution works great - will test Owls today later - thanks everyone so far - great helpful community
simple one liner
(Get-NetIPConfiguration).ipv4address | where {$_.InterfaceAlias -eq "Lan2"} | foreach { write-host $_.ipaddress}
get from enabled network
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | select Ipaddress
only from particular adapter and ipv4
Get-WMIObject win32_NetworkAdapterConfiguration |
Where-Object { $_.IPEnabled -eq $true } |
Foreach-Object { $_.IPAddress } |
Foreach-Object { [IPAddress]$_ } |
Where-Object { $_.AddressFamily -eq 'LAN2' } |
Foreach-Object { $_.IPAddressToString }
Without access to get-netipaddress, here is a very dirty and long one liner. Change out "Local Area Connection" for your network's name.
((([System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? {$_.Name -eq "Local Area Connection"}).GetIPProperties() | Select-Object -ExpandProperty 'UniCastAddresses' | ? {$_.IPv4Mask -ne
"0.0.0.0"}) | Select Address).Address.IPAddressToString
With Get-netIpAddress, I suspect this would also work:
$ipa = (Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } | `
Select-Object { $_.IPAddress }).IPAddress
You can accomplish what you are looking for by first getting a list of all network adapters on the machine using get-wmiobject, selecting the name you want, then piping it to get-netipaddress and then selecting the IPAddress property as shown below.
(Get-WmiObject win32_networkadapter | Where-Object {$_.name -eq "YOUR_NAME"} | Get-NetIPAddress).IPAddress

Powershell Select-Object Formatting

$output = $data | Where-Object {$_.Name -eq "$serverName"} | Select-Object -Property Description1,Version | Where-Object {$_.Description1 -eq "Power controller Firmware"} | Select-Object -Property Version
Write-Host $output
Gives me the following output:
#{Version=3.4}
So $data is an array and I select what I want form it and assign it to a variable to eventually be inputted into a excel file but no matter what I seem to try I cant just select "3.4" Instead it selects like the above (#{Version=3.4}). Doesn't anybody know how to just select the "3.4" within my command?
Just replace last line with:
foreach( $out in $output )
{
Write-Host $out.Version
}
In fact your $output variable contains an array so you need to go through it with a foreach loop.
Then you can Write-Host or do anything with the Version property.
As stated by #okaram, if you want to make the same kind of looping but after a pipe you can do it this way:
$output | ForEach-Object {Write-Host $_.Version}
or
$output | %{Write-Host $_.Version}
Your last expression of
Select-Object -Property Version
Keeps the entire object in the pipeline, but filters down the properties to only Version. However, the -ExpandProperty will put the property value itself in the pipeline.
Select-Object -ExpandProperty Version
That should return the "3.4" result you expect.
Please try the following code:
$data | Where-Object {$_.Name -eq "$serverName"} | Select-Object -Property
Description1, Version | Where-Object {$_.Description1 -eq "Power controller
Firmware"} | write-Host $_.Version

PowerShell: How can I list the index of an item using format-table?

I wish to list the index of an item in a result table.
In this case I am using get-aduser with the filter option.
I am then using format-table with -Property to display only the properties I want.
I previously used a loop to display the items along with a counter to emulate the index but this was messy and I wanted it in a table.
Code:
$search_param = read-host "Enter search string: "
$search_param = "*" + $search_param + "*" # Construct search param.
$search_result = Get-ADUser -Filter {Surname -like $search_param -or GivenName -like $search_param -or SamAccountName -like $search_param}
$search_result | format-table -Property GivenName,Surname,SamAccountName
How can I get format-table to display the item index/position without using some kind of loop? ie, is there some kind of 'hidden' index property that I can simply provide to format-table?
The format-table CmdLet -Property param can be a new calculated property see Format-table help.
Here is an example with a computed index on Get-Process objects.
$index=0
Get-Process | Format-Table -Property #{name="index";expression={$global:index;$global:index+=1}},name,handles
The ForEach-Object cmdlet can be used to create a calculated "index" property for the Format-Table cmdlet
For Example:
$search_result | ForEach-Object {$index=0} {$_; $index++} | Format-Table -Property #{ Label="index";Expression={$index}; Width=5 },GivenName,Surname,SamAccountName
Additional References:
Adding Array Index Numbers to PowerShell Custom Objects
ForEach-Object
Format-Table
In my opinion you can't, you need to extend the objects with an Index property and include it in your Format-Table command.
$procs = Get-Process
#option 1
$procs | Select #{N='Index'; E={$procs.IndexOf($_)}}, name, handles
#option 2
$procs | Format-Table -Property #{name="index";expression={$procs.IndexOf($_)}},name,handles