Return to site

Powershell Press Any Key To Continue

broken image


How do you do a ‘Pause' with PowerShell 2.0? (5 answers) Closed 4 years ago. According to Microsoft's documentation, read-host lets the user type some input, and then press enter to continue. Not exactly the correct behavior if you want to have 'Press any key to continue'. Read -p 'Press enter to continue' As mentioned in the comments above, this command does actually require the user to press enter; a solution that works with any key would be: read -n 1 -s -r -p 'Press any key to continue' Explanation by Rayne and wchargin-n defines the required character count to stop reading-s hides the user's input. Solution 2: Works in PowerShell ISE Here is a simple way to pause the script execution and wait for the user to press the ENTER key to continue. This works for both the PowerShell commandline console as well as in the PowerShell ISE. I've just had two occurrences of windows 7 stalling an execution until I hit enter on the command prompt/powershell. Afterward execution seems to continue as expected. First was an batch file of copy commands like. Copy //host/file2010-1.xml localfolder/01/ copy //host/file 2010-2.xml localfolder/02/ One file seemed to be taking ages.

If you are using a FIDO2 Security Key, such as a YubiKey, you may have run into the issue that you cannot use it to authenticate with your Azure AD account using PowerShell:

As you can see, the needed Sign in with a security key option is missing here.

This is because PowerShell still uses the older Active Directory Authentication Library (ADAL) when prompting for Azure AD credentials. That login prompt is actually rendered using Internet Explorer, and IE will likely never have support for WebAuthN, the protocol that FIDO2 logon requires.

