我需要在2008 R2服务器上从dirquota au l
命令获取以下输出,遍历每一行,将每个自动配额路径的相关数据添加到其自己的唯一对象,然后添加所有对象到一个数组。见下面的例子:
Auto Apply Quota Path: E:\Dir\SubDir Share Path: \\SERVER\SubDir \\SERVER\E\Dir\SubDir \\SERVER\Dir\SubDir Source Template: Template (Matches template) Limit: 500.00 MB (Hard) Thresholds: Warning ( 80%): Event Log Limit (100%): Event Log Auto Apply Quota Path: E:\Dir\SubDir Share Path: \\SERVER\SubDir \\SERVER\E\Dir\SubDir \\SERVER\Dir\SubDir Source Template: Template (Matches template) Limit: 500.00 MB (Hard) Thresholds: Warning ( 80%): Event Log Limit (100%): Event Log Auto Apply Quota Path: E:\Dir\SubDir Share Path: \\SERVER\SubDir \\SERVER\E\Dir\SubDir \\SERVER\Dir\SubDir Source Template: Template (Matches template) Limit: 500.00 MB (Hard) Thresholds: Warning ( 80%): Event Log Limit (100%): Event Log Auto Apply Quota Path: E:\Dir\SubDir Share Path: \\SERVER\SubDir \\SERVER\E\Dir\SubDir \\SERVER\Dir\SubDir Source Template: Template (Matches template) Limit: 500.00 MB (Hard) Thresholds: Warning ( 80%): Event Log Limit (100%): Event Log Auto Apply Quota Path: E:\Dir\SubDir Share Path: \\SERVER\E\Dir\SubDir \\SERVER\SubDir \\SERVER\Dir\SubDir Source Template: Template (Matches template) Limit: 500.00 MB (Hard) Thresholds: Warning ( 80%): Event Log Limit (100%): Event Log
例如,采取任何自动配额,我想隔离"自动应用配额路径:"部分,我知道我可以使用RegEx和Select-String
,将其添加到" Name"下的对象中。属性,然后隔离" E:\ Dir \ Subdir" part并将其添加到" Value"下的同一个对象属性。然后我想从源模板和限制中收集数据,将它们添加到同一个对象中。然后,一旦到达空白行,将刚刚创建的对象添加到数组中,清空对象变量,然后开始迭代下一个自动配额部分,完成相同的过程。
这是我目前使用的代码:
$AutoQuota = @()
$AutoQuota = dirquota au l
$AutoQuota = $AutoQuota[2..$AutoQuota.Count]
$obj = New-Object PSObject
$objArr = @()
$AutoQuota | % {
switch -regex ($_) {
"^Auto.*" {
$Name = $_ | Select-String -Pattern "^Auto*: " |
%{$_.Matches} | %{$_.Value}
$Value = $_ | Select-String -Pattern "\w:*" |
%{$_.Matches} | %{$_.Value}
$obj | Add-Member -MemberType NoteProperty -Name $Name -Value $Value
}
"^\s*$" {
$objArr += $obj
$obj = @()
}
}
}
显然,switch
语句会有更多内容,我只想让第一行按预期工作,然后我将添加其余内容。如果有更简单/更好的方法,我可以接受建议。
答案 0 :(得分:0)
将命令输出转换为单个字符串,并将该字符串拆分为两个连续的换行符(Windows将换行符编码为CR-LF,因此每个换行符需要\r\n
)
(dirquota au l | Out-String) -split '\r\n\r\n'
然后按正则表达式进行过滤,该表达式将要从子字符串中提取的值分组,并直接从匹配中构造对象:
$re = 'Auto Apply Quota Path:\s+(.*)[\s\S]*?' +
'Source Template:\s+(.*)\s+' +
'Limit:\s+(.*)'
... | Where-Object { $_ -match $re } | ForEach-Object {
New-Object -Type PSObject -Property @{
Path = $matches[1]
Template = $matches[2]
Limit = $matches[3]
}
}
答案 1 :(得分:0)
尝试类似这样的事情
$template=@"
{Groupement*:Auto Apply Quota Path: {QuotaPath:c:\Tmp\tmp2}
Share Path: {SharePaths:\\Server\
Yy2
Zz3}
Source Template: {SourceTemplate:Template xxx (Matches template)}
Limit: {Limit:600.00 MB (Hard)}
Thresholds:
Warning ( 80%): {TWarning:Event0 Log0}
Limit (100%): {LWarning:Event0 Log0}}
{Groupement*:Auto Apply Quota Path: {QuotaPath:Zxx}
Share Path: {SharePaths:Xx3
Yy4
Zz5}
Source Template: {SourceTemplate:Template (Matches template)}
Limit: {Limit:500.00 MB}
Thresholds:
Warning (100%): {TWarning:Event1 Log1}
Limit (100%): {LWarning:Event1 Log1}}
"@
$result=$AutoQuota | ConvertFrom-String -TemplateContent $template
$result.groupement | select QuotaPath, SharePaths, SourceTemplate, Limit, TWarning, LWarning