2017-02-27 81 views
0

如何以CSV格式獲取作業輸出。當我執行下面的命令時,我會在屏幕上顯示輸出,但是當我將它導出到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
+0

當我嘗試下面的命令,它搞砸了所有格式 Get-Job -Name top1 | Receive-Job -Keep |格式 - 表格* 大小可用空間PSComputerName RunspaceId PSShowComputerName ---- --------- -------------- ------------ ----------------- 64422408192 4909293568 localhost 9e149962-c2e4-47c5-a83a-81e6c4f8c92a False – SUN

回答

1

您需要將您的數據轉換的東西,實際上導出爲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 
+0

Thanks Ansgar Wiechers,This works great :) – SUN