Powershell替换CMD

时间:2019-06-19 22:45:05

标签: shell powershell powershell-4.0

在默认情况下,执行策略受到限制(这是有充分理由的)时,PowerShell如何替换CMD,但是,例如,对于周围需要在安装过程中和安装后进行批处理的许多商用软件系统, PS有可能吗?  例如,大多数软件包和游戏在安装过程中的确会运行并运行几个cmd文件来完成各种任务。如果CMD完全熄灭,由于powershell默认设置为受限(甚至使用默认的记事本打开),因此powershell将在这方面提供帮助。对于已安装的每个第三方软件,powershell会要求用户手动将“执行策略”设置为“签名”或“不受限制”吗?还是在软件安装过程中,产品会提示用户手动将.ps1代码复制并粘贴到powershell提示中以完成工作吗?

3 个答案:

答案 0 :(得分:1)

我的大部分回答来自reading Microsoft's Powershell Execution Policy documentation

执行策略会影响 PowerShell脚本执行,而不是阻止PowerShell终端执行批处理文件或启动可执行文件等。

将其视为: PowerShell外壳环境具有ExecutionPolicies来防止意外执行代码(如该文档中所述)。这些ExecutionPolicies用于管理 PowerShell脚本,但不适用于非PowerShell脚本。

由于PowerShell中仍然支持.bat / .cmd文件,因此软件仍然可以依赖批处理文件来运行PowerShell命令,如Option 3 of this answer to a question on bypassing ExecutionPolicy

中所述

答案 1 :(得分:0)

调用脚本时,可以通过-ExecutionPolicy参数覆盖执行策略

powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\your.ps1"

提供的执行策略未在本地或组策略中定义(优先于-ExecutionPolicy参数)。

您还可以基于每个系统,每个用户或每个进程定义执行策略:

# set execution policy to "Unrestricted" for the current PowerShell process
Set-ExecutionPolicy -Scope Process -Policy Unrestricted

# set execution policy to "RemoteSigned" for the currently logged-in user
Set-ExecutionPolicy -Scope CurrentUser -Policy RemoteSigned

# set execution policy to "RemoteSigned" system-wide
Set-ExecutionPolicy -Scope LocalMachine -Policy RemoteSigned

上面的第一个示例只要正在运行就只会影响当前的PowerShell进程,其他两个是持久性设置,即它们只需设置一次。

您还可以使用每个用户(范围UserPolicy)或每个系统(范围MachinePolicy)的本地策略或组策略来定义执行策略。

请注意,执行策略范围具有严格的层次结构:MachinePolicy取代UserPolicy取代Process取代CurrentUser取代LocalMachine。这意味着每个进程执行策略可以覆盖默认的LocalMachine设置,但不能覆盖本地/组策略强制的设置。

您可以通过Get-ExecutionPolicy -List查看当前设置的执行策略:

PS C:\> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine          Signed

我通常的建议是通过在系统设置过程中运行RemoteSigned或在每台计算机的本地/组策略中定义执行策略,将执行策略更改为Set-ExecutionPolicy -Scope LocalMachine -Policy RemoteSigned

See here了解更多信息。

答案 2 :(得分:0)

我不知道为什么你的问题被否决了,可能是出于基调。

答案already provided涵盖了即使默认执行策略受到限制,PowerShell仍可以运行的大多数方式。

虽然不是您具体要求的人,但是作为一名完成主义者,我将在提供的答案中添加以下内容。

即使在受限的PowerShell进程/范围内运行,只要您能够运行Invoke-Expression,就可以执行文件中存储的代码。 它与运行的脚本不完全相同,因为代码在受限制的同时不能引用任何其他脚本,但这是绕过执行策略的一种简单方法。

示例:

名为Test-ExecutinoPolicyRestrictions.ps1的脚本文件包含以下代码:

Write-Host -ForegroundColor Green '### Nothing can stop me'

然后在受限的PowerShell进程中运行以下代码:

"### $(Get-ExecutionPolicy)"
### Restricted
.\Test-ExecutinoPolicyRestrictions.ps1

将产生类似于以下内容的错误输出:

.\Test-ExecutinoPolicyRestrictions.ps1 : File <...>\Test-ExecutinoPolicyRestrictions.ps1 cannot be loaded
because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\Test-ExecutinoPolicyRestrictions.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

但是,运行以下命令将起作用:

Get-Content Test-ExecutinoPolicyRestrictions.ps1 | Invoke-Expression
### Nothing can stop me