我尝试从 c# 运行 powershell
脚本。
首先,我将 ExecutionPolicy
设置为 Unrestricted
,脚本现在从 PowerShell ISE
运行。
现在这是 c# 我的代码:
class Program
{
private static PowerShell ps;
static void Main(string[] args)
{
ps = PowerShell.Create();
string ps1File = Path.Combine(Environment.CurrentDirectory, "script.ps1");
ExecuteScript(ps1File);
Console.ReadLine();
}
static void ExecuteScript(string script)
{
try
{
ps.AddScript(script);
Collection<PSObject> results = ps.Invoke();
Console.WriteLine("Output:");
foreach (var psObject in results)
{
Console.WriteLine(psObject);
}
Console.WriteLine("Non-terminating errors:");
foreach (ErrorRecord err in ps.Streams.Error)
{
Console.WriteLine(err.ToString());
}
}
catch (RuntimeException ex)
{
Console.WriteLine("Terminating error:");
Console.WriteLine(ex.Message);
}
}
}
输出是:
ps1 无法加载,因为在此系统上禁用了运行脚本。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。
Get-ExecutionPolicy -List
并使用结果编辑您的问题。此命令将向您显示不同的 Scope 及其 ExecutionPolicy 设置。
这可能是由于当前用户具有未定义的 ExecutionPolicy
。
以管理员身份在 PowerShell 中,您可以尝试以下操作:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
以管理模式打开powershell并运行以下命令
Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
这对我有用,并将范围限制为当前用户。
在您的 powershell 或 cmd 中运行此代码
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
如果您使用的是 Visual Studio 代码:
打开终端运行命令:Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted 然后运行命令protractor conf.js
这与量角器测试脚本执行相关,我遇到了同样的问题,它是这样解决的。
在终端(Visual Studio Code 终端或您正在使用的任何 IDE)中尝试此命令,问题将解决
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
注意:不要将 CurrentUser 更改为您的用户名
转到系统设置 -> 更新和安全 -> 对于开发人员 -> PowerShell
应用以下选项
https://i.stack.imgur.com/vcjVS.png
打开 VS Code 终端并运行以下脚本:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
以下三个步骤用于修复 Running Scripts is disabled on this System 错误
Step1 : 要解决此类问题,我们必须以管理员模式启动 power shell。
第 2 步:键入以下命令 set-ExecutionPolicy RemoteSigned 第 3 步:按 Y 进行确认。
访问以下内容了解更多信息https://youtu.be/J_596H-sWsk
关闭您当前的命令提示符或 vs 代码(终端) 在管理员模式下打开 PowerShell 在 PowerShell 中运行此命令 Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted answer Y 或 A(如果您想授予所有用户访问权限)现在打开命令提示符或vs 代码或任何你喜欢运行你的项目的东西
将此代码粘贴到您的终端中
(Visual Studio Code 终端或您正在使用的任何 IDE)
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
按回车
享受 :)
另一种解决方案是从目录 C:\Users%username%\AppData\Roaming\npm\ 中删除 ng.ps1 并清除 npm 缓存
以管理员模式打开 windows powershell 并运行以下命令及其工作 瞧!
Set-ExecutionPolicy RemoteSigned
PowerShell 执行策略默认设置为受限。您可以使用 Set-ExecutionPolicy cmdlet 更改 PowerShell 执行策略。运行外部脚本集策略为 RemoteSigned。
PS C:> Set-ExecutionPolicy RemoteSigned 下面是 PowerShell 中四种不同执行策略的列表
Restricted – 不能运行任何脚本。 AllSigned – 只能运行由受信任的发布者签名的脚本。 RemoteSigned – 下载的脚本必须由受信任的发布者签名。不受限制 - 可以运行所有 Windows PowerShell 脚本。
以管理模式打开 PowerShell 并运行以下命令
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
最近,我在尝试在 netlify
上部署应用程序时遇到了与 running-scripts-is-disabled-on-this-system
相同的问题
下面的 cmd 对我有用。
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
我收到了这个错误:
ng : File C:\Users\Nisha Jain\AppData\Roaming\npm\ng.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
+ ng serve
+ ~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
只需删除此文件 ng.ps1
Path is : C:\Users\username\AppData\Roaming\npm\ng.ps1
这对我来说可以。
打开 windows powershell 或 cmd 并粘贴以下命令。如果它要求进一步确认,只需输入是Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
下面应该出现
`Execution Policy Change
执行策略有助于保护您免受您不信任的脚本的侵害。更改执行策略可能会使您面临 https://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies 帮助主题中描述的安全风险。是否要更改执行策略? [Y] 是 [A] 全部是 [N] 否 [L] 全部否 [S] 暂停 [?] 帮助(默认为“N”):是 PS C:\Users\Tessact01>`
我认为您可以在管理模式或命令提示符下使用 powershell。
不定期副业成功案例分享
Powershell
中执行,而不是 Windows 的 cmd。Unrestricted
表示您可以运行所有脚本。另一种选择是将执行策略设置为RemoteSigned
,因为这将允许Allow local scripts and remote signed scripts
,但取决于应用程序及其正在执行的操作,这可能仍会引发相同的错误。我认为您的方法在安全性方面是有效的,您始终可以使用Get-ExecutionPloicy
事先阅读策略的内容。虽然这是说范围是当前用户,所以我认为风险很小。我希望这有帮助。RemoteSigned
担保。Unrestricted
可能是不必要的允许。