powershell脚本忽略输入
powershell script to ignore enter
我正在寻找默认安装。尽管我已经提到绕过和 \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):
运行 就是这样:
powershell {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -File "C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1"}
解释:
要发送回车键,您可以使用以下代码:
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys('~')
在解决方案中,我在您 运行 脚本之前发送回车键,然后它将接收并继续。
参考:
How to perform keystroke inside powershell?
运行ning 脚本中有 read-host
,其中 需要输入 。除了使用 sendkeys 或第三方应用程序(如 AutoHotKey)之外,没有什么好的解决方法。我唯一能想到的就是在 non-interactive mode which
中通过 运行ning 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
逻辑并完全避免此问题。
我正在寻找默认安装。尽管我已经提到绕过和 \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):
运行 就是这样:
powershell {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -File "C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1"}
解释:
要发送回车键,您可以使用以下代码:
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys('~')
在解决方案中,我在您 运行 脚本之前发送回车键,然后它将接收并继续。
参考:
How to perform keystroke inside powershell?
运行ning 脚本中有 read-host
,其中 需要输入 。除了使用 sendkeys 或第三方应用程序(如 AutoHotKey)之外,没有什么好的解决方法。我唯一能想到的就是在 non-interactive mode which
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
逻辑并完全避免此问题。