Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get-Job Status Reporting Failure?

$
0
0

Been doing PowerShell for quite some time now, but this issue really stumps me and I'm wondering if I found a bug.

Setup: Windows 2012 R2, PowerShell 4.0, PowerCLI (VMware) 5.5

I've written an all-in-one deployment script for VMware PowerCLI cmdlets to take a spreadsheet and feed the values into a script block, which will then deploy the new VM through the various stages to prep, deploy, set custom settings, etc, run cleanup and finish.

I utilize heavily write-progress statements to give -Activity, -Status and -PercentComplete, which is read by Get-Job to display an On-Screen Display of what is happening in the background. -Activity is static on each line, while -Status and -PercentComplete change with each step to keep in toe with script progress and allow the user to know where progress is at.

The issue:

The reporting always fails at the same point, while the job itself completes. I use a custom function to write log data events to a log file, and it will continue to write, commands will continue to execute. It's almost like the command from PowerCLI causes an issue with write-progress reporting. When the script is executing, Get-Job will lists jobs correctly, but the Percent Complete and Status always hang on my write-progress line "Setting Network Label" at 36 percent complete from Get-Job output. Get-Job will list the -state of the job correctly, however. Seems only after the PowerCLI cmdlet Set-NetworkAdapter is executed that write-progress is never updated again to Get-Job.

This is the point where get-job hangs reading write-progress, inside the script block. Thank you!!

#BLOCK STAGE::Set Network Label

if($BlockStageIX-eq"Continue")
{
Write-Progress-Activity"VM::$($vms.name)"-Status"Setting Network Label"-PercentComplete36 #-Completed
Write_Log ("ATTEMPT: Setting Network Label")

Try {
$ProgressPreference="SilentlyContinue"

Set-NetworkAdapter-NetworkAdapter (Get-VM$vms.name |Get-NetworkAdapter) -NetworkName$vms.netlabel -Confirm:$False-StartConnected:$True-ErrorActionStop-WarningActionSilentlyContinue|Out-Null

$ProgressPreference="Continue"

Write-Progress-Activity"VM::$($vms.name)"-Status"Network Label Set"-PercentComplete37#-Completed

Write_Log ("SUCCESS: Network Label Set: $($vms.netlabel)")

$BlockStageX="Continue"
}

Catch

{
Write_Log ("FAILURE: Could Not Set Label: $($vms.netlabel)")

$BlockStageX="Halt"
}


Custom functions listed, just in case(I have a more elegant reporting function, this was reduced to see if something was off in the more elegant solution, which is wasn't sadly).

#----------------------------------------------------------------------------------------------------------------------
#Write_Log

function
Write_Log($msg)

{
# Create a DateTimeStamp with the current date and time
$DateTimeStamp=get-date-uformat"%Y-%m-%d %H:%M:%S"
$logmsg=$DateTimeStamp+" "+$msg
Add-Content-path"$ScriptRoot\MHSVDM_Log.log"-value$logmsg-ErrorActionSilentlyContinue
}
#----------------------------------------------------------------------------------------------------------------------

#Read-JobStatus
functionRead-JobStatus{
CLS
Write-Host"---------------------------"-ForegroundColorWhite
Write-Host" Deployment Status"-ForegroundColorMagenta
Write-Host"---------------------------"-ForegroundColorWhite
ForEach($JobStatusinGet-Job)
{
ForEach ($ChildStatusin$JobStatus.ChildJobs)
{
$CurrentProgress=$ChildStatus.Progress[$ChildStatus.Progress.Count -1]
$CurrentProgress|Select-ObjectActivity,StatusDescription,PercentComplete
}
}
Start-Sleep1
}
#----------------------------------------------------------------------------------------------------------------------


Viewing all articles
Browse latest Browse all 6937

Trending Articles