markus2016
Goto Top

Funktion Run Job mit ProgressBar - oscdimg x complete

Hallo zusammen,

ich übergebe meiner Funktion einen Scriptblock und dieser wird dann in einem Runspace ausführt.
Während der Ausführung wird eine Progressbar in meiner GUI anzeigt.
Das funktioniert auch gut.

Der Aufruf der Funktion z.B. mit dem Scriptblock "oscdimg" läuft fehlerfrei durch.
unbenannt

Derzeit verwende ich für meine Progressbar, bei "$SyncHash.PercentComplete = 0" einen Ersatzwert "$SyncHash.Value", welcher in einer Schleife die Progressbar steuert.
Ich würde aber gerne den %-Wert "complete" von "oscdimg" für die Progressbar verwenden. Wie kann ich den abrufen?

Irgendwie komme ich gerade nicht weiter.
Bin für jede Unterstützung dankbar.

Aufruf Funktion
Run-Job-ProgressBar -Text "Create ISO" -Job ([scriptblock]::create("(Start-Process -FilePath ""$oscdimg"" -args @('""-bootdata:""$BootData','-h','-m','-o','-u2','-udfver102','-l""$NameISO""','""$PathImage""','""$PathISO""') -PassThru -Wait -NoNewWindow).Exitcode"))  

Funktion
$Script:SyncHash = [Hashtable]::Synchronized(@{})

Function Run-Job-ProgressBar {
  
  param
  (
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [scriptblock]$Job,
    [Parameter(Mandatory = $false)]
    [String]$Text = ([String]$Job).Split(" "),  
    [Parameter(Mandatory = $false)]
    [String]$AddText,
    [Parameter(Mandatory = $false)]
    [String]$TextLocation = "340,290"  
  )
  
  try
  {
  $ProgressBar = New-Object System.Windows.Forms.ProgressBar
  $ProgressBar.Name = "ProgressBar"  
  $ProgressBar.Location = "340,505"  
  $ProgressBar.Size = "620,20"  
  $Progressbar.ForeColor = "lightblue"  
  $Progressbar.BackColor= "#f7f7f7"  
  $ProgressBar.Minimum = 0
  $ProgressBar.Maximum = 100 
  $ProgressBar.DataBindings.DefaultDataSourceUpdateMode = 0
  
  $ProgressInfo = New-Object System.Windows.Forms.Label
  $ProgressInfo.Location = New-Object System.Drawing.Size($TextLocation)
  $ProgressInfo.Size = New-Object System.Drawing.Size(620,20)
  $ProgressInfo.ForeColor = "red"  
  $ProgressInfo.DataBindings.DefaultDataSourceUpdateMode = 0

  $Runspace =[RunspaceFactory]::CreateRunspace()
  $Runspace.ApartmentState = "STA"   
  $Runspace.ThreadOptions = "ReuseThread"             
  $Runspace.Open() 
  $Runspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)  

  $syncHash.Runspace = $Runspace    
    
  $JobRunspace = [PowerShell]::Create().AddScript($Job)
   
  $SyncHash.Job = $JobRunspace
  $AsyncResult = $SyncHash.Job.BeginInvoke()
  
  $SyncHash.Value = 0
  $SyncHash.PercentComplete = 0

  while(!$AsyncResult.IsCompleted)
  {

  [System.Windows.Forms.Application]::DoEvents()
 
  $Window.Controls.Add($ProgressBar)
  $Window.Controls.Add($ProgressInfo)
   
  $JobRunspace.Streams.Progress | ForEach-Object {
  IF ($_.Activity -and $_.Activity -ne $null) { $SyncHash.Activity = $_.Activity }
  IF ($_.StatusDescription -and $_.StatusDescription -ne $null) { $SyncHash.StatusDescription = $_.StatusDescription } 
  IF ($_.CurrentOperation -and $_.CurrentOperation -ne $null) { $SyncHash.CurrentOperation = $_.CurrentOperation }
  IF ($_.PercentComplete -and $_.PercentComplete -gt -1) {$SyncHash.PercentComplete = $_.PercentComplete} 
  }

  $ProgressInfo.Text = $Text + " " + $AddText + $(IF ($SyncHash.PercentComplete -gt 0){" | " + $SyncHash.StatusDescription + " | "  + $SyncHash.PercentComplete + " %"})  
  $ProgressInfo.Update()

  Switch ($SyncHash.PercentComplete){
  {$_ -eq 0}{
  $ProgressBar.Value = $SyncHash.Value
  $ProgressBar.Update()

  Start-Sleep -Milliseconds 5

  IF ($SyncHash.Value -eq 100) {$SyncHash.Value = 0} elseIF ($AsyncResult.IsCompleted -eq $true) {break}
       
  $SyncHash.Value++
  }
  {$_ -ge 1}{
  $ProgressBar.Value = $SyncHash.PercentComplete
  $ProgressBar.Update()  
  }
  }

  $JobRunspace.EndInvoke($AsyncResult)
  }
  }

  finally
  {

  IF ($JobRunspace.Streams.Error.Count -gt 0) {ForEach ($ErrorRecord in $JobRunspace.Streams.Error){Write-Error -ErrorRecord $ErrorRecord}}
  IF ($JobRunspace.Streams.Warning.Count -gt 0) {ForEach ($Warning in $JobRunspace.Streams.Warning){Write-Warning -Message $Warning}}
  IF ($JobRunspace.Streams.Verbose.Count -gt 0) {ForEach ($Verbose in $JobRunspace.Streams.Verbose){Write-Verbose -Message $Verbose}}
  
  $Window.Controls.Remove($ProgressBar)
  $Window.Controls.Remove($ProgressInfo)

  $JobRunspace.Stop()   
  $JobRunspace.Runspace.Close()
  $JobRunspace.Dispose()

  }
   
}

Schöne Grüße
Markus2016

Content-Key: 383800

Url: https://administrator.de/contentid/383800

Ausgedruckt am: 19.03.2024 um 09:03 Uhr