I’ve put together a PowerShell Universal Notebook. If you have .NET Interactive Notebooks installed in VS Code, you’ll be able to open and run it. Just save the script as a .dib
file.
#!markdown
# Download and Start PowerShell Universal
The following command installs the `Universal` module and then downloads and starts the PowerShell Universal server. If you have the server already installed, this will just start the server. This example stores the configuration data in a temporary location.
#!pwsh
$Module = Import-Module Universal -PassThru -ErrorAction SilentlyContinue
if (-not $module) {
Install-Module Universal -Scope CurrentUser -Force -WarningAction SilentlyContinue
}
$Path = Join-Path $env:LOCALAPPDATA "universal-notebook"
if (-not (Test-Path $Path))
{
Install-PSUServer -Latest -Path $Path -ErrorAction SilentlyContinue
}
$TempStorage = Join-Path ([IO.Path]::GetTempPath()) (New-GUID)
$Env:Data__RepositoryPath = Join-Path $TempStorage "repository"
$Env:Data__ConnectionString = Join-Path $TempStorage "database.db"
$Universal = Start-PSUServer
#!markdown
# Connect to PowerShell Universal
The following generates an app token and connects the `Universal` module to the server.
#!pwsh
Invoke-RestMethod http://localhost:5000/api/v1/signin -Method Post -Body (@{
Username = "admin"
Password = "admin"
} | ConvertTo-Json) -SessionVariable Session -ContentType "application/json" | Out-Null
$AppToken = (Invoke-RestMethod http://localhost:5000/api/v1/apptoken/grant -Method GET -WebSession $Session).Token
Connect-PSUServer -AppToken $AppToken -ComputerName http://localhost:5000
#!markdown
# Create an API
The following creates an API to return some data.
#!pwsh
New-PSUEndpoint -Url "/data" -Method GET -Endpoint {
Get-Service | Select-Object Name, Status -First 10
}
#!markdown
We can use `Invoke-RestMethod` to call our new endpoint.
#!pwsh
Invoke-RestMethod http://localhost:5000/data
#!markdown
# Create a Script
The following creates a new script in PowerShell Universal and executes it. You can view the job by logging in to the admin console. You can use the user name `admin` with any password.
[PowerShell Universal Admin Console](http://localhost:5000)
#!pwsh
New-PSUScript -Name 'Script.ps1' -ScriptBlock {
Start-Process Notepad
}
Invoke-PSUScript -Script 'Script.ps1'
#!markdown
# Create a Dashboard
The following creates a new dashboard. You can view the dashboard by [clicking here](http://localhost:5000/dashboard).
#!pwsh
$Framework = Get-PSUDashboardFramework
New-PSUDashboard -Name 'Dashboard' -BaseUrl '/dashboard' -AutoDeploy -Content {
New-UDDashboard -Content {
New-UDTypography -Text 'Hello, world!'
}
} -Framework $Framework
#!markdown
# Stop PowerShell Universal
The following command stops PowerShell Universal and cleans up the temporary data folder.
#!pwsh
$Universal | Stop-Process
Start-Sleep 5
Remove-Item $TempStorage -Recurse -Force -ErrorAction SilentlyContinue