Microsoft Virtualization Discussions
Microsoft Virtualization Discussions
Hi,
Can someone please give me tips to format, cut and copy the output generated by PSTK cmdlet Invoke-NaSsh.
The specific requirement what I am looking is to grep few records from the outpt of [Invoke-NaSsh "sp status"]
Thanks
Charan
Solved! See The Solution
Hello Charan,
Using regular expressions you can create a custom object which holds the information. Here is a crude example:
$output = "Service Processor Status: Online Firmware Version: 1.0 Mgmt MAC Address: 00:A0:98:13:9C:22 Ethernet Link: up Using DHCP: yes IPv4 configuration: IP Address: 172.22.131.145 Netmask: 255.255.224.0 Gateway: 172.22.128.1" $spStatus = "" | Select Status,Firmware,MAC,LinkState,UsingDHCP,IP_Address,Netmask,Gateway if ($output -match 'Status:\s*(.*)\b') { $matches $spStatus.Status = $matches[1] } else { $spStatus.Status = "unknown" } if ($output -match 'Firmware Version:\s*(\d*\.\d*)') { $matches $spStatus.Firmware = $matches[1] } else { $spStatus.Firmware = "unknown" } if ($output -match 'Mgmt MAC Address:\s*(.*)\b') { $matches $spStatus.MAC = $matches[1] } else { $spStatus.MAC = "unknown" } if ($output -match 'Ethernet Link:\s*(.*)\b') { $matches $spStatus.LinkState = $matches[1] } else { $spStatus.LinkState = "unknown" } if ($output -match 'Using DHCP:\s*(.*)\b') { $matches $spStatus.UsingDHCP = $matches[1] } else { $spStatus.UsingDHCP = "unknown" } if ($output -match 'IP Address:\s*(.*)\b') { $matches $spStatus.IP_Address = $matches[1] } else { $spStatus.IP_Address = "unknown" } if ($output -match 'Netmask:\s*(.*)\b') { $matches $spStatus.Netmask = $matches[1] } else { $spStatus.Netmask = "unknown" } if ($output -match 'Gateway:\s*(.*)\b') { $matches $spStatus.Gateway = $matches[1] } else { $spStatus.Gateway = "unknown" } $spStatus
Which would output this:
Status : Online Firmware : 1.0 MAC : 00:A0:98:13:9C:22 LinkState : up UsingDHCP : yes IP_Address : 172.22.131.145 Netmask : 255.255.224.0 Gateway : 172.22.128.1
Since it's an object you can address properties during control statements:
if ($spStatus.LinkState -ne "up") { # do something }
Hope that helps,
Andrew
Hello Charan,
Using regular expressions you can create a custom object which holds the information. Here is a crude example:
$output = "Service Processor Status: Online Firmware Version: 1.0 Mgmt MAC Address: 00:A0:98:13:9C:22 Ethernet Link: up Using DHCP: yes IPv4 configuration: IP Address: 172.22.131.145 Netmask: 255.255.224.0 Gateway: 172.22.128.1" $spStatus = "" | Select Status,Firmware,MAC,LinkState,UsingDHCP,IP_Address,Netmask,Gateway if ($output -match 'Status:\s*(.*)\b') { $matches $spStatus.Status = $matches[1] } else { $spStatus.Status = "unknown" } if ($output -match 'Firmware Version:\s*(\d*\.\d*)') { $matches $spStatus.Firmware = $matches[1] } else { $spStatus.Firmware = "unknown" } if ($output -match 'Mgmt MAC Address:\s*(.*)\b') { $matches $spStatus.MAC = $matches[1] } else { $spStatus.MAC = "unknown" } if ($output -match 'Ethernet Link:\s*(.*)\b') { $matches $spStatus.LinkState = $matches[1] } else { $spStatus.LinkState = "unknown" } if ($output -match 'Using DHCP:\s*(.*)\b') { $matches $spStatus.UsingDHCP = $matches[1] } else { $spStatus.UsingDHCP = "unknown" } if ($output -match 'IP Address:\s*(.*)\b') { $matches $spStatus.IP_Address = $matches[1] } else { $spStatus.IP_Address = "unknown" } if ($output -match 'Netmask:\s*(.*)\b') { $matches $spStatus.Netmask = $matches[1] } else { $spStatus.Netmask = "unknown" } if ($output -match 'Gateway:\s*(.*)\b') { $matches $spStatus.Gateway = $matches[1] } else { $spStatus.Gateway = "unknown" } $spStatus
Which would output this:
Status : Online Firmware : 1.0 MAC : 00:A0:98:13:9C:22 LinkState : up UsingDHCP : yes IP_Address : 172.22.131.145 Netmask : 255.255.224.0 Gateway : 172.22.128.1
Since it's an object you can address properties during control statements:
if ($spStatus.LinkState -ne "up") { # do something }
Hope that helps,
Andrew
Many Thanks Andrew; if possible plz explain the regex[$output -match 'Status:\s*(.*)\b'] that you have used as I am new to use regex.
Charan
Regular expressions are used for pattern matching against strings. They can be used multiple ways, the most common being to check if a string contains some pattern, or to extract some text which matches a pattern. In this instance we're doing the latter.
if ($output -match 'Netmask:\s*(.*)\b') { $matches $spStatus.Netmask = $matches[1] } else { $spStatus.Netmask = "unknown" }
The first line checks to see if the string $output matches against the regular expression 'Netmask:\s*(.*)\b'. If it matches it goes into the if statement. Breaking out the regex used, there's a couple of things going on...
# look for the text "Netmask:" (no quotes, but colon required) Netmask: # followed by any number of white space characters, e.g. tab or space # the \s is short for whitespace, the * is short for 0 or more \s* # capture any text after the last white space character # the parenthesis denotes "capture text", the period is short for any character, # and the * is short for any number of occurrences (.*) # stop capturing at the end of the word. \b is short for "word boundary" \b
Assuming it matches the regex, the extracted text will be found in a created array variable, $matches, with the first value ($matches[0]) being equal to the whole of the matched text, and the second and later values ($matches[1], etc.) equal to the extracted values.
There is a huge amount of information on regex out there. There's a decent reference of the special quantifiers and identifiers here. Regex can be complicated, but they've been around for a long time (Perl uses them heavily), so for most things you can search the internet and find an example or two to base yours off of.
Hope that helps.
Andrew
^^
Very nice explanation and tip... Nice output
A quick way I do is
$v = Invoke-nassh "sp status"
$v = $v.split("`n")
$v | ? {$_ -like "*ip*"}