powershell - "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 !
解决方案
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
?
推荐阅读
- wordpress - 删除特定页面的 Jetpack 的 Open Graph 元标记
- c# - 在 DataGridView 中使用嵌套列表作为数据源的列表
- sql - Column' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中
- erlang - elixir/erlang 中的 des-ecb 加密
- visual-studio-code - 在 Visual stuido 行中保存 html 文件时搞砸了
- javascript - 没有 async/await 就不会保存缓存中的更改
- hashcode - 为什么两个具有相似标签的 NodeList 不相等?
- blazor - 更新 Visual Studio 2019 预览后出错
- android - 即使在 postValue() Kotlin 之后 MutableLiveData ArrayList 也是空的
- scala - 在 VScode 中设置 Scala(metals) 时出现问题