在 Web 应用程序中出现错误 System.Management.Automation.PSSecurityException HResult=0x80131501

问题描述 投票:0回答:1

我在Azure Portal中创建了App Service并将我的Web应用程序代码发布到Web App。在 Web 应用程序 Powershell 脚本中,并非不执行,而是尝试将调试器进程附加到 Web 应用程序,并在异常中出现以下错误。

以下是错误:

系统.管理.自动化.PSSecurityException H结果=0x80131501 消息 = 文件 C:\home\site\wwwroot\Scripts\KeyVault.ps1 无法加载,因为在此系统上禁用了运行脚本。有关详细信息,请参阅 about_Execution_Policies,网址为 https://go.microsoft.com/fwlink/?LinkID=135170 来源=系统.管理.自动化 堆栈跟踪: 在 System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable 输入) 在 System.Management.Automation.Runspaces.Pipeline.Invoke() 在 D:\KVPowershell\Repository\RolesRepository.cs 中的 MSaaSPartnerOnboarding.Repository.RolesRepository.CreateAADAPP(RolesModel 角色):第 32 行

内部异常1: UnauthorizedAccessException:无法加载文件 C:\home\site\wwwroot\Scripts\KeyVault.ps1,因为在此系统上禁用运行脚本。有关详细信息,请参阅 about_Execution_Policies,网址为 https://go.microsoft.com/fwlink/?LinkID=135170。**

下面是我的 Powershell 脚本:

param(
    [Parameter(Mandatory=$true)]
    [string]$KeyVaultName
)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Set-AzContext -Subscription "<<SubscriptionId>>"
New-AzKeyVault -VaultName $KeyVaultname -ResourceGroupName 'testrg' -Location 'East US'

下面是C#代码:

public void CreateKV(RolesModel roles)
{
    try
    {
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        PowerShell PowerShellInstance = PowerShell.Create();
        string scriptPath = @"./Scripts/KeyVault.ps1";          
        Command mycmd = new Command(scriptPath);
        Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
        commandParameters.Add(new CommandParameter("KeyVaultName", roles.KeyVaultName));

        foreach (CommandParameter commandParameter in commandParameters)
        {
            mycmd.Parameters.Add(commandParameter);
        }
        pipeline.Commands.Add(mycmd);
        pipeline.Runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "Stop");
        Collection<PSObject> psObjects;
        psObjects = pipeline.Invoke();
        runspace.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

脚本正在本地执行,但是当我尝试在 Web 应用程序中执行时,它不起作用。

c# powershell web-applications azure-web-app-service
1个回答
0
投票

以下是错误:

System.Management.Automation.PSSecurityException HResult=0x80131501 Message=文件 C:\home\site\wwwroot\Scripts\KeyVault.ps1 无法加载,因为在此系统上禁用运行脚本。

错误中明确提到“无法加载脚本,因为在此系统上禁用运行脚本。”所以,这与用户范围的执行策略有关,感谢@stuartd指出朝着正确的方向前进。

将执行策略更改为

RemoteSigned
应该可以解决该问题。为此,请以
“以管理员身份运行”
打开 PowerShell 并使用
Get-ExecutionPolicy 
检索在环境中启用的执行策略,如下所示。如果“远程签名”不存在,则将执行策略设置为远程签名。

Set-ExecutionPolicy -ExecutionPolicy  RemoteSigned

相关问题参考blog

我已签入

App servcie >> Kudu console >> PowerShell
并设置为预期保单值:-

enter image description here

另请检查您在 Azure Web App 中是否具有运行 PowerShell 脚本所需的权限。 (例如:贡献者角色)

如果问题仍然存在,请尝试将执行策略设置为无限制,如下所示。

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

注意:不建议允许

unrestricted
访问PowerShell脚本,因为这可能会导致安全冲突,特别是在涉及机密数据的情况下。

© www.soinside.com 2019 - 2024. All rights reserved.