代码之家  ›  专栏  ›  技术社区  ›  Usman

包含MotW的远程脚本不包含MotW,ExecutionPolicy RemoteSigned不可测试

  •  0
  • Usman  · 技术社区  · 8 年前

    我正在与Powershell policy一起工作,作为“ RemoteSigned “以前配置过 Powershell.Invoke() 执行。我有包含MotW的脚本文件,当然没有签名。我想在我的机器/测试服务器上测试的全部内容是,远程脚本下载的文件不应执行,因为我将策略配置为RemoteSigned。

    但我看到了奇怪的行为。在我的本地机器上,脚本文件(例如。 UnsignedRemoteScript.ps1 )首先在我的部署测试目录中正确部署,并像以前一样包含MotW。但不幸的是,首先它没有复制到其他机器上的TestDirectory。我查过之后 Enable Deployment “在 Local.testsettings ,它开始部署,但MotW并没有像在我的机器上那样完好无损。这就是为什么test get失败的原因,它在所有其他机器上也会失败。 我试图通过指向unc路径来执行脚本,但我不确定用于创建unc路径的powershell脚本是否正确。

    我不知道是什么原因。unc路径创建是否不正确?unc路径与这个故事有什么关系?

    下面是代码。

    public void Setup()
    {
        var iss = InitialSessionState.CreateDefault();
        iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned;
        var powInstance = PowerShell.Create();
    }
    
    [TestMethod]
    [DeploymentItem(@"Path.......\RandomData.ps1")]
    public void 
    ExecuteRemoteScriptWithoutSignMustOutputUnauthorizedAccessException()
    {
            var runRemoteScript = "...\..\RunRemoteScript.ps1"
            var pipelineCommand = new Command(runRemoteScript, true);
            powInstance.Commands.AddCommand(pipelineCommand);
    
            powInstance.AddArgument(TestDeploymentDirectory);
    
            powInstance.Invoke();
    
            Assert.IsTrue(powInstance.Streams.Error.Any(), "Execution Policy evaluation failed. " +
                                                                    "Script is not digitally signed.");
    
            Assert.IsTrue(powInstance.Streams.Error[0].Exception != null,
                            "Exception not available while script execution policy evaluation");
            Assert.AreEqual(powInstance.Streams.Error[0].FullyQualifiedErrorId, "UnauthorizedAccess");
    
            StringAssert.Contains(powInstance.Streams.Error[0].Exception.Message,"digitally signed",
                                    "Execution Policy evaluation failed." +
                                    "Script execution policy found to be other than remote digitally signed");
    }
    

    使用unc路径执行远程脚本的脚本

    File : RunRemoteScript.ps1
    
    Param($remoteScriptPath)
    Set-Location $remoteScriptPath
    
    $drive = Get-PSDrive -Name (Get-location).Drive.Name
    $root = if($drive.DisplayRoot -ne $null){$drive.DisplayRoot} else {$drive.Root}
    $uncPath = Join-Path -Path $root -ChildPath $drive.CurrentLocation
    cd $uncPath
    
    .\RemoteUnsignedScript.ps1
    

    在这方面有人能帮我吗

    0 回复  |  直到 8 年前