美文网首页
[System.Collections.ArrayList]::

[System.Collections.ArrayList]::

作者: 夙小叶 | 来源:发表于2021-07-28 20:13 被阅读0次
    截屏2021-07-28 20.12.23.png

    原代码:

    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

    相关文章

      网友评论

          本文标题:[System.Collections.ArrayList]::

          本文链接:https://www.haomeiwen.com/subject/mpxtvltx.html