首页 > 解决方案 > "ReadKey" Error - suddenly appears working on powershell script

问题描述

I have the following script which until now worked just fine.

# Mailbox Migration Script
$ErrorActionPreference = "stop"
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

#$UserCredential = Get-Credential -Message "`n Please supply your O365 credentials using (Example: user@corp.mycorp.com): `n "
#$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection


#Import-PSSession $Session

#Choose, USERNAME related to mailbox:
Write-Host "`n ATTENTION : THIS SCRIPT WILL SHUT DOWN IF YOU ENTER WRONG MAILBOX INFO `n OR THE MAILBOX HAS ALREADY BEEN MIGRATED." -for Red;  
$username = Read-Host -Prompt "`n Please provide AD-USERNAME to Migrate";
Write-Host "`n The chosen user is:";
Write-Host "`n " $username -for Yellow;
Write-Host "`n Press ENTER key to confirm and continue. `n Press CTRL+C to cancel";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

Now after running this, i get an error which crashes the script.

Exception calling "ReadKey" with "1" argument(s): "The method or operation is not 
implemented."
At C:\Users\username\Desktop\PowerShell\Migration-prompt.ps1:19 char:1
+ $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordExcepti 
   on
    + FullyQualifiedErrorId : NotImplementedException

I imagine the issue should be somewhere on this line:

$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

Your help would be mostly appreciated on fixing this.

Thank you very much !

标签: powershell

解决方案


Replace the troubled line with this one.

$null = Read-Host

If you want to troubleshoot the issue instead, start with the error. The method is not implemented. The PowerShell host has changed in some fashion. What is the output for $host?


推荐阅读