用于在Invoke-Command中附加找到或批量记录的脚本的脚本

问题描述 投票:1回答:1

我对powershell很新。以下是我尝试通过invoke-command返回系统列表中的软件记录。它确实有效。但是,似乎所有结果都存储在内存中,然后一次性导出到CSV。相反,我想分批卸载结果或者返回结果。无论哪种方法最简单。这样,我可以从我们的服务器运行类似的类似CMD,而不会让运行脚本的服务器陷入困境,目前其内存负载为99%,然后在脚本完成卸载后CPU跳转到99%结果。

$IC_ScriptBlock = {
    Get-itemproperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\currentversion\Uninstall* |
        Select Publisher, DisplayName, DisplayVersion, installDate
        }
$IC_Params = @{
    ThrottleLimit = 100
    ScriptBlock = $IC_ScriptBlock
    ComputerName = (Get-Content "C:\Users\JT\Desktop\Scripts\laptops.txt")
    }
Invoke-Command @IC_Params |
    Export-csv -NoTypeInformation -Path "C:\Users\JT\Desktop\Scripts\laptops.csv" -Delimiter ";"
powershell powershell-remoting invoke-command
1个回答
1
投票

看起来以下是我需要的。

 $Computers = Get-Content "C:\Desktop\workstations.txt" -ReadCount 20
    FOREACH ($Computer in $Computers) {
    Invoke-Command -ComputerName $Computer -ScriptBlock {
    Get-itemproperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\currentversion\Uninstall\* | 
    Select @{Name="HostName";Expression={$Computer}}, Publisher, DisplayName, DisplayVersion, installDate} |
    Export-csv -NoTypeInformation -Path "C:\Desktop\results.csv" -Delimiter ";" -Append
    } 
    {
    Start-Sleep 1
    }
© www.soinside.com 2019 - 2024. All rights reserved.