我正在寻找默认安装。即使我提到了绕过和\quiet 命令,脚本仍然需要输入才能继续...我也尝试过不受限制,并尝试通过传递 -ArgumentList powershell -ExecutionPolicy ByPass -File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1\quiet -ArgumentList "computername=ABCHOSTNAME"
但仍然停止并要求输入..请建议
PS C:\orchestrator\AddServerRolesAndFeatures> powershell -ExecutionPolicy ByPass -File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 \quiet
Your OS version is:Windows Server 2016 Datacenter
Computername (Press Enter for current computer - ABCHOSTNAME):
最佳答案
正在运行的脚本中有read-host
,它需要输入。除了使用 sendkeys 或 AutoHotKey 等第三方应用程序之外,实际上没有任何漂亮的方法可以解决这个问题。我唯一能想到的就是通过在 non-interactive mode 中运行 PowerShell 来抑制 Read-Host其中
Does not present an interactive prompt to the user.
这有一个副作用,会触发错误。考虑以下脚本。
$var = "Original"
$result = read-host -Prompt "Push Enter or type and such"
if($result){$var=$result}
$var
将变量设置为字符串值并提示用户更改它。无论用户输入 null 还是什么都不输入,都会显示结果。
现在以非交互模式运行它...
PS D:\Temp\PSH> powershell.exe -Noninteractive -file .\skipReadHost.ps1
powershell.exe : read-host : Windows PowerShell is in NonInteractive mode. Read and Prompt
At line:1 char:1
+ powershell.exe -Noninteractive -file .\skipReadHost.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (read-host : Win...ead and Prompt :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
functionality is not available.
At D:\Temp\PSH\skipReadHost.ps1:2 char:11
+ $result = read-host -Prompt "Push Enter or type and such"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Read-Host], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ReadHostCommand
Original
请注意,最后字符串以其第一个状态出现。这一切都取决于脚本如何处理这个被跳过的提示。
从表面上看,您还可以从脚本中删除 read-host
逻辑,从而完全避免此问题。
关于powershell脚本忽略回车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46383151/