So we have four options:

  • Wait until PowerShell moves from ADAL to MSAL, and sign in prompts are rendered by a modern browser that supports WebAuthN.
  • Wait until each PowerShell Module you need starts supporting its own implementation of modern authentication to Azure AD.
  • Use Cloud Shell, where you can run PowerShell directly in your browser:http://shell.azure.com/powershell

    This option works with FIDO2, but a web-based shell has its limitations.

  • Use Device Authorization Grant Flow to login.
  • This post explains the last option.

    What is Device Authorization Grant Flow

    The Device authorization grant flow is usually used when you need to sign in on 'input-constrained devices', such as IoT devices and printers. In this case, we can view PowerShell as a 'device'. The sign in flow is initiated on the device, but the user needs to visit a web page (on any device with a browser that hopefully supports WebAuthN) to complete the sign in. Once the user has signed in, the device (or PowerShell window) can get the needed access tokens and refresh tokens.

    Powershell wait for key

    Initiate the Device Authorization Grant Flow

    Run this code in the PowerShell window you want to sign in to Azure AD:

    Note: You do not need to register any new app in Azure AD for this to work since we are using the well-known ClientID for Azure AD PowerShell. You do not have to add any custom values for your tenant either, since we use the Common endpoint. This means that you will automatically be redirected to the tenant the user belongs to when signing in.

    A code will be shown that you need to enter at the following web page to continue the sign in:

    Besides https://microsoft.com/devicelogin, you can also use http://aka.ms/devicelogin. Both will redirect you to https://login.microsoftonline.com/common/oauth2/deviceauth.

    Enter the code in the prompt:

    As you can see, we are now signing in on a remote device or service.

    Powershell Press Any Key To Continue Ise

    Be aware that this sign in method can be misused in phishing attempts. Only enter codes you generated yourself!

    You can sign in using your regular account name and password, but to sign in using a FIDO2 key, click on Sign-in options:

    Now we can use our FIDO2 key to authenticate:

    Once authentication is successful, you can close the page in the web browser. The next step (obtaining tokens) will happen in the PowerShell window:

    Obtain the tokens

    Again, no customization is needed for this script block. We are re-using the device_code from the DeviceCodeRequest we made earlier.

    You now have a valid access token in the variable $Token that can be used to authenticate when using Connect-AzureAD. Note that the variable $TokenRequest also contains refresh_token and id_token, if you want to use them.

    Connect to Azure AD

    When using the Connect-AzureAD cmdlet with an access token, you also need to specify the username you used to authenticate and the TenantId. You can find your TenantID using PowerShell:

    or by going to :

    Powershell Press Any Key To Continue Or Exit

    Powershell Press Any Key To Continue

    Now we are ready to connect to Azure AD:

    Now you should be able to run commands from that module, like this one to get the first group:

    What if I need to use the Microsoft Graph?

    That will also work, but you need to change $Resource variable in the first script block to the Service Endpoint of Microsoft Graph ('https://graph.microsoft.com/”) and repeat the process.

    Then you should be able to run queries against the Microsoft Graph, like this one to get the first group:

    How about Exchange Online?

    For this to work, you need to change both the $Resource and the $ClientID variables in the first script block to:

    When you sign in, you will see that you are signing in to Microsoft Exchange Online Remote PowerShell:

    After you obtain the token you need to create a new credential object based on your username and the token:

    Now you can connect to Exchange Online using these commands:

    Thanks

    Powershell Press Any Key To Continue

    Powershell Press Any Key To Continue Enter

    Big thanks to Stefan Schörling (@stefanschorling) for pointing me in the right direction and to Simon Wahlin for his writeup about Device login flow for MS Graph access.

    Context:This morning I was standing with Ben Gelens at the coffee machine since we're both at the same customer.
    Problem:Someone walked by and asked me if there is a way for PowerShell to ask for a key to press before it continues with the script… like a pause.
    Together we replied: Read-Host. However, it seems that this scripter didn't want a pop-up… which is what Read-Host gives… in PowerShell v2 at least.
    In PowerShell 5 (which is what I've tested) it does not provide a pop-up. It also isn't exactly like Press Any Key To Continue since it will only continue after an ENTER.
    Solution: Upgrade to the latest and greatest version of Windows (or just upgrade WMF/PowerShell).
    Workaround: See below.

    Inside a PowerShell prompt you can do the following:

    This is maybe a bit much for beginning scripters, so here's an simpler version:

    Powershell Press Any Key To Continue

    Initiate the Device Authorization Grant Flow

    Run this code in the PowerShell window you want to sign in to Azure AD:

    Note: You do not need to register any new app in Azure AD for this to work since we are using the well-known ClientID for Azure AD PowerShell. You do not have to add any custom values for your tenant either, since we use the Common endpoint. This means that you will automatically be redirected to the tenant the user belongs to when signing in.

    A code will be shown that you need to enter at the following web page to continue the sign in:

    Besides https://microsoft.com/devicelogin, you can also use http://aka.ms/devicelogin. Both will redirect you to https://login.microsoftonline.com/common/oauth2/deviceauth.

    Enter the code in the prompt:

    As you can see, we are now signing in on a remote device or service.

    Powershell Press Any Key To Continue Ise

    Be aware that this sign in method can be misused in phishing attempts. Only enter codes you generated yourself!

    You can sign in using your regular account name and password, but to sign in using a FIDO2 key, click on Sign-in options:

    Now we can use our FIDO2 key to authenticate:

    Once authentication is successful, you can close the page in the web browser. The next step (obtaining tokens) will happen in the PowerShell window:

    Obtain the tokens

    Again, no customization is needed for this script block. We are re-using the device_code from the DeviceCodeRequest we made earlier.

    You now have a valid access token in the variable $Token that can be used to authenticate when using Connect-AzureAD. Note that the variable $TokenRequest also contains refresh_token and id_token, if you want to use them.

    Connect to Azure AD

    When using the Connect-AzureAD cmdlet with an access token, you also need to specify the username you used to authenticate and the TenantId. You can find your TenantID using PowerShell:

    or by going to :

    Powershell Press Any Key To Continue Or Exit

    Now we are ready to connect to Azure AD:

    Now you should be able to run commands from that module, like this one to get the first group:

    What if I need to use the Microsoft Graph?

    That will also work, but you need to change $Resource variable in the first script block to the Service Endpoint of Microsoft Graph ('https://graph.microsoft.com/”) and repeat the process.

    Then you should be able to run queries against the Microsoft Graph, like this one to get the first group:

    How about Exchange Online?

    For this to work, you need to change both the $Resource and the $ClientID variables in the first script block to:

    When you sign in, you will see that you are signing in to Microsoft Exchange Online Remote PowerShell:

    After you obtain the token you need to create a new credential object based on your username and the token:

    Now you can connect to Exchange Online using these commands:

    Thanks

    Powershell Press Any Key To Continue Enter

    Big thanks to Stefan Schörling (@stefanschorling) for pointing me in the right direction and to Simon Wahlin for his writeup about Device login flow for MS Graph access.

    Context:This morning I was standing with Ben Gelens at the coffee machine since we're both at the same customer.
    Problem:Someone walked by and asked me if there is a way for PowerShell to ask for a key to press before it continues with the script… like a pause.
    Together we replied: Read-Host. However, it seems that this scripter didn't want a pop-up… which is what Read-Host gives… in PowerShell v2 at least.
    In PowerShell 5 (which is what I've tested) it does not provide a pop-up. It also isn't exactly like Press Any Key To Continue since it will only continue after an ENTER.
    Solution: Upgrade to the latest and greatest version of Windows (or just upgrade WMF/PowerShell).
    Workaround: See below.

    Inside a PowerShell prompt you can do the following:

    This is maybe a bit much for beginning scripters, so here's an simpler version:

    </div><table><tbody><tr><td><div><span>[</span><span>void</span><span>]</span><span>(</span><span>$</span><span>Host</span><span>.</span><span>UI</span><span>.</span><span>RawUI</span><span>.</span><span>ReadKey</span><span>(</span><span>'NoEcho,IncludeKeyDown'</span><span>)</span><span>)</span></div></td></tr></tbody></table><p>However, if you were to execute this in <em>PowerShell ISE</em>, you'll get hit by an error:</p><h3 id='powershell-press-any-key-to-continue-access'>Powershell Press Any Key To Continue Access</h3><div><textarea wrap='soft' readonly='>Exception calling 'ReadKey' with '1' argument(s): 'The method or operation is not implemented.' At line:1 char:1 + $Host.UI.RawUI.ReadKey( [System.Management.Automation.Host.ReadKeyOpt ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotImplementedException
    2
    4
    6
    Exception calling'ReadKey'with'1'argument(s):'The method or operation is not implemented.'
    +$Host.UI.RawUI.ReadKey([System.Management.Automation.Host.ReadKeyOpt...
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    +CategoryInfo:NotSpecified:(:)[],MethodInvocationException

    It seems that the ReadKey method isn't implemented in the host of Windows PowerShell ISE
    So here's some code that will offer a bit like the same functionality but in ISE (instead of any key, only ENTER will work):

    Yes, Read-Host.
    All my endeavours and investigations have led me to one single conclusion: Within Windows PowerShell ISE there is only one way to get functionality that resembles the Press Any Key To Continue behaviour and that is by using Read-Host.
    This is because the console in ISE isn't a console. It resembles one, but it isn't the PowerShell console.
    I don't know if the terminology is correct, but I think it's something that emulates a PowerShell console 🙂

    Powershell 5 Press Any Key To Continue

    Hope you find this information useful.





    broken image