当执行策略设置为RemoteSigned

时间:2018-07-19 11:03:58

标签: c# powershell execution sign

我正在从azure blob存储下载一个脚本,然后在powershell脚本中执行。无论我的计算机上设置了什么策略,都将下载并执行它的获取。目前,我的计算机策略已设置为“ RemoteSigned”

在代码端,我将InitialSessionState的executionPolicy属性配置为RemoteSigned。

    var iss = InitialSessionState.CreateDefault();
    iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned;
    var runSpace = RunspaceFactory.CreateRunspace(iss);
    runSpace.Open();    
    PowerShellInstance = PowerShell.Create();               
    PowerShellInstance.Runspace = runSpace; 
    var output = PowerShellInstance.Invoke();

它只是执行脚本

脚本是

   $Urls = @()
   $Urls += "https://.............ps1"

   $OutPath = "C:\Temp\"

   ForEach ( $item in $Urls) {
       $file = $OutPath +  ($item).split('/')[-1]
       (New-Object System.Net.WebClient).DownloadFile($item, $file)
   }

   cd "C:\Temp\"
   .\.ps1

根据我对执行策略的了解,除非包含签名的证书或签名,否则它决不能执行。但是它仍然执行。任何人都可以让我知道为什么吗?

1 个答案:

答案 0 :(得分:0)

使用Get-Content $ScriptPath -Stream zone.identifier检查区域标识符。如果该流不存在,那么即使使用RemoteSigned设置,脚本也将运行。