Powershell脚本从命令行输出中捕获部分数据

时间:2018-11-03 18:26:01

标签: powershell

很抱歉,如果标题有点混乱,说实话,我不知道如何用通俗易懂的词来表达或确切地寻找什么。我的命令行输出如下所示。

Remote Copy System Information
Status: Started, Normal

Target Information

Name        ID Type Status Options Policy
3PARSYSTEM1  2 IP   ready  -       mirror_config

Link Information

Target      Node  Address      Status Options
3PARSYSTEM1 0:3:1 xxx.xxx.xxx.xxx Up     -
3PARSYSTEM1 1:3:1 xxx.xxx.xxx.xxx Up     -
receive     0:3:1 receive      Up     -
receive     1:3:1 receive      Up     -

Group Information

Name         Target      Status   Role       Mode     Options
GRP001Temp   3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 00:08:09 MYT, Period 3h,over_per_alert
  LocalVV        ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN001-Temp 13304 LUN001-TempDR 16914 Synced        2018-11-04 00:08:10 MYT

Name              Target      Status   Role       Mode     Options
GRP002-PHY01 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:17:54 MYT, Period 2h,auto_recover,over_per_alert
  LocalVV          ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN001-VVT2.12  120 LUN001-VVT2.12  210 Syncing (33%) 2018-11-03 23:51:04 MYT

Name              Target      Status   Role       Mode     Options
GRP003-PHY02 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:27:12 MYT, Period 1h45m,auto_recover,over_per_alert
  LocalVV          ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN002-VVT2.14  130 LUN002-VVT2.14  207 Syncing (49%) 2018-11-03 23:59:27 MYT

Name              Target      Status   Role       Mode     Options
GRP001-PRD-ORA 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 00:45:09 MYT, Period 2h,auto_recover,over_per_alert
  LocalVV                      ID   RemoteVV                     ID   SyncStatus    LastSyncTime
  ORA-PROD-VG01.35   97 ORA-PROD-VG01.35 2451 Synced        2018-11-04 00:45:54 MYT
  ORA-PROD-VG02.36   98 ORA-PROD-VG02.36 2452 Synced        2018-11-04 00:46:10 MYT
  ORA-PROD-VG03.37   99 ORA-PROD-VG03.37 2453 Synced        2018-11-04 00:45:48 MYT
  ORA-PROD-VG04.38  100 ORA-PROD-VG04.38 2454 Synced        2018-11-04 00:45:12 MYT
  ORA-PROD-VG05.39  101 ORA-PROD-VG05.39 2455 Synced        2018-11-04 00:45:12 MYT

Name              Target      Status   Role       Mode     Options
GRP001-PRD-SAP 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:24:25 MYT, Period 23m,auto_recover,over_per_alert
  LocalVV                 ID   RemoteVV                ID   SyncStatus    LastSyncTime
  SAP-PROD-APPS.4        80 SAP-PROD-APPS.4      1474 Synced        2018-11-04 01:24:28 MYT
  SAP-PROD-LOCK.19       95 SAP-PROD-LOCK.19     1490 Synced        2018-11-04 01:24:25 MYT
  SAP-PROD-SAPDT1.5      81 SAP-PROD-SAPDT1.5    1475 Synced        2018-11-04 01:25:16 MYT
  SAP-PROD-SAPDT2.6      82 SAP-PROD-SAPDT2.6    1476 Synced        2018-11-04 01:25:05 MYT
  SAP-PROD-SAPDT3.7      83 SAP-PROD-SAPDT3.7    1477 Synced        2018-11-04 01:25:07 MYT
  SAP-PROD-SAPDT4.8      84 SAP-PROD-SAPDT4.8    1478 Synced        2018-11-04 01:25:41 MYT
  SAP-PROD-SAPDT5.9      85 SAP-PROD-SAPDT5.9    1479 Synced        2018-11-04 01:25:35 MYT
  SAP-PROD-SAPDT6.10     86 SAP-PROD-SAPDT6.10   1480 Synced        2018-11-04 01:25:56 MYT

Name              Target      Status   Role       Mode     Options
GRP002-PRD-SAP 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:24:55 MYT, Period 23m,over_per_alert
  LocalVV          ID   RemoteVV          ID   SyncStatus    LastSyncTime
  SAP-PROD-VG01.10   15 SAP-PROD-VG01.10 29769 Synced        2018-11-04 01:28:44 MYT

,我想使用Powershell捕获组信息,这样我就可以获取组名并运行另一个命令来遍历组名。示例输出如下。

GRP001Temp
GRP002-PHY01
GRP003-PHY02
GRP001-PRD-ORA
GRP001-PRD-SAP
GRP002-PRD-SAP

希望您能帮助我解决我的问题。预先谢谢你。

3 个答案:

答案 0 :(得分:1)

如果每个组都在“主要”角色中,一种简单的方法可能是以下语句:

get-content Demo.txt | where { $_ -match "Primary" } | % { $_.Split(" ")[0] }  

它获得包含单词“ Primary”的行,并采用第一个单词(在您的情况下为组名)

答案 1 :(得分:0)

您可以按换行符拆分输出,然后遍历结果。 如果一行以Name开头,请用空格将下一行分隔,然后将第一个元素写入输出中。

类似的东西:

param($output)

$lines = $output -split [Environment]::NewLine
$name = $false
foreach($line in $lines) {
    if($name) {
        ($line -split ' ')[0] | Write-Output
        $name = $false
    }
    if($line.Startswith('Name')) {
        $name = $true
    }
}

答案 2 :(得分:0)

很明显,您在标题后面的第一个单词之后

Name         Target      Status   Role       Mode     Options

不包括 false 标头

Name        ID Type Status Options Policy

其他答案也不排除。

因此,我通过此Header将带有RegEx的文件内容分为几部分,并跳过了第一个部分。
然后将每个部分分成个单词,并用空格\s隔开,并获得第一个[0]

(Get-Content .\SO_53154266.txt -raw) -split "(?sm)^Name\s+Target.*?`r?`n" |
  Select-Object -skip 1|
    ForEach-Object { ($_ -split '\s')[0] }

示例输出:

GRP001Temp
GRP002-PHY01
GRP003-PHY02
GRP001-PRD-ORA
GRP001-PRD-SAP
GRP002-PRD-SAP