0

So I wrote a script to go through and optimize some VHDX files and output the results. In an effort to reduce the script run time (dramatically), I taught myself Workflow. That was an adventure on it's own but I got the script working up to the point of outputting the results. Calling the variable and piping it into a sort-object by percentage saved, then piped into a sort-object by job status, then piped into a format table; the code isn't actually sorting the data. It just outputs in order added to the array.

The code worked fine when it wasn't pulling the data from the Workflow (ran serially through the script, instead). My assumption is that the Workflow is outputting the data as a full table to the script array, rather than individual entries, so there isn't anything to sort by the time it gets to the script (the script sees it as a single entry).

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"

I'm still working on optimizing and cleaning up the code but I'm trying to get it working first. Sorry in advance, it might be a bit difficult to read.

I anticipated getting output, to the console, of the object; sorted and grouped by status so the table only shows two or three sections. I also should have received an unsorted CSV of the data from the array but instead got an empty CSV with a row stating "#TYPE Selected.System.Object[]" and a row with the column names. The console output looks like the following:

   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

The output should have looked like:

   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
2
  • Use Export-Csv with the -NoTypeInformation switch to get rid of the "#TYPE Selected.System.Object[]" Commented Feb 7, 2019 at 7:25
  • Awesome! That did remove that line but unfortunately it's still not outputting the data. It looks like the Workflow passed the array to the script as an object (table) instead of entries. The CSV exports the properties (count, length, etc) of the array instead of the array itself. It surprisingly tricky to get data out of a Workflow. Commented Feb 8, 2019 at 0:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.