{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[this doc on github](https://github.com/dotnet/interactive/tree/master/samples/notebooks/powershell)\n", "\n", "# Interact with Azure Cloud Shell from PowerShell notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Magic of the `Enter-AzShell` cmdlet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `Enter-AzShell` cmdlet is a new addition to the `Microsoft.DotNet.Interactive.PowerShell` module.\n", "Running it will authenticate you to Azure and establish a PowerShell cloud shell session for you.\n", "After that, all subsequent code submitted from the cells are executed in that cloud shell session.\n", "\n", "When you are done with the cloud shell, just type and run `exit` from a cell to end the session, and after that, all subsequent code submitted from the cells will again be executed using the local PowerShell kernel.\n", "\n", "Let's get started with some examples!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's list the available cmdlets from the `Microsoft.DotNet.Interactive.PowerShell`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "CommandType Name Version Source\n", "----------- ---- ------- ------\n", "Cmdlet Enter-AzShell 0.1.0 Microsoft.DotNet.Int…\n", "Cmdlet New-PlotlyChart 0.1.0 Microsoft.DotNet.Int…\n", "Cmdlet Out-Display 0.1.0 Microsoft.DotNet.Int…\n", "\n" ] } ], "source": [ "Get-Command -Module Microsoft.DotNet.Interactive.PowerShell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There you go! Now run `Enter-AzShell`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Authenticating with Azure...\n", "To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code BZU7QNJAY to authenticate.\n", "Requesting Cloud Shell...Succeeded.\n", "Connecting terminal...\n", "\n", "MOTD: Modules installed with 'Install-Module' are persisted across sessions\n", "\n", "\u001b[39;49m\u001b[93m\u001b[39;49m\u001b[93mVERBOSE: Authenticating to Azure ...\n", "\u001b[39;49m\u001b[39;49m\u001b[39;49m\u001b[93m\u001b[39;49m\u001b[93mVERBOSE: Building your Azure drive ...\n", "\n", "\u001b[92mWelcome to Azure Cloud Shell!\u001b[0m\n", "\u001b[92mSubmitted code will run in the Azure Cloud Shell, type 'exit' to quit.\u001b[0m\n", "\n" ] } ], "source": [ "Enter-AzShell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are connected to the PowerShell Azure cloud shell session,\n", "and the code submitted from cells will run in the cloud shell session.\n", "\n", "Let's run some commands." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cd Azure:\\\n", "dir\n", "\n", "\n", " Directory: Azure:\n", "\n", "\n", "Mode SubscriptionName SubscriptionId TenantId\n", "---- ---------------- -------------- -------- \n", "+ Visual Studio Ultimate with MSDN 99d1d4aa-4408-4117-b1fd-28a6a1bb10b0 453fb556-678d-4480-ad85…\n", "\n", "\n" ] } ], "source": [ "cd Azure:\\\n", "dir" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Get-AzLog -max 3\n", "\u001b[39;49m\u001b[93m\u001b[39;49m\u001b[93mWARNING: 6:21:06 AM - *** The namespace for all the model classes will change from Microsoft.Azure.Management.Monitor.Management.Models to Microsoft.Azure.Management.Monitor.Models in future releases.\n", "\u001b[39;49m\u001b[39;49m\u001b[39;49m\u001b[93m\u001b[39;49m\u001b[93mWARNING: 6:21:06 AM - *** The namespace for output classes will be uniform for all classes in future releases to make it independent of modifications in the model classes.\n", "\u001b[39;49m\u001b[39;49m\u001b[39;49m\u001b[93m\u001b[39;49m\u001b[93mWARNING: [Get-AzLog] Parameter deprecation: The DetailedOutput parameter will be deprecated in a future breaking change release.\n", "\u001b[39;49m\u001b[39;49m\n", "Authorization : \n", " Scope : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGrou\n", " ps/cloud-shell-storage-southcentralus/providers/Microsoft.Storage/storageAcc\n", " ounts/cs799d1d4aa4408x4117xb1f\n", " Action : Microsoft.Storage/storageAccounts/listKeys/action\n", " Role : \n", " Condition : \n", "Claims : \n", " aud : https://management.core.windows.net/\n", " iss : \n", " https://sts.windows.net/453fb556-678d-4480-ad85-0fb14b2ed2a4/\n", " iat : 1584511264\n", " nbf : 1584511264\n", " exp : 1584515164\n", " http://schemas.microsoft.com/claims/authnclassreference: 1\n", " aio : AUQAu/8OAAAAQLHHrgEYzU4CaDx5YQ8R8iM+XITd3eUDEiX/lCz8FMTkgzs\n", " DQf07c1a64hzcMfUV6AbHDi39vp1fII8rO8x9Ng==\n", " altsecid : 1:live.com:0006BFFD9555D217\n", " http://schemas.microsoft.com/claims/authnmethodsreferences: pwd\n", " appid : 245e1dee-74ef-4257-a8c8-8208296e1dfd\n", " appidacr : 0\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: \n", " rockyterminator@hotmail.com\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Wang\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Dongbo\n", " groups : b873ef20-4cb2-4ce6-93d9-6c3b44766ebb\n", " http://schemas.microsoft.com/identity/claims/identityprovider: live.com\n", " ipaddr : 50.46.216.48\n", " name : Dongbo Wang\n", " http://schemas.microsoft.com/identity/claims/objectidentifier: \n", " d8e84d9f-bdcb-4c74-aadf-b5601dad7649\n", " puid : 10037FFE8779A7C7\n", " http://schemas.microsoft.com/identity/claims/scope: user_impersonation\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: \n", " Y7EN1vBLk9nJZtlbpW2z9au8CV_OozYHzKKYS29guUY\n", " http://schemas.microsoft.com/identity/claims/tenantid: \n", " 453fb556-678d-4480-ad85-0fb14b2ed2a4\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: \n", " live.com#rockyterminator@hotmail.com\n", " uti : elkRbf4SsE6tUER3Rg5RAA\n", " ver : 1.0\n", " wids : 62e90394-69f5-4237-9190-012177145e10\n", "HttpRequest : \n", " ClientId : b2920582-7aea-4ebc-b50e-e3677cfd5ea4\n", " Method : POST\n", " Url : \n", " ClientIpAddress : 104.210.220.216\n", "Properties : \n", " statusCode : OK\n", " serviceRequestId: 5ab1fb4e-dab4-41a8-9062-6d667bb364fc\n", "Level : Informational\n", "SubmissionTimestamp : 3/18/2020 6:07:41 AM\n", "EventTimestamp : 3/18/2020 6:06:08 AM\n", "Caller : rockyterminator@hotmail.com\n", "Description : \n", "Id : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f/events/67c2e90c-a3cd-4912-b67c-ad0d2770b833/ticks/6372010\n", " 83682737399\n", "EventDataId : 67c2e90c-a3cd-4912-b67c-ad0d2770b833\n", "CorrelationId : bd2d95fa-4947-4d46-b87c-fb47a39995a1\n", "EventName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Category : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceGroupName : cloud-shell-storage-southcentralus\n", "ResourceProviderName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceId : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f\n", "ResourceType : \n", "OperationId : bd2d95fa-4947-4d46-b87c-fb47a39995a1\n", "OperationName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Status : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubStatus : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubscriptionId : 99d1d4aa-4408-4117-b1fd-28a6a1bb10b0\n", "TenantId : \n", "\n", "Authorization : \n", " Scope : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGrou\n", " ps/cloud-shell-storage-southcentralus/providers/Microsoft.Storage/storageAcc\n", " ounts/cs799d1d4aa4408x4117xb1f\n", " Action : Microsoft.Storage/storageAccounts/listKeys/action\n", " Role : \n", " Condition : \n", "Claims : \n", " aud : https://management.core.windows.net/\n", " iss : \n", " https://sts.windows.net/453fb556-678d-4480-ad85-0fb14b2ed2a4/\n", " iat : 1584511264\n", " nbf : 1584511264\n", " exp : 1584515164\n", " http://schemas.microsoft.com/claims/authnclassreference: 1\n", " aio : AUQAu/8OAAAAQLHHrgEYzU4CaDx5YQ8R8iM+XITd3eUDEiX/lCz8FMTkgzs\n", " DQf07c1a64hzcMfUV6AbHDi39vp1fII8rO8x9Ng==\n", " altsecid : 1:live.com:0006BFFD9555D217\n", " http://schemas.microsoft.com/claims/authnmethodsreferences: pwd\n", " appid : 245e1dee-74ef-4257-a8c8-8208296e1dfd\n", " appidacr : 0\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: \n", " rockyterminator@hotmail.com\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Wang\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Dongbo\n", " groups : b873ef20-4cb2-4ce6-93d9-6c3b44766ebb\n", " http://schemas.microsoft.com/identity/claims/identityprovider: live.com\n", " ipaddr : 50.46.216.48\n", " name : Dongbo Wang\n", " http://schemas.microsoft.com/identity/claims/objectidentifier: \n", " d8e84d9f-bdcb-4c74-aadf-b5601dad7649\n", " puid : 10037FFE8779A7C7\n", " http://schemas.microsoft.com/identity/claims/scope: user_impersonation\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: \n", " Y7EN1vBLk9nJZtlbpW2z9au8CV_OozYHzKKYS29guUY\n", " http://schemas.microsoft.com/identity/claims/tenantid: \n", " 453fb556-678d-4480-ad85-0fb14b2ed2a4\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: \n", " live.com#rockyterminator@hotmail.com\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " uti : elkRbf4SsE6tUER3Rg5RAA\n", " ver : 1.0\n", " wids : 62e90394-69f5-4237-9190-012177145e10\n", "HttpRequest : \n", " ClientId : b2920582-7aea-4ebc-b50e-e3677cfd5ea4\n", " Method : POST\n", " Url : \n", " ClientIpAddress : 104.210.220.216\n", "Properties : \n", "Level : Informational\n", "SubmissionTimestamp : 3/18/2020 6:07:41 AM\n", "EventTimestamp : 3/18/2020 6:06:08 AM\n", "Caller : rockyterminator@hotmail.com\n", "Description : \n", "Id : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f/events/30e4aad3-190f-419b-a9fc-957e3f6a419b/ticks/6372010\n", " 83682306219\n", "EventDataId : 30e4aad3-190f-419b-a9fc-957e3f6a419b\n", "CorrelationId : bd2d95fa-4947-4d46-b87c-fb47a39995a1\n", "EventName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Category : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceGroupName : cloud-shell-storage-southcentralus\n", "ResourceProviderName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceId : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f\n", "ResourceType : \n", "OperationId : bd2d95fa-4947-4d46-b87c-fb47a39995a1\n", "OperationName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Status : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubStatus : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubscriptionId : 99d1d4aa-4408-4117-b1fd-28a6a1bb10b0\n", "TenantId : \n", "\n", "Authorization : \n", " Scope : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGrou\n", " ps/cloud-shell-storage-southcentralus/providers/Microsoft.Storage/storageAcc\n", " ounts/cs799d1d4aa4408x4117xb1f\n", " Action : Microsoft.Storage/storageAccounts/listKeys/action\n", " Role : \n", " Condition : \n", "Claims : \n", " aud : https://management.core.windows.net/\n", " iss : \n", " https://sts.windows.net/453fb556-678d-4480-ad85-0fb14b2ed2a4/\n", " iat : 1584508258\n", " nbf : 1584508258\n", " exp : 1584512158\n", " http://schemas.microsoft.com/claims/authnclassreference: 1\n", " aio : AUQAu/8OAAAAmFG9k2+EKbEtTwkdNxZkvnvdRsamY2gJ2Rj8TR/mOFF1b4x\n", " TaCOFAwITpfvEaM1Ca0ZVlH+w5vRdPXQiS5BTcg==\n", " altsecid : 1:live.com:0006BFFD9555D217\n", " http://schemas.microsoft.com/claims/authnmethodsreferences: pwd\n", " appid : 245e1dee-74ef-4257-a8c8-8208296e1dfd\n", " appidacr : 0\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: \n", " rockyterminator@hotmail.com\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Wang\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Dongbo\n", " groups : b873ef20-4cb2-4ce6-93d9-6c3b44766ebb\n", " http://schemas.microsoft.com/identity/claims/identityprovider: live.com\n", " ipaddr : 50.46.216.48\n", " name : Dongbo Wang\n", " http://schemas.microsoft.com/identity/claims/objectidentifier: \n", " d8e84d9f-bdcb-4c74-aadf-b5601dad7649\n", " puid : 10037FFE8779A7C7\n", " http://schemas.microsoft.com/identity/claims/scope: user_impersonation\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: \n", " Y7EN1vBLk9nJZtlbpW2z9au8CV_OozYHzKKYS29guUY\n", " http://schemas.microsoft.com/identity/claims/tenantid: \n", " 453fb556-678d-4480-ad85-0fb14b2ed2a4\n", " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: \n", " live.com#rockyterminator@hotmail.com\n", " uti : Y0CcrLeo4Eawa1d3T2NYAA\n", " ver : 1.0\n", " wids : 62e90394-69f5-4237-9190-012177145e10\n", "HttpRequest : \n", " ClientId : 83e4087b-8b96-4bae-98f6-04eb92f5e75b\n", " Method : POST\n", " Url : \n", " ClientIpAddress : 104.210.220.216\n", "Properties : \n", " statusCode : OK\n", " serviceRequestId: 75a6f4e5-8092-4dd2-8760-5674fc9dfe05\n", "Level : Informational\n", "SubmissionTimestamp : 3/18/2020 5:17:38 AM\n", "EventTimestamp : 3/18/2020 5:16:02 AM\n", "Caller : rockyterminator@hotmail.com\n", "Description : \n", "Id : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f/events/3c427e2a-cc8f-4552-a7fa-dec7c28ab01f/ticks/6372010\n", " 53623045240\n", "EventDataId : 3c427e2a-cc8f-4552-a7fa-dec7c28ab01f\n", "CorrelationId : 652ebf84-1b93-46cb-a298-3218e4e59493\n", "EventName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Category : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceGroupName : cloud-shell-storage-southcentralus\n", "ResourceProviderName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "ResourceId : /subscriptions/99d1d4aa-4408-4117-b1fd-28a6a1bb10b0/resourceGroups/cloud-she\n", " ll-storage-southcentralus/providers/Microsoft.Storage/storageAccounts/cs799d\n", " 1d4aa4408x4117xb1f\n", "ResourceType : \n", "OperationId : 652ebf84-1b93-46cb-a298-3218e4e59493\n", "OperationName : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "Status : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubStatus : Microsoft.Azure.Management.Monitor.Models.LocalizableString\n", "SubscriptionId : 99d1d4aa-4408-4117-b1fd-28a6a1bb10b0\n", "TenantId : \n", "\n", "\n", "\n" ] } ], "source": [ "Get-AzLog -max 3" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hostname\n", "cc-b3aa132e-9d5f76c9-t47t6\n", "\n" ] } ], "source": [ "hostname" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you are done with using the cloud shell session, run `exit` to end the session." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[92mAzure Cloud Shell session ended.\u001b[0m\n", "\u001b[92mSubmitted code will run in the local PowerShell sub kernel.\u001b[0m\n" ] } ], "source": [ "exit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, code submitted from cells will again be executed by the local PowerShell kernel." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my-Air.home\n" ] } ], "source": [ "hostname" ] } ], "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": 2 }