powershell脚本忽略输入

时间:2017-09-23 18:49:35

标签: powershell powershell-v2.0

enter image description here

我正在寻找默认安装。即使我已经提到了bypass和\ 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):

2 个答案:

答案 0 :(得分:2)

正在运行的脚本中包含read-host需要输入。除了使用sendkeys或第三方应用程序(如AutoHotKey)之外,没有任何漂亮的方法。我唯一能想到的就是通过在non-interactive mode中运行PowerShell来压制Read-Host

  

不向用户显示交互式提示。

这有副作用,会触发错误。请考虑以下脚本。

$var = "Original"
$result = read-host -Prompt "Push Enter or type and such"
if($result){$var=$result}
$var

将变量设置为字符串值并提示用户更改它。显示结果,无论用户输入是否为空。

现在以非交互模式运行...

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逻辑并完全避免这个问题。

答案 1 :(得分:-1)

像那样运行:

powershell {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -File "C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1"}

<强>解释
要发送输入键击,您可以通过以下代码执行此操作:

$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys('~')

在解决方案中,我在运行脚本之前发送enter键,然后它将接收并继续。

<强>参考:
How to perform keystroke inside powershell?