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):
Matt
  • 45,022
  • 8
  • 78
  • 119
gary
  • 157
  • 1
  • 3
  • 15
  • If the script requires an enter, you should post the script, not how you invoke it. – vrdse Sep 23 '17 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.. – gary Sep 23 '17 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. – Matt Sep 23 '17 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. – Bill_Stewart Sep 24 '17 at 17:28

2 Answers2

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.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • PS C:\> powershell.exe -Noninteractive -file C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 AuthorizationManager check failed. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess – gary Sep 23 '17 at 20:24
  • 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"} – gary Oct 09 '17 at 16:33
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?

E235
  • 11,560
  • 24
  • 91
  • 141
  • So flag as a duplicate then? – Matt Sep 23 '17 at 20:16
  • File C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1 cannot be loaded because you opted not to run this software now. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess – gary Sep 23 '17 at 20:23
  • @gary try `powershell -ExecutionPolicy ByPass {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -ExecutionPolicy ByPass -File "C:\orchestrator\AddServerRolesAndFeatures\InstallRolesAndFeatures.ps1"}` – E235 Sep 23 '17 at 20:29
  • 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): – gary Sep 23 '17 at 20:40
  • 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 – gary Sep 23 '17 at 20:44
  • @gary How many `Read-Host` commands are in the code ? Did you run the powershell with `RunAs Administrator` ? If not, please try it also. – E235 Sep 23 '17 at 20:47
  • Where do I need to put RunAs Administrator ? I tried putting at the end but not working . then I tried .."} -Verb runAs not working :( – gary Sep 23 '17 at 21:09
  • @gary When you open powershell console. Before that, press right click on powershell shortcut or the EXE itself and choose from the menu `RunAs Administrator`, like here: http://www.isumsoft.com/it/wp-content/uploads/2016/04/run-powershell-as-admin.jpg – E235 Sep 23 '17 at 21:11
  • I have tried executing second statement first but same :( – gary Sep 23 '17 at 21:57
  • @gary I want to understand where is the problem. Firs thing - run `Get-ExecutionPolicy` and write me what it writes you. Second, create simple script `C:\tmp\myScript.ps1` and inside it put `Read-Host "computer name: "`. Then, run: `powershell {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -File "C:\tmp\myScript.ps1"}` What was the result ? did it wait for enter ? – E235 Sep 23 '17 at 22:01
  • Yes , its aksing for computer name !!PS C:\Users\Administrator> Get-ExecutionPolicy Unrestricted PS C:\> .\myScript.ps1 powershell {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -File "C:\myScript.ps1"} computer name: : – gary Sep 23 '17 at 22:08
  • its okie E235 Thanks for your time !!! I will manage by some other way !!! – gary Sep 23 '17 at 22:38
  • @gary It worked on my computer with a simple script. Try what I wrote you. With a simple script of one line with `Read-Host` and check my solution. If it work, so maybe there another request for input in your script and we need to add more enters. – E235 Sep 24 '17 at 07:28