CSV 格式的 Start-Job 输出
Start-Job output in CSV format
如何以 CSV 格式获取作业输出。当我执行下面的命令时,我在屏幕上得到了输出,但是当我将它导出为 CSV 时,它没有相同的格式。
$wmidiskblock = {
Get-WmiObject -ComputerName $args[0] -Class Win32_LogicalDisk -Filter "DeviceID='C:'" |
Select-Object Size, Freespace
(Test-Connection -ComputerName $args[0] | Select-Object -ExpandProperty IPV4Address) |
Select-Object IPAddressToString -Unique
Get-Service -ComputerName $args[0] | ? {
($_.DisplayName -match "VMWARE") -and
($_.Name -notmatch "mbcs") -and
($_.Name -notmatch "vmvss") -and
($_.Name -notmatch "vmware-autodeploy-waiter") -and
($_.Name -notmatch "vmware-network-coredump") -and
($_.Name -notmatch "VMWareNetworkCoredumpWebserve") -and
($_.Name -notmatch "vsan-health")
} -ErrorAction Stop
}
$com = @()
$com = "Server-x" , "Server-y"
$pop = @()
foreach ($ser in $com) {
[array]$pop += Start-Job -ArgumentList $ser -ScriptBlock $wmidiskblock -Name top1
}
Get-Job -Name top1 | Receive-Job -Keep
实际输出:
Size : 64422408192
Freespace : 4908081152
RunspaceId : cdb3xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
IPAddressToString : x.x.x.x
RunspaceId : cdb3xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
Status : Running
Name : client_service
DisplayName : VMware Horizon Client
Status : Running
Name : ftnlsv3hv
DisplayName : VMware Netlink Supervisor Service
Status : Running
Name : ftscanmgrhv
DisplayName : VMware Scanner Redirection Client
Server-x
所需的输出(作为 CSV 文件):
Server Totalspace in GB Freespace in GB IP VMware ESX Agent Manager VMware Inventory Service
Server-x 100 36 144.215.150.67 Running Running
您需要将数据转换为实际上可导出为 CSV 的数据。基本上,这意味着您需要获取从服务器中提取的信息位,并将其放入每个服务器的一个对象中:
$wmidiskblock = {
$disk = Get-WmiObject ...
$addr = (Test-Connection ...
$svc = Get-Service ...
$prop = [ordered]@{
Server = $args[0]
Totalspace = $disk.Size
Freespace = $disk.Freespace
IP = $addr
}
$svc | ForEach-Object { $prop[$_.Name] = $_.Status }
New-Object -Type PSObject -Property $prop
}
然后您可以像这样导出从作业中收到的数据:
... | Receive-Job | Export-Csv 'C:\path\to\output.csv' -NoType -Append
如何以 CSV 格式获取作业输出。当我执行下面的命令时,我在屏幕上得到了输出,但是当我将它导出为 CSV 时,它没有相同的格式。
$wmidiskblock = {
Get-WmiObject -ComputerName $args[0] -Class Win32_LogicalDisk -Filter "DeviceID='C:'" |
Select-Object Size, Freespace
(Test-Connection -ComputerName $args[0] | Select-Object -ExpandProperty IPV4Address) |
Select-Object IPAddressToString -Unique
Get-Service -ComputerName $args[0] | ? {
($_.DisplayName -match "VMWARE") -and
($_.Name -notmatch "mbcs") -and
($_.Name -notmatch "vmvss") -and
($_.Name -notmatch "vmware-autodeploy-waiter") -and
($_.Name -notmatch "vmware-network-coredump") -and
($_.Name -notmatch "VMWareNetworkCoredumpWebserve") -and
($_.Name -notmatch "vsan-health")
} -ErrorAction Stop
}
$com = @()
$com = "Server-x" , "Server-y"
$pop = @()
foreach ($ser in $com) {
[array]$pop += Start-Job -ArgumentList $ser -ScriptBlock $wmidiskblock -Name top1
}
Get-Job -Name top1 | Receive-Job -Keep
实际输出:
Size : 64422408192 Freespace : 4908081152 RunspaceId : cdb3xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx IPAddressToString : x.x.x.x RunspaceId : cdb3xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx Status : Running Name : client_service DisplayName : VMware Horizon Client Status : Running Name : ftnlsv3hv DisplayName : VMware Netlink Supervisor Service Status : Running Name : ftscanmgrhv DisplayName : VMware Scanner Redirection Client Server-x
所需的输出(作为 CSV 文件):
Server Totalspace in GB Freespace in GB IP VMware ESX Agent Manager VMware Inventory Service Server-x 100 36 144.215.150.67 Running Running
您需要将数据转换为实际上可导出为 CSV 的数据。基本上,这意味着您需要获取从服务器中提取的信息位,并将其放入每个服务器的一个对象中:
$wmidiskblock = {
$disk = Get-WmiObject ...
$addr = (Test-Connection ...
$svc = Get-Service ...
$prop = [ordered]@{
Server = $args[0]
Totalspace = $disk.Size
Freespace = $disk.Freespace
IP = $addr
}
$svc | ForEach-Object { $prop[$_.Name] = $_.Status }
New-Object -Type PSObject -Property $prop
}
然后您可以像这样导出从作业中收到的数据:
... | Receive-Job | Export-Csv 'C:\path\to\output.csv' -NoType -Append