自动生成对powershell命令的响应?

时间:2014-08-22 00:08:59

标签: windows powershell windows-7

我正在执行一个powershell脚本,即ps1文件中的命令。要在系统上运行任何PowerShell脚本,您必须先运行Set-ExecutionPolicy RemoteSigned,然后再手动输入y。只有在那之后,您才能运行ps脚本。有没有办法可以自动输入y以及启用PowerShell脚本的命令?

演示:

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. 
Changing the execution policy might expose you to the security risks described 
in the about_Execution_Policies help topic. Do you want to change the execution 
policy? [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

1 个答案:

答案 0 :(得分:4)

当您启动shell时,可以为当前会话设置executionpolicy:
powershell.exe -executionpolicy remotesigned -noexit

引自doc

  

为一个会议制定不同的执行政策

     
You can use the ExecutionPolicy parameter of PowerShell.exe to
set an execution policy for a new Windows PowerShell session.
The policy affects only the current session and child sessions.

To set the execution policy for a new session, start Windows PowerShell
at the command line (such as Cmd.exe or Windows PowerShell), and then use
the ExecutionPolicy parameter of PowerShell.exe to set the execution
policy.

For example:

PowerShell.exe -ExecutionPolicy AllSigned

The execution policy that you set is not stored in the registry.
Instead, it is stored in the $env:PSExecutionPolicyPreference
environment variable. The variable is deleted when you close
the session in which the policy is set. You cannot change the
policy by editing the variable value.


During the session, the execution policy that is set for the session takes
precedence over an execution policy that is set in the registry for the 
local computer or current user. However, it does not take precedence over 
the execution policy set by using a Group Policy setting (discussed below).