Powershell Workflow的阵列输出未正确格式化或导出

时间:2019-02-07 00:39:53

标签: powershell arraylist export-to-csv powershell-workflow

所以我写了一个脚本来检查和优化一些VHDX文件并输出结果。为了减少脚本运行时间(剧烈地),我自学了Workflow。这本身就是一个冒险,但是我让脚本工作到可以输出结果的程度。调用该变量并将其按保存的百分比传递到排序对象中,然后按作业状态传递到排序对象中,然后传递到格式表中;代码实际上并没有对数据进行排序。它只是按顺序输出到数组。

当没有从工作流中提取数据时,代码运行良好(而是通过脚本顺序运行)。我的假设是工作流将数据作为完整表输出到脚本数组,而不是单个条目,因此,在到达脚本时,没有什么可排序的(脚本将其视为单个条目)

c

我仍在优化和清理代码,但我正在尝试使其首先工作。预先抱歉,可能有点难以阅读。

我期望将对象的输出输出到控制台;按状态排序和分组,因此该表仅显示两个或三个部分。我还应该从数组中接收到未排序的CSV数据,但是却得到了一个空CSV,其中包含一行说明“#TYPE Selected.System.Object []” 的行,以及一行包含列名称的行。控制台输出如下所示:

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

$listofprof = New-Object System.Collections.ArrayList
$UPDList = Get-ChildItem $PSScriptRoot -Filter *.vhdx | select -ExpandProperty Name
$updhome = $PSScriptRoot

Workflow TestCompact {
    param(
        [string]$updhome,
        [array]$UPDList
    )
    Foreach -Parallel ($i in $UPDList) {
            $listofproftemp =
                InlineScript {
                $startsize = ("{0:N2}" -f ((Get-ChildItem "$Using:updhome\$Using:i" | select -ExpandProperty length)/1GB))
                $usersid = $Using:i -ireplace 'UVHD-|\.vhdx', ""
                $username = (Get-ADUser -Filter 'SID -like $usersid' | Select-Object -ExpandProperty Name)
                Try {
                    Optimize-VHD "$Using:updhome\$Using:i" -mode full -ErrorAction Stop
                    $jobstatus = "Completed"
                } Catch [Microsoft.HyperV.PowerShell.VirtualizationException] {
                    $jobstatus = "Failed (UPD likely in use!)"
                } Catch {
                    $jobstatus = "Failed (Something strange happened :( ....)"
                }
                    $endsize = ("{0:N2}" -f ((Get-ChildItem "$Using:updhome\$Using:i" | select -ExpandProperty length)/1GB))
                    $percentagediff = ("{0:N2}" -f ((($startsize-$endsize)/$startsize)*100))
                $temp = [PSCustomObject]@{
                    UserName = $username
                    StartSize = $startsize
                    EndSize = $endsize
                    PercentageSaved = $percentagediff
                    JobStatus = $jobstatus
                    FileName = $Using:i
                }
                $temp
            }
            $listofproftemp
    }
}

$listofprof.add((TestCompact -updhome $updhome -updlist $UPDList)) | Out-Null
$listofprof | Sort-Object PercentageSaved | Sort-Object JobStatus | Format-Table -AutoSize -GroupBy JobStatus -Property UserName, StartSize, EndSize, PercentageSaved, JobStatus, FileName 
$listofprof | Select-Object UserName, StartSize, EndSize, PercentageSaved, JobStatus, FileName | Export-Csv -Path "$updhome\$(get-date -Format yyyy-MM-dd-hhmmtt).csv"

输出应该看起来像:

   JobStatus: Failed (UPD likely in use!)

UserName        StartSize EndSize PercentageSaved JobStatus                   FileName
--------        --------- ------- --------------- ---------                   --------
C**** S****     1.04      1.04    0.00            Failed (UPD likely in use!) UVHD-S-1-5-21-2318372095-3838506165-4286****09-5***.vhdx
C***** S***** 1.07      1.07    0.00            Failed (UPD likely in use!) UVHD-S-1-5-21-2318372095-3838506165-4286****09-5***.vhdx


   JobStatus: Completed

UserName          StartSize EndSize PercentageSaved JobStatus FileName
--------          --------- ------- --------------- --------- --------
A***** N**** M**** 0.50      0.50    0.00            Completed UVHD-S-1-5-21-2318372095-3838506165-4286****09-5***.vhdx


   JobStatus: Failed (UPD likely in use!)

UserName  StartSize EndSize PercentageSaved JobStatus                   FileName
--------  --------- ------- --------------- ---------                   --------
M**** K***** 1.10      1.10    0.00            Failed (UPD likely in use!) UVHD-S-1-5-21-2318372095-3838506165-4286****09-5***.vhdx


   JobStatus: Completed

UserName           StartSize EndSize PercentageSaved JobStatus FileName
--------           --------- ------- --------------- --------- --------
S******* G********** 1.82      1.82    0.00            Completed UVHD-S-1-5-21-2318372095-3838506165-4286****09-5***.vhdx

0 个答案:

没有答案