Remote scripts containing MotW does not containing MotW and ExecutionPolicy RemoteSigned not testable

99 Views Asked by At

I am working with Powershell policy as "RemoteSigned" configured before Powershell.Invoke() executes. I have script file containing MotW which is not signed of course. All I want to test on my machine/on test server that remote script downloaded file should not get executed because I configured policy as RemoteSigned.

But I saw strange behaviors. On my local machine, script file ( e.g. UnsignedRemoteScript.ps1) properly gets deployed at my Deployment Test Directory first and contains the MotW as it is as it was before. But unfortunately, first it didn't copy to TestDirectory on other machines. After I checked 'Enable Deployment' at Local.testsettings, it start getting deployed but MotW didn't remain intact as it was on my machine. That's why test get's fail which also fails on all other machines. I tried to execute the script by pointing as unc path way, but I am not sure powershell script for making unc path is correct or not.

I don't know what's the reason. Is unc path creation is not correct? What unc path has to do with this story?

Following Code.

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");
}

Script executing remote script by using unc path

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

Can anybody help me in this regard

0

There are 0 best solutions below