这真的很丑陋,而且非常脆弱。一个好的 HTML 解析器将是一个更好的方法来做到这一点。
但是,假设您没有为此获得资源,这里有一种获取数据的方法。如果你真的想再生成两个文件 [Admin & User],你可以从这个对象中做到这一点......
# fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
</p><ul><li>(None)</li></ul><h2><span style="font-size:18px;">Authorized Administrators and Users</span></h2><pre><b>Authorized Administrators:</b>
jim (you)
password: (blank/none)
bob
password: Littl3@birD
batman
password: 3ndur4N(e&home
dab
password: captain
<b>Authorized Users:</b>
bag
crab
oliver
james
scott
john
apple
</pre><h2><span style="font-size:18px;">Competition Guidelines</span></h2>
'@ -split [environment]::NewLine
$CleanedInStuff = $InStuff.
Where({
$_ -notmatch '^</' -and
$_ -notmatch '^ ' -and
$_
})
$UserType = 'Administrator'
$UserInfo = foreach ($CIS_Item in $CleanedInStuff)
{
if ($CIS_Item.StartsWith('<b>'))
{
$UserType = 'User'
continue
}
[PSCustomObject]@{
Name = $CIS_Item.Trim()
UserType = $UserType
}
}
# on screen
$UserInfo
# to CSV
$UserInfo |
Export-Csv -LiteralPath "$env:TEMP\LandonBB.csv" -NoTypeInformation
屏幕输出...
Name UserType
---- --------
jim (you) Administrator
bob Administrator
batman Administrator
dab Administrator
bag User
crab User
oliver User
james User
scott User
john User
apple User
CSV 文件内容...
"Name","UserType"
"jim (you)","Administrator"
"bob","Administrator"
"batman","Administrator"
"dab","Administrator"
"bag","User"
"crab","User"
"oliver","User"
"james","User"
"scott","User"
"john","User"
"apple","User"