Latest Updates to PoshRSJob v1.5.7.1

The past few weeks have been about updating some of my modules such as PoshWSUS and now PoshRSJob. With PoshRSJob, I spent a lot of the time focused on improving the PowerShell V2 support by fixing some bugs that had been reported but I also took on a request to provide support for the module to run on Nano server.

Originally, I used Add-Type to compile and load some C# code to provide the classes that the module will use. Unfortunately, Nano server doesn’t have all of the cmdlets available that we know and love and includes Add-Type. I took one approach by using reflection to build out the classes and while this worked outside of Nano, it ultimately didn’t really…yet. I didn’t want to spend a lot of time on that just yet, so I decided to build a dll that would get loaded with the module to create the classes. After fixing that, I had to fix a spot where the runspace.ApartmentState was set to STA to support UIs as Nano doesn’t appear to have an ApartmentState property available to set.

NanoPoshRSJob

The release notes is available below along with the link to the GitHub page where you can download the module or help contribute if you want!

https://github.com/proxb/PoshRSJob

Release Notes

  • Fixed Issue #64 (HadErrors in PoshRS.PowerShell.RSJob throws errors in PowerShell V2)
  • Fixed Issue #67 (Converted Add-Type code for C# classes to be created via Reflection for Nano server support) <- Created custom dll
  • Fixed Issue #61 (Receive-RSJob not allowing -Job parameter input)
  • Fixed Issue #63 (Replaced Global variables with Script scope)
  • Fixed Issue #66 (Parameters don’t work with PowerShell V2)
  • Fixed Issue #65 (Bug with v2 variable substitution – single-quoted strings get $var references replaced)
  • Fixed Issue #68 (ApartmentState Does Not Exist in Nano)
This entry was posted in powershell and tagged , , . Bookmark the permalink.

7 Responses to Latest Updates to PoshRSJob v1.5.7.1

  1. Jon Gibert says:

    Love PoshRSJobs and your blog!

    I seem to be seeing an error when using Wait-RSJob using both of these parameters
    Wait-RSJob -Name “Myjobname” -State “Completed”

    Error is divide by zero on line 180 … where $Completed is divided by $TotalJobs. Looks like the same calculation is used again two lines below that.

    Can you confirm whether or not this is a bug, or am I using the cmdlet wrong? I want to wait for my job to finish.

    Thanks.

    • Boe Prox says:

      Hi Jon!
      What version of PoshRSJob are using? Could you provide the code that you are using?

      • Jon Gibert says:

        Here are the most pertinent portions of our test script, This is running on Win7 with .NET framework 4.6.1 and PowerShell v5
        And if I use the cmdlet without the -State switch (Wait-RSJob -Name “jobname”) then it seems to never detects that the Runspace job finished after clicking the second button to close the UI window.

        Common header

        #Requires -Version 2.0
        set-strictmode -version 2.0
        $DebugPreference = “Continue”

        $SCRIPT:scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent

        Import module for PoshRSJob cmdlets

        try {
        Import-Module $($SCRIPT:scriptDir + “\PoshRSjob.psd1”) -Force -ErrorAction Stop
        }
        catch {
        if (-not ([System.Diagnostics.EventLog]::SourceExists(‘Win10-IPU-TSInit’))) {
        # Register our application as a valid Source for the Application event log
        New-EventLog -LogName “Application” -Source “Win10-IPU-TSInit”
        }
        # Write to the Windows Event log that we couldn’t load the necessary module
        Write-EventLog -EntryType “Error” -EventID 9001 -LogName “Application” -Source “Win10-IPU-TSInit” -Message “Error loading the required PoshRSJob module.nError Details:n’$($_.Exception.Message)'”
        # Display an ugly error message box
        $objShell = new-object -comobject wscript.shell
        $intResponse = $objShell.popup(“There was a fatal error starting the Windows 10 in-place upgrade program. Please contact the Service Desk.”,0,”Error starting program”,16)
        # Quit
        return
        }

        add the presentation framework if needed

        TODO: Check to see if already loaded

        Add-Type -AssemblyName presentationframework

        $SyncHashTable = [Hashtable]::Synchronized(@{})
        $SyncHashTable[“MyTestString”] = “Checking…”

        $UIScript = {
        Param($SyncHash)

        #Build the GUI
        [xml]$xaml = @”

        “@

        $reader=(New-Object System.Xml.XmlNodeReader $xaml)
        $Window=[Windows.Markup.XamlReader]::Load( $reader )

        Connect to controls

        $CloseButton = $Window.FindName(“button2”)
        $StatusLabel = $Window.FindName(“ipu_label”)

        $Window.Add_SourceInitialized({
        #Create Timer object
        Write-Verbose ‘Creating timer object’
        $Script:timer = new-object System.Windows.Threading.DispatcherTimer
        #Fire off every 1 second
        Write-Verbose ‘Adding 1 second interval to timer object’
        $timer.Interval = [TimeSpan]’0:0:01′
        #Add event per tick
        Write-Verbose ‘Adding Tick Event to timer object’
        $timer.Add_Tick({
        $StatusLabel.Content = $SyncHash[“MyTestString”]
        })
        #Start timer
        Write-Verbose ‘Starting Timer’
        $timer.Start()
        })

        $Window.Add_MouseLeftButtonDown({
        $This.DragMove()
        })

        $CloseButton.Add_Click({
        $Window.Close()
        [System.Threading.Monitor]::Enter($SyncHash.SyncRoot)
        $SyncHash[“UITest_Output”] = “User Clicked Exit”
        Start-Sleep -Seconds (Get-Random (1..5))
        [System.Threading.Monitor]::Exit($SyncHash.SyncRoot)
        })

        $Window.Showdialog() | Out-Null
        }

        Start-RSJob -Name “UITest” -ScriptBlock $UIScript -ArgumentList $SyncHashTable
        Wait-RSJob -Name “UITest” -State “Completed”

        • Jon Gibert says:

          Heh. Is there a way to post and not have the web page mangle the straight quotation marks with fancy ones?

        • Jon Gibert says:

          I happened to also test this script with PowerShell v2 (Win7 with only v2; and Win7 with v5 but invoking PS with the -version 2.0 switch) and saw two other odd behaviors.

          If set-strictmode is changed to version 1.0 (necessary for compatibility with PS v2) then your PoshRSJob modules throw errors about variables not being defined.

          After commenting out the set-strictmode command (to reduce the noise) I also noticed that the Start-RSJob cmdlet does not return control to the main script until the job it started is completed (closing the UI windows that is opened.)

          Although we expect a minimum of PS v3 on Win7 in our corporate environment, I know that there are still many Win7 computers that were imaged years ago and have not had PS upgraded from v2 to v3 or higher yet (mainly due to unhealthy SCCM agents… sigh) So I need to test and make all of our solutions run on Win7 with PS v2

          • Jon Gibert says:

            Sorry. Correction: the command “set-strictmode -version 2.0” of course works with PS v2, but using it causes even more errors to be thrown.

      • Jon Gibert says:

        I forgot to answer your first question. I am using PoshRSJob v1.5.7.7

Leave a comment