How do you call and show an image from a PowerShell script. You need it in a modal form with that can be closed but such the the calling script waits for its closure. show-image does this.

PowerShell can instatiate a Windows Form, add UI elements to it such as an Image control, buttons and TextBoxes, add some event handlers for the UI elemnts and display the form. It can be orchetsrated so that the calling PS script is block until the form is closed. In that way it can graphical dispplay information to user for their attention before continuing. It could also be used for user input such as selection.

The show-mage function was developed for the djaus2/az-iothub-ps suite as on GitHub. This came about where I was attampting to script the setup of an Azure Sphere app using Azure IoT Central. When I did the same for an Azure IoT HUb - Device Provisoning Service scenario in az-iothub-ps using Az Cli and AzSpehere SDK calls all steps except one could be doen using these APIs, which had to be done in the Azure Portal. Wilh IoT Central, a number of the similar steps required them to be performed in the Azure Portal. There were no API calls for these (working on this though). Where a step wasn’t possible with an API the show-image function was used to show an image of what to do in the Portal at that point. When done, the user presses [Done] on the image and control returns to teh pS script. At return to the script in some cases, it expected ceratin values to have been copied to the clipboard from eth Portal which were seamlessly consumed by the script, giving some sembalnce of automation..

About az-iothub-ps

This tool is a menu driven PowerShell tool that can create an Azure Group, IoT Hub, Device and Device Provisioning Service (both linked to the hub). There is also an option to automatically create all those items from the command line. It automatically makes all of the Azure IoT Hub SDK queries.

show-image

The show-image PS function instantiates a Windows Form, installs an image in the form, sets the dimensions of the form to that of the image and adds a Button with text ‘Done’ at the top left of the form. An event handler is added to the button to close the form. This was devloped from an MS Docs article on the matter as well as from some web searches on issues to further expand its functionality

function show-image{
    param (
    [string]$ImageName = '300Homer-Simpson-Doh45.png',
    [string]$Title='Homer',
    [string]$Text = 'Doh!' ,
    $Layout='none',
    $FontStyle='Italic'  # The textbox font style
)
    Add-Type -AssemblyName System.Windows.Forms

    $Form = New-Object system.Windows.Forms.Form
    $Form.Text = $Title
    ...
    ...
}

A Snippet of the show-image PS function

Usage

show-image <Path to the iamge file> <The form title used in Windowing> <Optional:Textbox at top to display>
For eaxple:
show-image 'whitelist-1.png' 'Configure Azure Sphere Learning Path Azure IoT App' ''

A sample
Using the function with no parameters


 TopicSubtopic
   
 This Category Links 
Category:Coding Index:Coding
  Next: > PowerShell
<  Prev:   PowerShell