标签: executionpolicy

绕过与不受限制的执行策略

关于该主题的文档仅提供以下内容:

无限制。加载所有配置文件并运行所有脚本。如果运行从Internet下载的未签名脚本,则会在运行之前提示您获得许可。

旁路。没有任何障碍,也没有警告或提示。

在我看来,两者似乎可以接受任何脚本,但令我惊讶的是事实并非如此。在某些情况下,绕过似乎会阻止执行。

那么,两者有什么区别?

powershell executionpolicy

8
推荐指数
1
解决办法
1万
查看次数

为什么 PowerShell 有执行策略?

我充分了解ExecutionPolicy在 PowerShell 中的工作原理。我所看到和所做的大部分是如何禁用它。甚至还有一个命令行标志来禁用它(powershell -ExecutionPolicy Unrestricted ...)。

所以我的问题是为什么,而不是如何。为什么这甚至是一个功能?根据我的经验,这更像是一个错误功能;它对我所做的唯一一件事就是在我看到“无法加载,因为在此系统上禁用脚本执行”错误和我想起那个-ExecutionPolicy标志之间的时候惹恼了我。

为什么PowerShell 会有这样的功能?它就像一个防盗警报器,在建筑物的前门旁边有一个开/关开关。

powershell executionpolicy

6
推荐指数
1
解决办法
1017
查看次数

从c#运行仅签名的powershell脚本

我有一个Windows服务,下载脚本,然后运行它.

我一直在努力使我的Windows服务更安全,使它只接受签名的power-shell脚本.

我在服务器上运行了Set-ExecutionPolicy AllSigned命令,这可以在windows power shell命令提示符下运行.

但是,即使set-executionpolicy设置为restricted,我的代码仍会运行有符号和无符号脚本.

我尝试了两种方法:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        Pipeline pipeline = runspace.CreatePipeline();         
        pipeline.Commands.AddScript(@"Set-ExecutionPolicy AllSigned");
        pipeline.Commands.AddScript(@"Get-ExecutionPolicy");
        pipeline.Commands.AddScript(script);
        Collection<PSObject> results = pipeline.Invoke();
Run Code Online (Sandbox Code Playgroud)

另一种方法:

using (PowerShell ps = PowerShell.Create())
                {
                    ps.AddCommand("Set-ExecutionPolicy").AddArgument("Restricted");
                    ps.AddScript("Set-ExecutionPolicy Restricted");
                    ps.AddScript(script);
                    Collection<PSObject> results = ps.Invoke();
                  }
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,代码也会运行未签名的脚本.

我错过了什么吗?

c# powershell executionpolicy

5
推荐指数
1
解决办法
1059
查看次数

Powershell 调用-SQLCmd 和 SQLPS

Windows 7 工作站 POSH 3.0 SS 2012 SP1

摘要:
Developer 1 无法运行 Invoke-SQLCmd
Developer 2 具有类似 SS Client 工具安装可以运行 Invoke-SQLCmd
Developer 1 无法运行 Import-Module SQLPS

当我尝试从笔记本电脑运行查询时,如下所示:

Invoke-sqlcmd -ServerInstance <ServerName> -Query "Select top 10 * from <SomeTable>;"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Invoke-sqlcmd : The 'Invoke-sqlcmd' command was found in the module 'SQLPS', 
but the module could not be loaded. For more information, run 'Import-Module 
SQLPS'. 
Run Code Online (Sandbox Code Playgroud)

另一位开发人员可以成功运行该命令,并且我们都有本地

由于明显的原因,我无法在我的计算机上运行导入模块 sqlps - ExecutionPolicy 受到限制。

我不确定为什么其他开发人员的机器允许查询,而我的机器不允许。这是 SQL 客户端安装问题吗?

powershell import-module executionpolicy sqlps

5
推荐指数
2
解决办法
4万
查看次数

重启后第一次运行powershell脚本的执行策略提示

我有一个 PowerShell 脚本,我想用它来自动化一些事情。我通过右键单击它并选择“使用 PowerShell 运行”来运行它。

我已经Set-ExecutionPolicy Bypass在 32 位和 64 位 PowerShell 中运行,并使用Get-ExecutionPolicy.

尽管如此,每次重新启动后我第一次尝试运行脚本时,我收到以下提示:

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 at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): …
Run Code Online (Sandbox Code Playgroud)

powershell executionpolicy powershell-5.0

5
推荐指数
1
解决办法
2481
查看次数

为什么从 PowerShell 设置的新执行策略在 C# 中不可见?

我使用以下代码成功获取执行策略:

using (var powerShellInstance = PowerShell.Create())
{
    powerShellInstance.AddCommand("Get-ExecutionPolicy");
    Collection<PSObject> obj = powerShellInstance.Invoke();
}
Run Code Online (Sandbox Code Playgroud)

我使用以下代码来设置执行策略:

using (var powerShellInstance = PowerShell.Create())
{
    powerShellInstance.AddCommand("Set-ExecutionPolicy").AddArgument("ByPass");
    powerShellInstance.Invoke();
}
Run Code Online (Sandbox Code Playgroud)

问题是当我从 PowerShell 设置执行策略时,更改在 C# 中不可见。如果我从 C# 设置执行策略,则更改是可见的。

