{ "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": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[94m\u001b[0m\n", "\u001b[94mName Value\u001b[0m\n", "\u001b[94m---- -----\u001b[0m\n", "\u001b[94mPSVersion 7.0.0\u001b[0m\n", "\u001b[94mPSEdition Core\u001b[0m\n", "\u001b[94mGitCommitId 7.0.0\u001b[0m\n", "\u001b[94mOS Darwin 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST…\u001b[0m\n", "\u001b[94mPlatform Unix\u001b[0m\n", "\u001b[94mPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\u001b[0m\n", "\u001b[94mPSRemotingProtocolVersion 2.3\u001b[0m\n", "\u001b[94mSerializationVersion 1.1.0.1\u001b[0m\n", "\u001b[94mWSManStackVersion 3.0\u001b[0m\n", "\u001b[94m\u001b[0m\n" ] } ], "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": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[94;47mSomething to think about ...\u001b[0m\n" ] } ], "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": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[93mWARNING: Warning\u001b[0m\n", "\u001b[93mVERBOSE: Verbose\u001b[0m\n", "\u001b[93mDEBUG: Debug\u001b[0m\n" ] } ], "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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[91mHello \u001b[0m\u001b[94mWorld!\u001b[0m\n" ] } ], "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": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[95mPowerShell credential request\u001b[0m\n", "\u001b[94mEnter your credentials.\u001b[0m\n", "User: Joe\n", "Password for user Joe: ········\n", "\n", "\u001b[94mJoe, password received!\u001b[0m\n" ] } ], "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": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[93mVERBOSE: Ask for name\u001b[0m\n", "What's your name? : Joe\n", "\u001b[34mGreetings, Joe!\u001b[0m\n" ] } ], "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": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "token? : ········\n", "\u001b[94mSystem.Security.SecureString\u001b[0m\n" ] } ], "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": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[95mConfirm\u001b[0m\n", "\u001b[94mThe term 'nonExist' is not recognized as the name of a cmdlet, function, script file, or operable program.\n", "Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\u001b[0m\n", "[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help\u001b[94m(default is 'Y')\u001b[0mSelect: Y\n", "\u001b[91mGet-Command: \u001b[91mThe term 'nonExist' is not recognized as the name of a cmdlet, function, script file, or operable program.\n", "Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\u001b[0m\n" ] } ], "source": [ "Get-Command nonExist -ErrorAction Inquire" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "9. _You can user the mandatory parameter prompts:_" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[95mcmdlet Write-Output at command pipeline position 1\u001b[0m\n", "\u001b[94mSupply values for the following parameters:\u001b[0m\n", "InputObject: Something to output\n", "\u001b[94mI received 'Something to output'\u001b[0m\n" ] } ], "source": [ "Write-Output | ForEach-Object { \"I received '$_'\" }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "10. _Of course, pipeline streaming works:_" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[94m\u001b[0m\n", "\u001b[94m NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName\u001b[0m\n", "\u001b[94m ------ ----- ----- ------ -- -- -----------\u001b[0m\n", "\u001b[94m 0 0.00 0.00 0.00 0 …39 \u001b[0m\n", "\u001b[94m 0 0.00 0.00 0.00 1 1 \u001b[0m\n", "\u001b[94m 0 0.00 0.00 0.00 109 109 \u001b[0m\n", "\u001b[94m 0 0.00 0.00 0.00 110 110 \u001b[0m\n", "\u001b[94m 0 0.00 0.00 0.00 113 113 \u001b[0m\n", "\u001b[94m\u001b[0m\n" ] } ], "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": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[93mVERBOSE: working hard!!!\u001b[0m\n", "\u001b[94mSomething to output\u001b[0m\n" ] } ], "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 }