Retrieve value from command output

Multi tool use
Retrieve value from command output
I'm looking for a method which would allow me to retrieve a value from a command output. An example might be retrieving the IP address from an ipconfig
output, or the encryption status from a manage-bde -status
output. It's essentially the value after the colon ":". I tried playing with the findstr options, but I could not retrieve "just" the value. In the ipconfig example, I want only the IP address, and in the "manage-bde" example, I want only the word "protection off". The above two commands are only examples.
ipconfig
manage-bde -status
In addition, I need to figure out a method to perform a foreach
in the event there are more than one IP/Nic, or more than one volume.
foreach
I could be either just one command, or output the results to a file, and then run something against the file.
1 Answer
1
I have created a quick example of something you might be able to do for the ipconfig
example.
ipconfig
Essentially I am retrieving all of the IP Configurations for my local machine, then if their is greater than 1 it will loop through each of these and create an object that's more readable.
#Get IP Configurations
$ipList = Get-NetIPConfiguration
#If there is more than 1 item in the ipList
If($ipList.count -gt 1){
#Loop through each of the IPs int the list
foreach($entry in $ipList){
#Create a Custom PowerShell Object and Assign empty attributes for the Name and IP
$object = new-object psobject | select-object Name, IPAddress
#Add a value to the $object.Name property
$object.Name = $entry.InterfaceAlias
#Add a value to the $object.IPAddress property
$object.IPAddress = $entry | select -ExpandProperty IPv4Address
#Store the value of each object in an array
[array]$output += $object
}
}
#Output the contents of the Array into a file
$output | export-csv c:tempIP_Info.csv -NoTypeInformation
Now there is more you could do to this, It currently outputs System.Object
if there is no IP Value found, it also only looks for an IPv4Address
System.Object
Hopefully this helps you start to find a solution
A way shorter select with calculated properties to do the (unneccessary) rename
Get-NetIPConfiguration|select @{n='Name';e={$_.Interfacealias}},@{n='IPAddress';e={$_.IPv4Address}}
yields the same.– LotPings
Jul 2 at 7:24
Get-NetIPConfiguration|select @{n='Name';e={$_.Interfacealias}},@{n='IPAddress';e={$_.IPv4Address}}
@LotPings lots of super short ways to do it, usually I am very verbose on these responses so that the people who ask the question get a better understanding :)
– Lachie White
Jul 2 at 7:26
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The answer to the basic question here is assign it to a variable. However you are looking to parse from that output. Without a specific example you are just going to get the answer of regex.
– Matt
Jul 1 at 23:59