从命令输出中检索值

时间:2018-07-01 23:55:34

标签: shell powershell

我正在寻找一种方法,该方法允许我从命令输出中检索值。一个示例可能是从ipconfig输出中检索IP地址,或者从manage-bde -status输出中检索加密状态。本质上是冒号“:” 之后的值。我尝试使用findstr选项,但无法“仅”获取值。在ipconfig示例中,我只需要IP地址,而在“ manage-bde”示例中,我只需要单词“ protection off”。以上两个命令仅是示例。

此外,我需要找出一种方法来在有多个IP / Nic或多个卷的情况下执行foreach

我可以只是一个命令,也可以将结果输出到文件中,然后对文件运行某些内容。

1 个答案:

答案 0 :(得分:0)

我已经为#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:\temp\IP_Info.csv -NoTypeInformation 示例创建了一个简单的示例示例。

基本上,我正在检索本地计算机的所有IP配置,然后,如果它们大于1,它将遍历每个IP配置并创建一个更具可读性的对象。

System.Object

现在您可以做更多的事情,如果没有找到IP值,它当前会输出{{1}},它也只会查找IPv4Address

希望这可以帮助您开始寻找解决方案

相关问题