PowerShell scripts are often used by PowerShellers or other tech folk. Sometimes though, you may want a less tech-savvy group of people to use your PowerShell script. If this is the case you’re going to need something a bit more user-friendly and fool-proof.
In this blog post (with my first EVER video tutorial!) I’ll show you how to give your users an interactive and user-friendly interface to your scripts: Microsoft PowerApps.
You’ll need a PowerApps trial or license to follow this tutorial.
By using an Azure AD group, that was created to publish your app to when it is ready for distribution, your users will automatically be granted the correct permissions in Azure to start a runbook. This is because PowerApps does not use its own identity when interacting with connectors, it impersonates the user’s own identity.
The source code for the runbook is:
Param( [String]$searchParameter ) $uri = "https://techcommunity.microsoft.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&q=$searchParameter" Start-Sleep -s 2 Write-Output "Runbook started, searching for $searchParameter..." $res = Invoke-WebRequest -Uri $uri -UseBasicParsing -Method GET -ErrorAction Stop Start-Sleep -s 2 Write-Output "found some results, analyzing...." $firstHit = $res.Links | where-object {$_.outerHTML -like "*lia-link-navigation*" -and $_.href -like "/t5/*"} | select href -First 1 -ExpandProperty href $firstHit = "https://techcommunity.microsoft.com/$firstHit" Start-Sleep -s 2 Write-Output "Retrieving first 100 characters of first result..." $res = Invoke-WebRequest -Uri $firstHit -UseBasicParsing -Method GET -ErrorAction Stop $excerpt = $res.Content.Substring(($res.Content.IndexOf("class=`"lia-message-body-content`"")+64),100) -Replace('<[^>]+>','') Start-Sleep -s 2 Write-Output "Result:" Write-Output $excerpt write-Output "" write-Output "" write-Output "" write-Output "source: $firstHit"
The app screen’s OnStart property’s function is:
Set(runbookOutput,Blank());Set(runbookJobId,Blank());Set(runbookActive,false);Set(runbookResult,Blank())
The search button’s function is:
Set(runbookResult,Blank());Set(runbookOutput,Blank());Set(runbookJobId,Blank());Set(runbookActive,true);Set(runbookJobId,'new-searchQuery'.Run(TextInput2.Text).jobid)
The status label’s function is:
If(IsBlank(runbookResult) && runbookActive = false," ",If(runbookActive,"Please wait for job to complete…",Concatenate("Job result: ",runbookResult)))
The timer OnTimerStart function is:
If(runbookActive && Len(runbookJobId) > 5,Set(runbookOutput,'get-searchQueryOutput'.Run(runbookJobId).joboutput))
The timer OnTimerEnd function is:
If(runbookActive && Len(runbookJobId) > 5,Set(runbookResult,'get-searchQueryStatus'.Run(runbookJobId).jobstatus));If(runbookResult = "Completed" Or runbookResult = "Suspended" Or runbookResult = "Stopped",Set(runbookActive,false));
That’s it. Hope you enjoy creating your own apps, let us know how you get on in the comments – or if you have any questions.