0

我正在使用 Powershell 脚本来监控 SAN。我使用以下代码成功地从 Powershell 中的系统中提取了一个包含所有值的文本文件:

& "NaviSecCli.exe" -user xxxx -password xxxx -h host -f "C:\LUNstate.txt" lun -list
$Path = "C:\LUNstate.txt"
$Text = "Capacity \(GBs\)"
$Name = "^Name"
Get-Content $Path | Select-String -pattern $Text,$Name

这会生成以下输出:

Name:  TEST-DATASTORE-1
User Capacity (GBs):  1536.000
Consumed Capacity (GBs):  955.112
Name:  CV Snapshot Mountpoint
User Capacity (GBs):  1024.000
Consumed Capacity (GBs):  955.112

现在我可以通过冒号拆分值,将输出放入一个变量中:

$LUNArray = Get-Content $Path | Select-String -pattern $Text,$Name
$LUNArray | foreach {
    $LUNArray = $_ -split ':  '
    Write-Host $LUNArray[0]
    Write-Host $LUNArray[1]
}

唯一有趣的数据存储在 $LUNArray[1] 中,所以我可以省略 Write-Host $LUNArray[0],它会给出以下输出:

TEST-DATASTORE-1
1536.000
955.112
CV Snapshot Mountpoint
1024.000
955.112

现在棘手的部分是,我想将数据放入一个多维数组中。所以我会得到以下数组布局:

LUN                     Usercap    ConsCap
TEST-DATASTORE-1        1536.000   955.112
CV Snapshot Mountpoint  1024.000   955.112

输入文件如下所示:

LOGICAL UNIT NUMBER 201
Name:  TEST-DATASTORE-1
UID:  60:06:E4:E3:11:50:E4:E3:11:20:A4:D0:C6:E4:E3:11
Current Owner:  SP B
Default Owner:  SP B
Allocation Owner:  SP B
User Capacity (Blocks):  3221225472
User Capacity (GBs):  1536.000
Consumed Capacity (Blocks):  2005641216
Consumed Capacity (GBs):  956.364
Pool Name:  Pool HB Hasselt
Raid Type:  Mixed
Offset:  0
Auto-Assign Enabled:  DISABLED
Auto-Trespass Enabled:  DISABLED
Current State:  Ready
Status:  OK(0x0)
Is Faulted:  false
Is Transitioning:  false
Current Operation:  None
Current Operation State:  N/A
Current Operation Status:  N/A
Current Operation Percent Completed:  0
Is Pool LUN:  Yes
Is Thin LUN:  Yes
Is Private:  No
Is Compressed:  No
Tiering Policy:  Lowest Available
Initial Tier:  Lowest Available
Tier Distribution:  
Capacity:  100.00%

LOGICAL UNIT NUMBER 63920
Name:  CV Snapshot Mountpoint
UID:  60:50:38:00:14:50:38:00:C6:64:50:38:00:50:38:00
Current Owner:  SP B
Default Owner:  SP B
Allocation Owner:  SP B
User Capacity (Blocks):  2147483648
User Capacity (GBs):  1024.000
Consumed Capacity (Blocks):  2005641216
Consumed Capacity (GBs):  956.364
Pool Name:  Pool HB Hasselt
Raid Type:  Mixed
Offset:  0
Auto-Assign Enabled:  DISABLED
Auto-Trespass Enabled:  DISABLED
Current State:  Ready
Status:  OK(0x0)
Is Faulted:  false
Is Transitioning:  false
Current Operation:  None
Current Operation State:  N/A
Current Operation Status:  N/A
Current Operation Percent Completed:  0
Is Pool LUN:  Yes
Is Thin LUN:  Yes
Is Private:  No
Is Compressed:  No
Tiering Policy:  Lowest Available
Initial Tier:  Lowest Available
Tier Distribution:  
Capacity:  100.00%

...
4

2 回答 2

0

构建自定义对象列表,如下所示:

& "NaviSecCli.exe" -user xxxx -password xxxx -h host -f "C:\LUNstate.txt" lun -list

$datafile = 'C:\LUNstate.txt'
$pattern  = 'Name:\s+(.*)[\s\S]+(User Capacity).*?:\s+(.*)\s+(Consumed Capacity).*?:\s+(.*)'

$LUNArray = (Get-Content $datafile | Out-String) -split '\r\n(\r\n)+' |
  Select-String $pattern -AllMatches |
  Select-Object -Expand Matches |
  % {
    New-Object -Type PSObject -Property @{
      'LUN'              = $_.Groups[1].Value
      $_.Groups[2].Value = $_.Groups[3].Value
      $_.Groups[4].Value = $_.Groups[5].Value
    }
  }

数据可以显示如下:

"{0}: {1}" -f $LUNArray[1].LUN, $LUNArray[1].'Consumed Capacity'
于 2014-09-30T10:50:13.867 回答
0
$filePath = 'absolute path'
$content = [IO.File]::ReadAllText($filePath)
[regex]::Matches(
    $content,
    '(?x)
        Name: [ ]* ([^\n]+)  #  name
        \n User [ ] (Capacity) [^:]+: [ ]* ([^\n]+) # capacity 
        \n Consumed [ ] \2  [^:]+:[ ]* ([^\n]+)' # Consumed
    ) | 
    ForEach-Object {

              $LUN = $_.groups[1].value
              $Usercap = $_.groups[3].value
              $ConsCap = $_.groups[4].value
          # process $Lun, $Usercap and $ConsCap

    }
于 2014-09-30T15:30:16.463 回答