包含MotW的远程脚本不包含MotW,并且ExecutionPolicy RemoteSigned无法测试

时间:2018-08-12 10:59:21

标签: c# powershell mstest executionpolicy mark-of-the-web

我正在执行RemoteSigned之前将Powershell策略配置为“ Powershell.Invoke()”。我有包含MotW的脚本文件,该文件当然没有签名。我要在机器/测试服务器上进行的所有测试都是:由于将策略配置为RemoteSigned,因此不应执行远程脚本下载文件。

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

我不知道是什么原因。 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 个答案:

没有答案
相关问题