powershell - Script to append records as found or in batches from Invoke-Command
问题描述
I'm fairly new to powershell. The below is my attempt to return records of software on a list of systems via invoke-command. It does work. However, it seems like all results are stored in memory and then exported to the CSV all at once. Instead, I'd like to offload the results in batches or as they're returned. Whichever is the simplest method. That way, I can run similar similar CMDs from our servers, without it bogging down my server that's running the scripts, whose memory is at 99% load at the moment, and then the CPU jumps to 99% after the script completes to offload the results.
$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 ";"
解决方案
看起来下面是我需要的。
$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
}
推荐阅读
- here-api - 如何使用离线文件进行反向地理编码
- javascript - 如何使用访问器函数(d)来访问父级的值?
- ruby-on-rails - 解析 JSON 并将数据存储到数据库的性能问题
- html - Angular如何定义返回值以显示
- pandas - 如何将值分配给数据框的单元格有效地迭代另一个对象
- c# - 使用 Dapper 的 .net 核心的通用存储库模式
- numpy - 通过 polynomial.legendre 拟合并得到系数
- python - Pylint UnicodeDecodeError utf-8 无法解码字节
- hibernate - 明显的死锁!!!为未分配的待处理任务创建紧急线程
- ruby-on-rails - 在rails中一起使用'where'和'or'