我正在尝试解析输出字符串之间的一些文本。
我尝试使用-split和substring,但是我没有得到正确的输出
这是输入文件/管道值
Please remove the following Roles:
Role1
Role2
Please remove the following Groups:
Groups1
Groups2
Please remove the following Profiles:
Profiles1
Profiles3
Profiles8
Please remove the following DGroups:
DG1
DG9
DG12
这是我尝试的代码
Foreach($input in $file){
#I’ve tried the split and I get too much data after roles
write-host $input.split(':')[1]
#replace gives me too much info after roles
$input.replace('Please remove the following Roles:','')
#this loop will continuously run
do{
$input}
until{$input.contains("please*")}
}
我希望输出给我Role1和Role2,然后是groups1和groups2,然后是profile1和profile3和profile8,然后是dg1和dg9和dg12,然后忽略其余部分。
我遇到的问题是在do循环中,我得到了连续循环。 在替换中,我获得了角色,但也获得了请删除组行。
答案 0 :(得分:1)
[由于示例数据发生了很大的变化,因此先前的代码无法编辑。]
它做什么...
[PSCustomObject]
,其中包含目标类型和项目列表$Results
集合Roles
替换为其他类型之一-Groups
就是其中一种。 $Results
集合的内容代码...
# fake reading in a text file as one multiline string
# in real life, use Get-Content -Raw
$InStuff = @'
Please remove the following Roles:
Role1
Role2
Please remove the following Groups:
Groups1
Groups2
Please remove the following Profiles:
Profiles1
Profiles3
Profiles8
Please remove the following DGroups:
DG1
DG9
DG12
'@
$Results = foreach ($Block in $InStuff -split "`r`n`r`n|`r`r|`n`n")
{
$SplitBlock = $Block -split "`r`n|`r|`n"
$BlockName = $SplitBlock[0].Split(' ')[-1].Trim(':')
$ItemList = $SplitBlock[1..$SplitBlock.GetUpperBound(0)]
[PSCustomObject]@{
TargetType = $BlockName
TargetList = $ItemList
}
}
$Results.Where({$_.TargetType -eq 'Roles'}).TargetList
'=' * 30
$Results
输出...
TargetType TargetList
---------- ----------
Roles {Role1, Role2}
Groups {Groups1, Groups2}
Profiles {Profiles1, Profiles3, Profiles8}
DGroups {DG1, DG9, DG12}
答案 1 :(得分:0)
$ file -replace'。+(?=角色:|组:|配置文件:| DGroups:)'