2017-02-27 71 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