2

enter image description here

I am looking for the default installation. Even though I have mentioned bypass and \quiet command still script require an enter to proceed further...I have also tried with unrestricted and also tried by passing -ArgumentList powershell -ExecutionPolicy ByPass -File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 \quiet -ArgumentList "computername=ABCHOSTNAME" but still stops and ask to enter ..Please advice

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):
4
  • If the script requires an enter, you should post the script, not how you invoke it. Commented Sep 23, 2017 at 19:22
  • Thanks for the response !!! Yes, the script requires enter. I cannot change the script , I am sharing you the snippet of it.. Commented Sep 23, 2017 at 19:44
  • 2
    If you are going to post code. Post the actual code. Not a screen shot. Makes it easier for people to help you. I don't see why you can't change the script. Commented Sep 23, 2017 at 20:14
  • "I cannot change the script" - yes you can. Make a copy and remove the requirement for the Enter keystroke. If it was intended to be automated, it should not require user interaction. Commented Sep 24, 2017 at 17:28

2 Answers 2

3

The running script has read-host in it which requires input by design. There are not really any pretty ways around it short of using sendkeys or third party apps like AutoHotKey. Only thing I could think is to surpress Read-Host by running PowerShell in non-interactive mode which

Does not present an interactive prompt to the user.

That has a side effect where an error will be triggered. Consider the following script.

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

Set a variable to a string value and prompt the user to change it. Display the results whether the user enters null or nothing.

Now run that in noninteractive mode...

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

Notice at the end the string came out with its first state. This all depends on how the script handles this prompt being skipped.

From the looks of it you could also just remove the read-host logic from the script and avoid this issue altogether.

Sign up to request clarification or add additional context in comments.

2 Comments

PS C:\> powershell.exe -Noninteractive -file C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 AuthorizationManager check failed. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess
Need you help as this command is running fine when I run manually but vis powershell script its not working.powershell -ExecutionPolicy RemoteSigned -noprofile -Noninteractive {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -ExecutionPolicy ByPass -File "C:\WINDOWS\Temp\InstallRolesAndFeatures.ps1"}
0

Run it like that:

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

Explanation:
In order to send enter key stroke you can do it by this code:

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

In the solution I send the enter keystroke before you run the script and then it will receive it and continue.

Reference:
How to perform keystroke inside powershell?

13 Comments

So flag as a duplicate then?
File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 cannot be loaded because you opted not to run this software now. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess
@gary try powershell -ExecutionPolicy ByPass {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -ExecutionPolicy ByPass -File "C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1"}
PS C:\> powershell -ExecutionPolicy ByPass {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -ExecutionPolicy ByPass -File "C:\orchestrator\AddServerRolesAndFea tures\InstallRolesAndFeatures.ps1"} For Windows Server 2012/2016 make sure that you have RolesAndFeatures.xml in the current folder Press Enter to continue...: Your OS version is:Windows Server 2016 Datacenter Computername (Press Enter for current computer - EABCHOSTNAME):
C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1?[D] Do not run [R] Run once [S] Suspend [?] Help (default is "D"): File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 cannot be loaded because you opted not to run this software now. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.