我试图在Excel文档中显示数据,其中列A显示服务器名称,列B显示.NET版本。我在导出到.csv时遇到了一个问题,因为它表明文件路径不存在。我想要一些关于如何解决这个问题以及如何在Excel中的两列中显示数据的指导。
$Servers =
(
"test"
)
foreach ($Server in $Servers)
{
Invoke-Command -ComputerName $Server -ScriptBlock {
Write-Output "$(hostname)"
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -Recurse | Get-ItemProperty -Name Version,Release -EA 0 | where { $_.PSChildName -match '^(?!S)p{L}'} | select PSChildName, Version, Release | Select -ExpandProperty Version | Sort-Object Version | Export-Csv -Path C:UsersUserDesktopexample.csv
}
主要问题是在远程主机上使用Export-Csv
,因为它在Invoke-Command
脚本块内,而可能出现的错误是因为用作导出的路径在这些主机上不存在。
同样值得注意的是,Invoke-Command
可以并行运行,-ComputerName
和-Session
可以使用一个数组,这消除了对foreach
循环的需要,而且速度更快/效率更高。
Invoke-Command -ComputerName $servers -ScriptBlock {
Write-Host "Working on $($env:COMPUTERNAME)..."
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -Recurse |
Get-ItemProperty -Name Version, Release -EA 0 |
ForEach-Object {
if($_.PSChildName -notmatch '^(?!S)p{L}') {
return # skip this
}
[pscustomobject]@{
HostName = $env:COMPUTERNAME
Version = $_.Version
}
} | Sort-Object Version
} -HideComputerName | Select-Object * -ExcludeProperty RunspaceID |
Export-Csv -Path C:UsersUserDesktopexample.csv -NoTypeInformation