CSV格式的开始作业输出

时间:2017-02-27 15:27:00

标签: powershell export-to-csv jobs

如何以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

1 个答案:

答案 0 :(得分: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