所以我写了一个脚本来遍历和优化一些VHDX文件并输出结果。为了(显着)减少脚本运行时间,我自学了工作流程。这本身就是一次冒险,但我让脚本工作到输出结果的地步。调用变量并按保存百分比将其传递到排序对象中,然后按作业状态传递到排序对象中,然后传递到格式表中;代码实际上并没有对数据进行排序。它只是按添加到数组的顺序输出。
当它没有从工作流中提取数据时,代码运行良好(而是通过脚本串行运行)。我的假设是工作流将数据作为完整表输出到脚本数组,而不是单个条目,因此在到达脚本时没有任何要排序的内容(脚本将其视为单个条目) .
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"
我仍在努力优化和清理代码,但我正试图让它首先工作。提前抱歉,可能有点难以阅读。
我期望将对象的输出输出到控制台;按状态排序和分组,因此该表仅显示两个或三个部分。我还应该从数组中收到一个未排序的数据 CSV,而是得到一个空的 CSV,其中一行表示“#TYPE Selected.System.Object[]”,一行包含列名。控制台输出如下所示:
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
输出应该如下所示:
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
S******* G********** 1.82 1.82 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
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