考虑以下场景:

  • C#获取执行策略Unrestricted
  • PowerShell 将执行策略设置为Bypass
  • C#获取执行策略Unrestricted
  • C# 将执行策略设置为Bypass.
  • C#获取执行策略Bypass

c# powershell executionpolicy

5
推荐指数
1
解决办法
514
查看次数

Jenkins执行PowerShell脚本

我正在尝试从Jenkins运行PowerShell脚本,但它似乎完全忽略了执行策略!这可以通过直接执行powershell.exe或使用PowerShell插件来实现

附加信息:

Jenkins作为Windows服务运行(使用本地系统帐户,非交互式).确实连接到该服务器并检查执行策略RemoteSigned:

PS C:\> whoami
nt authority\system

PS C:\> Get-ExecutionPolicy
RemoteSigned
PS C:\>
Run Code Online (Sandbox Code Playgroud)

但是,在运行Jenkins构建时,情况并非如此.这是在Get-ExecutionPolicy -List构建步骤中执行的输出:

d:\workspace\test-job>powershell Get-ExecutionPolicy -list 
  Scope                         ExecutionPolicy
  -----                         ---------------
  MachinePolicy                 Undefined
  UserPolicy                    Undefined
  Process                       Undefined
  CurrentUser                   Undefined
  LocalMachine                  Undefined
Run Code Online (Sandbox Code Playgroud)

我也尝试从构建步骤中明确设置它,没有.

我错过了什么?

windows powershell jenkins jenkins-plugins executionpolicy

4
推荐指数
1
解决办法
4028
查看次数

为 CurrentUser 将 ExecutionPolicy 设置为 Unrestricted 是否构成安全漏洞?

我喜欢在 Windows 10 上的 PowerShell 中使用几个别名。

我希望它们在会话之间保持不变,因此我将它们放在 C:\Users{username}\Documents\WindowsPowerShell 下的 profile.ps1 文件中。

我收到这个烦人的“无法加载,因为在此系统上禁用了运行脚本。” 错误消息,并找到了有关如何摆脱它的页面:https : //social.technet.microsoft.com/Forums/en-US/3e4a9006-d47d-4e19-96f4-10327ae0c5b1/not-able-to- run-script-in-windows-10?forum=winserverpowershell

在这里有人说这条线会有所帮助:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force -Verbose
Run Code Online (Sandbox Code Playgroud)

但是,将执行策略设置为对我的用户不受限制是否危险?

security powershell executionpolicy windows-10

4
推荐指数
1
解决办法
2454
查看次数

Entity Framework PowerShell script cannot be loaded by Visual Studio because its operation is blocked by software restriction policies

When I load Package Manager Console within Visual Studio 2017 v15.9.6 in a project that uses Entity Framework I receive the following error:

\packages\EntityFramework.6.2.0\tools\init.ps1 cannot be loaded because its operation is blocked by software restriction policies, such as those created by using Group Policy. At line:1 char:45 + ... rgs+=$_}; & 'C:\Bitbucket\project-path\packages\EntityFramework.6.2. ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

This prevents me from running commands specific to Entity Framework like "Update-Database" or "Add-Migration". …

powershell entity-framework executionpolicy visual-studio-2017 package-manager-console

4
推荐指数
1
解决办法
1536
查看次数

如何使用 C# 执行 powershell 脚本并设置执行策略?

我尝试结合 stackoverflow 的两个答案(第一第二

InitialSessionState iss = InitialSessionState.CreateDefault();
// Override ExecutionPolicy

PropertyInfo execPolProp = iss.GetType().GetProperty(@"ExecutionPolicy");
if (execPolProp != null && execPolProp.CanWrite)
{
    execPolProp.SetValue(iss, ExecutionPolicy.Bypass, null);
}
Runspace runspace = RunspaceFactory.CreateRunspace(iss);
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);

pipeline.Commands.Add(myCommand);

// Execute PowerShell script
results = pipeline.Invoke(); 
Run Code Online (Sandbox Code Playgroud)

在我的 powershell 脚本中,我有以下参数:

Param(
[String]$key
)
Run Code Online (Sandbox Code Playgroud)

但是,当我执行此操作时,会出现以下异常:

System.Management.Automation.CmdletInvocationException: Cannot validate argument on parameter 'Session'. …
Run Code Online (Sandbox Code Playgroud)

c# powershell powershell-sdk executionpolicy

4
推荐指数
1
解决办法
2579
查看次数

以管理员身份从 cmd 批处理运行 PowerShell 脚本

我有一个 PowerShell 设置,我想在执行策略可能受到限制并且需要管理员权限的计算机上执行。
理想情况下,我可以将其包装在 cmd 批处理中,如下所示:

powershell -Command "Start-Process powershell -Verb runAs -ArgumentList '-noexit','-ExecutionPolicy','bypass','-File','C:\path\setup.ps1'"
Run Code Online (Sandbox Code Playgroud)

问题是,当包含空格时,我无法使其工作C:\path\setup.ps1,并且如果相对(使用 ),路径也不起作用cd C:\path
有什么帮助吗?

powershell cmd batch-file elevated-privileges executionpolicy

3
推荐指数
1
解决办法
9078
查看次数