原代码:
Function ServicesList
{
$services_list = [System.Collections.ArrayList]::new()
Foreach($i in $Services)
{
try
{
$var1 = Get-Service -Name $i -ErrorAction Stop | Select-Object -ExpandProperty "Name"
$services_list.add($var1) # <----
}
catch
{
# Continue
Write-Host -ForegroundColor Red "[!] " -NoNewline
Write-Host "Service " -NoNewline
Write-Host -ForegroundColor Cyan $i -NoNewline
Write-Host " not found. Skip"
}
}
# Write-Host $services_list
return $services_list
}
$services_list
的结果包含 0...20
这些代表值 index 的数字,这是结果之外的。
看了几遍官方文档,赣,丢了 [void]
。
[void]$services_list.add($var1)
也可以使用 Generic List
文档
[System.Collections.Generic.List[string]]::new()
修正后:
Function ServicesList
{
$services_list = [System.Collections.ArrayList]::new()
Foreach($i in $Services)
{
# $services_list = [System.Collections.Generic.List[string]]::new()
try
{
$var1 = Get-Service -Name $i -ErrorAction Stop | Select-Object -ExpandProperty "Name"
[void]$services_list.add($var1) # <----
}
catch
{
# Continue
Write-Host -ForegroundColor Red "[!] " -NoNewline
Write-Host "Service " -NoNewline
Write-Host -ForegroundColor Cyan $i -NoNewline
Write-Host " not found. Skip"
}
}
#Write-Host $services_list
return $services_list
}
截屏2021-07-28 20.11.34.png
网友评论