{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[this doc on github](https://github.com/dotnet/interactive/tree/master/samples/notebooks/powershell)\n", "\n", "# Interactive Host Experience in PowerShell notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The PowerShell notebook provides a rich interactive experience through its host.\n", "The following are some examples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. _You can set the foreground and background colors for the output. The code below sets the foreground color to `Blue`, and you can see the output is rendered in blue afterwards:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "$host.UI.RawUI.ForegroundColor = [System.ConsoleColor]::Blue\n", "$PSVersionTable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. _You can write to the host with specified foreground and background colors_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Write-Host \"Something to think about ...\" -ForegroundColor Blue -BackgroundColor Gray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. _Warning, Verbose, and Debug streams are rendered with the expected color:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Write-Warning \"Warning\"\n", "Write-Verbose \"Verbose\" -Verbose\n", "Write-Debug \"Debug\" -Debug" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. _You can use `Write-Host -NoNewline` as expected:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Write-Host \"Hello \" -NoNewline -ForegroundColor Red\n", "Write-Host \"World!\" -ForegroundColor Blue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. _You can read from user for credential:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "$cred = Get-Credential\n", "\"$($cred.UserName), password received!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. _You can read from user for regular input:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Write-Verbose \"Ask for name\" -Verbose\n", "\n", "$name = Read-Host -Prompt \"What's your name? \"\n", "Write-Host \"Greetings, $name!\" -ForegroundColor DarkBlue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7. _You can read from user for password:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Read-Host -Prompt \"token? \" -AsSecureString" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "8. _You can use the multi-selection when running commands:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Get-Command nonExist -ErrorAction Inquire" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "9. _You can user the mandatory parameter prompts:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Write-Output | ForEach-Object { \"I received '$_'\" }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "10. _Of course, pipeline streaming works:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "Get-Process | select -First 5 | % { start-sleep -Milliseconds 300; $_ }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "11. _Progress bar rendering works as expected:_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "pwsh" } }, "outputs": [], "source": [ "## Demo the progress bar\n", "For ($i=0; $i -le 100; $i++) {\n", " Write-Progress -Id 1 -Activity \"Parent work progress\" -Status \"Current Count: $i\" -PercentComplete $i -CurrentOperation \"Counting ...\"\n", " \n", " For ($j=0; $j -le 10; $j++) {\n", " Start-Sleep -Milliseconds 5\n", " Write-Progress -Parent 1 -Id 2 -Activity \"Child work progress\" -Status \"Current Count: $j\" -PercentComplete ($j*10) -CurrentOperation \"Working ...\"\n", " }\n", " \n", " if ($i -eq 50) {\n", " Write-Verbose \"working hard!!!\" -Verbose\n", " \"Something to output\"\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] } ], "metadata": { "kernelspec": { "display_name": ".NET (PowerShell)", "language": "PowerShell", "name": ".net-powershell" }, "language_info": { "file_extension": ".ps1", "mimetype": "text/x-powershell", "name": "PowerShell", "pygments_lexer": "powershell", "version": "7.0" } }, "nbformat": 4, "nbformat_minor": 4 }