2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2019-04-04 23:01:01 -07:00
Import-Module HelpersCommon
2017-09-11 17:45:21 -07:00
try
2017-08-08 09:36:22 -07:00
{
# Skip all tests on non-windows and non-PowerShellCore and non-elevated platforms.
2017-09-11 17:45:21 -07:00
$originalWarningPreference = $WarningPreference
$WarningPreference = "SilentlyContinue"
2019-04-04 23:01:01 -07:00
# Skip all tests if can't write to $PSHOME as Register-PSSessionConfiguration writes to $PSHOME
2023-01-31 12:06:29 -08:00
# or if the processor architecture is Arm64
2023-08-16 11:28:59 -07:00
$IsNotSkipped = (
$IsWindows -and
$IsCoreCLR -and
( Test-IsElevated ) -and
( Test-CanWriteToPsHome ) -and
-not ( Test-IsWindowsArm64 ) -and
-not ( Test-IsWinWow64 )
)
2017-08-08 09:36:22 -07:00
2023-08-16 11:28:59 -07:00
Push-DefaultParameterValueStack @ { "it:skip" = ! $IsNotSkipped }
2017-08-08 09:36:22 -07:00
#
# TODO: Enable-PSRemoting should be performed at a higher set up for all tests.
# Tests whether PowerShell remoting is enabled for this instance of PowerShell.
# If remoting is not enabled, it will enable it and then clean up after all the tests
# have executed.
#
if ( $IsNotSkipped )
{
2020-01-13 19:36:31 +00:00
$endpointName = "PowerShell. $( $PSVersionTable . GitCommitId ) "
2017-08-08 09:36:22 -07:00
$matchedEndpoint = Get-PSSessionConfiguration $endpointName -ErrorAction SilentlyContinue
if ( $matchedEndpoint -eq $null )
{
# An endpoint for this instance of PowerShell does not exist.
#
# -SkipNetworkProfileCheck is used in case Docker or another application
# has created a publich virtual network profile on the system
Enable-PSRemoting -SkipNetworkProfileCheck
$endpointCreated = $true
}
}
2017-09-11 17:45:21 -07:00
try
2017-08-08 09:36:22 -07:00
{
Describe "Validate Register-PSSessionConfiguration" -Tags @ ( "CI" , 'RequireAdminOnWindows' ) {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
AfterAll {
if ( $IsNotSkipped )
{
Get-PSSessionConfiguration -Name "ITTask*" -ErrorAction SilentlyContinue | Unregister-PSSessionConfiguration
}
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Register-PSSessionConfiguration -TransportOption" {
$ConfigurationName = "ITTask" + ( Get-Random -Minimum 10000 -Maximum 99999 )
$Transport = New-PSTransportOption -MaxSessions 40 -IdleTimeoutSec 3600
$null = Register-PSSessionConfiguration -Name $ConfigurationName -TransportOption $Transport
$result = Get-PSSessionConfiguration -Name $ConfigurationName
2018-03-23 10:36:11 -07:00
$result . MaxShells | Should -Be 40
$result . IdleTimeoutms | Should -Be 3600000
2020-01-24 09:54:07 -08:00
$result . UseSharedProcess | Should -Be 'False'
2017-08-08 09:36:22 -07:00
}
}
Describe "Validate Get-PSSessionConfiguration, Enable-PSSessionConfiguration, Disable-PSSessionConfiguration, Unregister-PSSessionConfiguration cmdlets" -Tags @ ( "CI" , 'RequireAdminOnWindows' ) {
BeforeAll {
if ( $IsNotSkipped )
{
# Register new session configuration
function RegisterNewConfiguration {
param (
2023-08-16 11:28:59 -07:00
[ string ] $Name ,
[ string ] $ConfigFilePath ,
[ switch ] $Enabled
2017-08-08 09:36:22 -07:00
)
$TestConfig = Get-PSSessionConfiguration -Name $Name -ErrorAction SilentlyContinue
2023-08-16 11:28:59 -07:00
if ( $TestConfig ) {
2017-08-08 09:36:22 -07:00
$null = Unregister-PSSessionConfiguration -Name $Name
}
2023-08-16 11:28:59 -07:00
if ( $Enabled ) {
2017-08-08 09:36:22 -07:00
$null = Register-PSSessionConfiguration -Name $Name -Path $ConfigFilePath
2017-09-11 17:45:21 -07:00
}
2023-08-16 11:28:59 -07:00
else {
2017-08-08 09:36:22 -07:00
$null = Register-PSSessionConfiguration -Name $Name -Path $ConfigFilePath -AccessMode Disabled
}
}
# Unregister session configuration
function UnregisterPSSessionConfiguration {
param (
2023-08-16 11:28:59 -07:00
[ string ] $Name
2017-08-08 09:36:22 -07:00
)
Unregister-PSSessionConfiguration -Name $Name -Force -NoServiceRestart -ErrorAction SilentlyContinue
}
# Create new Config File
function CreateTestConfigFile {
2017-09-11 17:45:21 -07:00
2018-07-16 19:35:48 -07:00
$TestConfigFileLoc = Join-Path $TestDrive "Remoting"
2023-08-16 11:28:59 -07:00
if ( -not ( Test-path $TestConfigFileLoc )) {
2017-08-08 09:36:22 -07:00
$null = New-Item -Path $TestConfigFileLoc -ItemType Directory -Force -ErrorAction Stop
}
2018-07-16 19:35:48 -07:00
$TestConfigFile = Join-Path $TestConfigFileLoc "TestConfigFile.pssc"
2017-08-08 09:36:22 -07:00
$null = New-PSSessionConfigurationFile -Path $TestConfigFile -SessionType Default
return $TestConfigFile
}
$LocalConfigFilePath = CreateTestConfigFile
2017-11-29 00:00:49 +04:00
$expectedPSVersion = " $( $PSVersionTable . PSVersion . Major ) . $( $PSVersionTable . PSVersion . Minor ) "
2017-08-08 09:36:22 -07:00
}
}
Context "Validate Get-PSSessionConfiguration cmdlet" {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Get-PSSessionConfiguration with no parameter" {
$Result = Get-PSSessionConfiguration
2018-03-23 10:36:11 -07:00
$Result . Name -contains $endpointName | Should -BeTrue
2018-04-10 12:21:44 -07:00
$Result . PSVersion -contains $expectedPSVersion | Should -BeTrue
2017-08-08 09:36:22 -07:00
}
It "Get-PSSessionConfiguration with Name parameter" {
$Result = Get-PSSessionConfiguration -Name $endpointName
2018-03-23 10:36:11 -07:00
$Result . Name | Should -BeExactly $endpointName
$Result . PSVersion | Should -BeExactly $expectedPSVersion
2017-08-08 09:36:22 -07:00
}
It "Get-PSSessionConfiguration -Name with wildcard character" {
2017-09-11 17:45:21 -07:00
$endpointWildcard = "powershell.*"
2017-08-08 09:36:22 -07:00
$Result = Get-PSSessionConfiguration -Name $endpointWildcard
2018-03-23 10:36:11 -07:00
$Result . Name -contains $endpointName | Should -BeTrue
2018-04-10 12:21:44 -07:00
$Result . PSVersion -contains $expectedPSVersion | Should -BeTrue
2017-08-08 09:36:22 -07:00
}
It "Get-PSSessionConfiguration -Name with Non-Existent session configuration" {
2018-03-23 10:36:11 -07:00
{ Get-PSSessionConfiguration -Name "NonExistantSessionConfiguration" -ErrorAction Stop } | Should -Throw -ErrorId "Microsoft.PowerShell.Commands.WriteErrorException"
2017-08-08 09:36:22 -07:00
}
}
Context "Validate Enable-PSSessionConfiguration and Disable-PSSessionConfiguration" {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
function VerifyEnableAndDisablePSSessionConfig {
param (
2023-08-16 11:28:59 -07:00
[ string ] $SessionConfigName ,
[ string ] $ConfigFilePath ,
[ Bool ] $InitialSessionStateEnabled ,
[ Bool ] $FinalSessionStateEnabled ,
[ string ] $TestDescription ,
[ bool ] $EnablePSSessionConfig )
2017-08-08 09:36:22 -07:00
It " $TestDescription " {
RegisterNewConfiguration -Name $SessionConfigName -ConfigFilePath $ConfigFilePath -Enabled: $InitialSessionStateEnabled
$TestConfigStateBeforeChange = ( Get-PSSessionConfiguration -Name $SessionConfigName ). Enabled
2017-09-11 17:45:21 -07:00
2023-08-16 11:28:59 -07:00
if ( $EnablePSSessionConfig ) {
2017-08-08 09:36:22 -07:00
$isSkipNetworkCheck = $true
# TODO: Get-NetConnectionProfile is not available during typical PS Core deployments. Once it is, this check should be used.
#Get-NetConnectionProfile | Where-Object { $_.NetworkCategory -eq "Public" } | ForEach-Object { $isSkipNetworkCheck = $true }
Enable-PSSessionConfiguration -Name $SessionConfigName -NoServiceRestart -SkipNetworkProfileCheck: $isSkipNetworkCheck
}
2023-08-16 11:28:59 -07:00
else {
2017-08-08 09:36:22 -07:00
Disable-PSSessionConfiguration -Name $SessionConfigName -NoServiceRestart
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
$TestConfigStateAfterChange = ( Get-PSSessionConfiguration -Name $SessionConfigName -ErrorAction SilentlyContinue ). Enabled
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
UnregisterPSSessionConfiguration -Name $SessionConfigName
2017-09-11 17:45:21 -07:00
2018-03-23 10:36:11 -07:00
$TestConfigStateBeforeChange | Should -Be " $InitialSessionStateEnabled "
$TestConfigStateAfterChange | Should -Be " $FinalSessionStateEnabled "
2017-08-08 09:36:22 -07:00
}
}
$TestData = @ (
@ {
SessionConfigName = "TestDisablePSSessionConfig"
ConfigFilePath = $LocalConfigFilePath
InitialSessionStateEnabled = $true
FinalSessionStateEnabled = $false
TestDescription = "Validate Disable-Configuration cmdlet"
EnablePSSessionConfig = $false
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
@ {
SessionConfigName = "TestEnablePSSessionConfig"
ConfigFilePath = $LocalConfigFilePath
InitialSessionStateEnabled = $false
FinalSessionStateEnabled = $true
TestDescription = "Validate Enable-Configuration cmdlet"
EnablePSSessionConfig = $true
}
)
2023-08-16 11:28:59 -07:00
foreach ( $testcase in $testData ) {
2017-08-08 09:36:22 -07:00
VerifyEnableAndDisablePSSessionConfig @testcase
}
}
Context "Validate Unregister-PSSessionConfiguration cmdlet" {
BeforeEach {
2023-08-16 11:28:59 -07:00
if ( $IsNotSkipped ) {
Register-PSSessionConfiguration -Name "TestUnregisterPSSessionConfig"
}
2017-08-08 09:36:22 -07:00
}
AfterAll {
2023-08-16 11:28:59 -07:00
if ( $IsNotSkipped ) {
2017-08-08 09:36:22 -07:00
Unregister-PSSessionConfiguration -name "TestUnregisterPSSessionConfig" -ErrorAction SilentlyContinue | Out-Null
}
}
function TestUnRegisterPSSsessionConfiguration {
param ( $Description , $SessionConfigName , $ExpectedOutput , $ExpectedError )
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It " $Description " {
$Result = [ PSObject ] @ { Output = $true ; Error = $null }
2020-01-13 19:19:39 +00:00
$error . Clear ()
2023-08-16 11:28:59 -07:00
try {
2017-08-08 09:36:22 -07:00
$null = Unregister-PSSessionConfiguration -name $SessionConfigName -ErrorAction stop
2017-09-11 17:45:21 -07:00
}
2023-08-16 11:28:59 -07:00
catch {
2017-08-08 09:36:22 -07:00
$Result . Error = $_ . Exception
}
2023-08-16 11:28:59 -07:00
if ( -not $Result . Error ) {
2017-08-08 09:36:22 -07:00
$ValidEndpoints = [ PSObject ] @ ( Get-PSSessionConfiguration )
2023-08-16 11:28:59 -07:00
foreach ( $endpoint in $ValidEndpoints ) {
2017-09-11 17:45:21 -07:00
# Setting it to false means the unregister was unsuccessful
2017-08-08 09:36:22 -07:00
# and there is still an endpoint with name matching the one we wanted to remove.
2023-08-16 11:28:59 -07:00
if ( $endpoint . name -like $SessionConfigName ) {
2017-08-08 09:36:22 -07:00
$Result . Output = $false
break
}
}
2017-09-11 17:45:21 -07:00
}
2023-08-16 11:28:59 -07:00
else {
2017-08-08 09:36:22 -07:00
$Result . Output = $false
}
2018-03-23 10:36:11 -07:00
$Result . Output | Should -Match $ExpectedOutput
$Result . Error | Should -Match $ExpectedError
2017-08-08 09:36:22 -07:00
}
}
$TestData = @ (
@ {
Description = "Validate Unregister-PSSessionConfiguration with -name parameter"
SessionConfigName = "TestUnregisterPSSessionConfig"
ExpectedOutput = $true
ExpectedError = $null
}
@ {
Description = "Validate Unregister-PSSessionConfiguration with name having wildcard character"
SessionConfigName = "TestUnregister*"
ExpectedOutput = $true
ExpectedError = $null
}
@ {
Description = "Validate Unregister-PSSessionConfiguration for non-existant endpoint"
SessionConfigName = 'TestInvalidEndPoint'
ExpectedOutput = $false
ExpectedError = "No session configuration matches criteria `" TestInvalidEndPoint `" ."
}
)
foreach ( $TestCase in $TestData )
{
TestUnRegisterPSSsessionConfiguration @TestCase
}
}
}
Describe "Validate Register-PSSessionConfiguration, Set-PSSessionConfiguration cmdlets" -Tags @ ( "Feature" , 'RequireAdminOnWindows' ) {
BeforeAll {
if ( $IsNotSkipped )
{
2017-11-29 00:00:49 +04:00
$expectedPSVersion = " $( $PSVersionTable . PSVersion . Major ) . $( $PSVersionTable . PSVersion . Minor ) "
2017-08-08 09:36:22 -07:00
function ValidateRemoteEndpoint {
param ( $TestSessionConfigName , $ScriptToExecute , $ExpectedOutput )
$Result = [ PSObject ] @ { Output = $null ; Error = $null }
2017-09-11 17:45:21 -07:00
try
2017-08-08 09:36:22 -07:00
{
$sn = New-PSSession . -ConfigurationName $TestSessionConfigName -ErrorAction Stop
if ( $sn )
{
if ( $ScriptToExecute )
{
$Result . Output = invoke-command -Session $Sn -ScriptBlock { param ( $scripttoExecute ) Invoke-Expression $scripttoExecute } -ArgumentList $ScriptToExecute
}
else
{
$Result . Output = $true
}
}
else
{
throw "Unable to create session $TestSessionConfigName "
}
2017-09-11 17:45:21 -07:00
}
catch
2017-08-08 09:36:22 -07:00
{
$Result . Error = $_ . Error . FullyQualifiedErrorId
}
finally
{
if ( $sn )
{
Remove-PSSession $sn -ErrorAction SilentlyContinue | Out-Null
$sn = $null
}
}
2018-03-23 10:36:11 -07:00
$Result . Output | Should -Be $ExpectedOutput
$Result . Error | Should -BeNullOrEmpty
2017-08-08 09:36:22 -07:00
}
# Create Test Startup Script
function CreateStartupScript {
$ScriptContent = @"
` $script:testvariable = "testValue"
"@
2017-09-11 17:45:21 -07:00
2018-07-16 19:35:48 -07:00
$TestScript = Join-Path $script:TestDir "StartupTestScript.ps1"
2017-08-08 09:36:22 -07:00
$null = Set-Content -path $TestScript -Value $ScriptContent
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
return $TestScript
}
# Create new Config File
function CreateTestConfigFile {
2017-09-11 17:45:21 -07:00
2018-07-16 19:35:48 -07:00
$TestConfigFile = Join-Path $script:TestDir "TestConfigFile.pssc"
2017-08-08 09:36:22 -07:00
$null = New-PSSessionConfigurationFile -Path $TestConfigFile -SessionType Default
return $TestConfigFile
}
function CreateTestModule {
$ScriptContent = @"
function IsTestModuleImported {
return ` $true
}
Export-ModuleMember IsTestModuleImported
"@
$TestModuleFileLoc = $script:TestDir
if ( -not ( Test-path $TestModuleFileLoc ))
{
$null = New-Item -Path $TestModuleFileLoc -ItemType Directory -Force -ErrorAction Stop
}
2018-07-16 19:35:48 -07:00
$TestModuleFile = Join-Path $TestModuleFileLoc "TestModule.psm1"
2017-08-08 09:36:22 -07:00
$null = Set-Content -path $TestModuleFile -Value $ScriptContent
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
return $TestModuleFile
}
function CreateTestAssembly {
$PscConfigDef = @"
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Remoting;
namespace PowershellTestConfigNamespace
{
public sealed class PowershellTestConfig : PSSessionConfiguration
{
/// <summary>
2017-09-11 17:45:21 -07:00
///
2017-08-08 09:36:22 -07:00
/// </summary>
/// <param name="senderInfo"></param>
/// <returns></returns>
public override InitialSessionState GetInitialSessionState(PSSenderInfo senderInfo)
{
return InitialSessionState.CreateDefault();
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
}
}
"@
2018-07-16 19:35:48 -07:00
$script:SourceFile = Join-Path $script:TestAssemblyDir "PowershellTestConfig.cs"
2017-08-08 09:36:22 -07:00
$PscConfigDef | out-file $script:SourceFile -Encoding ascii -Force
$TestAssemblyName = "TestAssembly.dll"
2018-07-16 19:35:48 -07:00
$TestAssemblyPath = Join-Path $script:TestAssemblyDir $TestAssemblyName
2017-08-08 09:36:22 -07:00
Add-Type -path $script:SourceFile -OutputAssembly $TestAssemblyPath
return $TestAssemblyName
}
2018-07-16 19:35:48 -07:00
$script:TestDir = Join-Path $TestDrive "Remoting"
2017-08-08 09:36:22 -07:00
if ( -not ( Test-Path $script:TestDir ))
{
$null = New-Item -path $script:TestDir -ItemType Directory
}
2018-07-16 19:35:48 -07:00
$script:TestAssemblyDir = Join-Path $TestDrive "AssemblyDir"
2017-08-08 09:36:22 -07:00
if ( -not ( Test-Path $script:TestAssemblyDir ))
{
$null = New-Item -path $script:TestAssemblyDir -ItemType Directory
}
$LocalConfigFilePath = CreateTestConfigFile
$LocalStartupScriptPath = CreateStartupScript
$LocalTestModulePath = CreateTestModule
$LocalTestAssemblyName = CreateTestAssembly
}
}
Context "Validate Register-PSSessionConfiguration" {
BeforeAll {
if ( $IsNotSkipped )
{
$TestSessionConfigName = "TestRegisterPSSesionConfig"
Unregister-PSSessionConfiguration -Name $TestSessionConfigName -Force -NoServiceRestart -ErrorAction SilentlyContinue
}
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
AfterEach {
Unregister-PSSessionConfiguration -Name $TestSessionConfigName -Force -NoServiceRestart -ErrorAction SilentlyContinue
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Validate Register-PSSessionConfiguration -name -path" {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
$pssessionthreadoptions = "UseCurrentThread"
$psmaximumreceivedobjectsizemb = 20
$psmaximumreceiveddatasizepercommandmb = 20
$UseSharedProcess = $true
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
Register-PSSessionConfiguration -Name $TestSessionConfigName -path $LocalConfigFilePath -MaximumReceivedObjectSizeMB $psmaximumreceivedobjectsizemb -MaximumReceivedDataSizePerCommandMB $psmaximumreceiveddatasizepercommandmb -UseSharedProcess: $UseSharedProcess -ThreadOptions $pssessionthreadoptions
2018-05-17 14:42:04 -07:00
$Result = [ PSObject ] @ { Session = Get-PSSessionConfiguration -Name $TestSessionConfigName ; Culture = ( Get-Item WSMan : \ localhost \ Plugin \ $endpointName \ lang -ErrorAction SilentlyContinue ). value }
2017-08-08 09:36:22 -07:00
2018-03-23 10:36:11 -07:00
$Result . Session . Name | Should -Be $TestSessionConfigName
$Result . Session . SessionType | Should -Be "Default"
$Result . Session . PSVersion | Should -BeExactly $expectedPSVersion
$Result . Session . Enabled | Should -BeTrue
$Result . Session . lang | Should -Be $Result . Culture
$Result . Session . pssessionthreadoptions | Should -Be $pssessionthreadoptions
$Result . Session . psmaximumreceivedobjectsizemb | Should -Be $psmaximumreceivedobjectsizemb
$Result . Session . psmaximumreceiveddatasizepercommandmb | Should -Be $psmaximumreceiveddatasizepercommandmb
$Result . Session . UseSharedProcess | Should -Be $UseSharedProcess
2017-08-08 09:36:22 -07:00
}
2017-09-11 17:45:21 -07:00
2021-12-01 15:54:25 -08:00
It "Verifies that Register-PSSessionConfiguration -PSVersion parameter is not supported" {
2017-11-29 00:00:49 +04:00
2021-12-01 15:54:25 -08:00
{ Register-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1 } | Should -Throw -ErrorId 'ParameterBindingFailed,Microsoft.PowerShell.Commands.RegisterPSSessionConfigurationCommand'
2017-11-29 00:00:49 +04:00
}
2017-08-09 15:12:59 -07:00
It "Validate Register-PSSessionConfiguration -startupscript parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Register-PSSessionConfiguration -Name $TestSessionConfigName -path $LocalConfigFilePath -StartupScript $LocalStartupScriptPath -Force
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute "return `$ script:testvariable" -ExpectedOutput "testValue" -ExpectedError $null
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Validate Register-PSSessionConfiguration -AccessMode parameter" {
$null = Register-PSSessionConfiguration -Name $TestSessionConfigName -path $LocalConfigFilePath -AccessMode Disabled -Force
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute $null -ExpectedOutput $null -ExpectedError "RemoteConnectionDisallowed,PSSessionOpenFailed"
}
2017-09-11 17:45:21 -07:00
2017-08-09 15:12:59 -07:00
It "Validate Register-PSSessionConfiguration -ModulesToImport parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Register-PSSessionConfiguration -Name $TestSessionConfigName -ModulesToImport $LocalTestModulePath -Force
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute "return IsTestModuleImported" -ExpectedOutput $true -ExpectedError $null
}
2017-08-09 15:12:59 -07:00
It "Validate Register-PSSessionConfiguration with ApplicationBase, AssemblyName and ConfigurationTypeName parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Register-PSSessionConfiguration -Name $TestSessionConfigName -ApplicationBase $script:TestAssemblyDir -AssemblyName $LocalTestAssemblyName -ConfigurationTypeName "PowershellTestConfigNamespace.PowershellTestConfig" -force
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute $null -ExpectedOutput $true -ExpectedError $null
}
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
Context "Validate Set-PSSessionConfiguration" {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
BeforeAll {
if ( $IsNotSkipped )
{
$TestSessionConfigName = "TestSetPSSesionConfig"
Unregister-PSSessionConfiguration -Name $TestSessionConfigName -Force -NoServiceRestart -ErrorAction SilentlyContinue
}
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
AfterEach {
Unregister-PSSessionConfiguration -Name $TestSessionConfigName -Force -NoServiceRestart -ErrorAction SilentlyContinue
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
BeforeEach {
Register-PSSessionConfiguration -Name $TestSessionConfigName
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Validate Set-PSSessionConfiguration -name -path -MaximumReceivedObjectSizeMB -MaximumReceivedDataSizePerCommandMB -UseSharedProcess -ThreadOptions parameters" {
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
$pssessionthreadoptions = "UseCurrentThread"
$psmaximumreceivedobjectsizemb = 20
$psmaximumreceiveddatasizepercommandmb = 20
$UseSharedProcess = $true
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
Set-PSSessionConfiguration -Name $TestSessionConfigName -MaximumReceivedObjectSizeMB $psmaximumreceivedobjectsizemb -MaximumReceivedDataSizePerCommandMB $psmaximumreceiveddatasizepercommandmb -UseSharedProcess: $UseSharedProcess -ThreadOptions $pssessionthreadoptions -NoServiceRestart
2018-05-17 14:42:04 -07:00
$Result = [ PSObject ] @ { Session = ( Get-PSSessionConfiguration -Name $TestSessionConfigName ) ; Culture = ( Get-Item WSMan : \ localhost \ Plugin \ microsoft . powershell \ lang -ErrorAction SilentlyContinue ). value }
2017-08-08 09:36:22 -07:00
2018-03-23 10:36:11 -07:00
$Result . Session . Name | Should -Be $TestSessionConfigName
$Result . Session . PSVersion | Should -BeExactly $expectedPSVersion
$Result . Session . Enabled | Should -BeTrue
$Result . Session . lang | Should -Be $result . Culture
$Result . Session . pssessionthreadoptions | Should -Be $pssessionthreadoptions
$Result . Session . psmaximumreceivedobjectsizemb | Should -Be $psmaximumreceivedobjectsizemb
$Result . Session . psmaximumreceiveddatasizepercommandmb | Should -Be $psmaximumreceiveddatasizepercommandmb
$Result . Session . UseSharedProcess | Should -Be $UseSharedProcess
2017-08-08 09:36:22 -07:00
}
2017-11-29 00:00:49 +04:00
2021-12-01 15:54:25 -08:00
It "Verifies that Set-PSSessionConfiguration -PSVersion parameter is not supported" {
2017-11-29 00:00:49 +04:00
2021-12-01 15:54:25 -08:00
{ Set-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1 } | Should -Throw -ErrorId 'ParameterBindingFailed,Microsoft.PowerShell.Commands.SetPSSessionConfigurationCommand'
2017-11-29 00:00:49 +04:00
}
2017-09-11 17:45:21 -07:00
2017-08-09 15:12:59 -07:00
It "Validate Set-PSSessionConfiguration -startupscript parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Set-PSSessionConfiguration -Name $TestSessionConfigName -StartupScript $LocalStartupScriptPath
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute "return `$ script:testvariable" -ExpectedOutput "testValue" -ExpectedError $null
}
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
It "Validate Set-PSSessionConfiguration -AccessMode parameter" {
$null = Set-PSSessionConfiguration -Name $TestSessionConfigName -AccessMode Disabled
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute $null -ExpectedOutput $null -ExpectedError "RemoteConnectionDisallowed,PSSessionOpenFailed"
}
2017-09-11 17:45:21 -07:00
2017-08-09 15:12:59 -07:00
It "Validate Set-PSSessionConfiguration -ModulesToImport parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Set-PSSessionConfiguration -Name $TestSessionConfigName -ModulesToImport $LocalTestModulePath -Force
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute "return IsTestModuleImported" -ExpectedOutput $true -ExpectedError $null
}
2017-08-09 15:12:59 -07:00
It "Validate Set-PSSessionConfiguration with ApplicationBase, AssemblyName and ConfigurationTypeName parameter" -Pending {
2017-08-08 09:36:22 -07:00
$null = Set-PSSessionConfiguration -Name $TestSessionConfigName -ApplicationBase $script:TestAssemblyDir -AssemblyName $LocalTestAssemblyName -ConfigurationTypeName "PowershellTestConfigNamespace.PowershellTestConfig" -force
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
ValidateRemoteEndpoint -TestSessionConfigName $TestSessionConfigName -ScriptToExecute $null -ExpectedOutput $true -ExpectedError $null
}
}
}
}
2017-09-11 17:45:21 -07:00
finally
2017-08-08 09:36:22 -07:00
{
if ( $endpointCreated )
{
Get-PSSessionConfiguration $endpointName -ErrorAction SilentlyContinue | Unregister-PSSessionConfiguration
}
}
Describe "Basic tests for New-PSSessionConfigurationFile Cmdlet" -Tags @ ( "CI" , 'RequireAdminOnWindows' ) {
It "Validate New-PSSessionConfigurationFile can successfully create a valid PSSessionConfigurationFile" {
2018-07-16 19:35:48 -07:00
$configFilePath = Join-Path $TestDrive "SamplePSSessionConfigurationFile.pssc"
2017-08-08 09:36:22 -07:00
try
{
New-PSSessionConfigurationFile $configFilePath
2018-07-16 19:35:48 -07:00
$result = Get-Content $configFilePath | Out-String
2017-09-11 17:45:21 -07:00
}
2017-08-08 09:36:22 -07:00
finally
{
if ( Test-Path $configFilePath ){ Remove-Item $configFilePath -Force }
}
$resultContent = invoke-expression ( $result )
2020-01-24 19:00:37 +00:00
$resultContent | Should -BeOfType System . Collections . Hashtable
2017-08-08 09:36:22 -07:00
# The default created hashtable in the session configuration file would have the
2017-09-11 17:45:21 -07:00
# following keys which we are validating below.
2018-03-23 10:36:11 -07:00
$resultContent . ContainsKey ( "SessionType" ) -and $resultContent . ContainsKey ( "SchemaVersion" ) -and $resultContent . ContainsKey ( "Guid" ) -and $resultContent . ContainsKey ( "Author" ) | Should -BeTrue
2017-08-08 09:36:22 -07:00
}
}
Describe "Feature tests for New-PSSessionConfigurationFile Cmdlet" -Tags @ ( "Feature" , 'RequireAdminOnWindows' ) {
It "Validate FullyQualifiedErrorId from New-PSSessionConfigurationFile when invalid path is provided as input" {
2018-03-23 10:36:11 -07:00
{ New-PSSessionConfigurationFile "cert:\foo.pssc" } |
Should -Throw -ErrorId "InvalidPSSessionConfigurationFilePath,Microsoft.PowerShell.Commands.NewPSSessionConfigurationFileCommand"
2017-08-08 09:36:22 -07:00
}
}
Describe "Test suite for Test-PSSessionConfigurationFile Cmdlet" -Tags @ ( "CI" , 'RequireAdminOnWindows' ) {
BeforeAll {
if ( $IsNotSkipped )
{
$parmMap = @ {
2017-09-11 17:45:21 -07:00
# values for PSSessionConfigFile
2017-08-08 09:36:22 -07:00
PowerShellVersion = '3.0'
SessionType = 'Default'
Author = 'User'
CompanyName = 'Microsoft Corporation'
2020-03-24 11:08:37 -07:00
Copyright = 'Copyright (c) Microsoft Corporation.'
2017-08-08 09:36:22 -07:00
Description = 'This is a sample session configuration file.'
GUID = '73cba863-aa49-4cbf-9917-269ddcf2b1e3'
SchemaVersion = '1.0.0.0'
2017-09-11 17:45:21 -07:00
2017-08-08 09:36:22 -07:00
# The scope of the test is to validate that a valid SessionConfigurationFile can be validated
# The test does not register the session configuration from the created session configuration file.
2017-09-11 17:45:21 -07:00
# The SCRATCH location is not validated.
2017-08-08 09:36:22 -07:00
EnvironmentVariables = @ {
PSModulePath = '$Env:PSModulePath + ";$env:SystemDrive\ProgramData"' ;
SCRATCH = "\\SomeValidRemoteShare\SharedLocation"
}
# The scope of the test is to validate that a valid SessionConfigurationFile can be validated
# The test does not register the session configuration from the created session configuration file.
# The AssembliesToLoad are not loaded by this test. The Test only validates that the supplied data
2017-09-11 17:45:21 -07:00
# is used to create a valid Session configuration file.
2017-08-08 09:36:22 -07:00
AssembliesToLoad = 'SomeValidBinary.dll'
# The same explanation as above holds good here.
ModulesToImport = 'SomeValidModule'
AliasDefinitions = @ (
@ {
Name = "gh" ;
Value = "Get-Help" ;
Description = "Gets the help" ;
Options = "AllScope" ;
},
@ {
Name = "sh" ;
Value = "Save-Help" ;
Description = "Saves the help" ;
Options = "Private" ;
},
@ {
Name = "uh" ;
Value = "Update-Help" ;
Description = "Updates the help" ;
Options = "ReadOnly" ;
}
)
Function Definitions = @ (
@ {
Name = "sysmodules" ;
2020-01-13 19:35:33 +00:00
ScriptBlock = 'pushd $PSHOME\Modules' ;
2017-08-08 09:36:22 -07:00
Options = "AllScope" ;
},
@ {
Name = "mymodules" ;
2020-01-13 19:33:43 +00:00
ScriptBlock = 'pushd $HOME\Documents\WindowsPowerShell\Modules' ;
2017-08-08 09:36:22 -07:00
Options = "ReadOnly" ;
}
)
VariableDefinitions = @ (
@ {
Name = "WarningPreference" ;
Value = "SilentlyContinue" ;
},
@ {
Name = "datahome" ;
Value = "\\fileserver\share\data" ;
},
@ {
Name = "allusershome" ;
Value = '$env:ProgramData'
}
)
# The scope of the test is to validate that a valid SessionConfigurationFile can be validated
# The test does not register the session configuration from the created session configuration file.
2017-09-11 17:45:21 -07:00
# The existance of the files supplied as input to TypesToProcess, FormatsToProcess, ScriptsToProcess
2017-08-08 09:36:22 -07:00
# are not validated while creating a valid session configurtation file.
# The Test only validates that the supplied data can be successfully used to create a valid Session configuration file.
TypesToProcess = '$env:SystemDrive\SampleTypesFile.ps1xml'
FormatsToProcess = '$env:SystemDrive\SampleFormatsFile.ps1xml'
ScriptsToProcess = '$env:SystemDrive\SampleScript.ps1'
VisibleAliases = "c*" , "g*" , "i*" , "s*"
VisibleCmdlets = "c*" , "get*" , "i*" , "set*"
VisibleFunctions = "*"
VisibleProviders = 'FileSystem' , 'Function' , 'Registry' , 'Variable'
VisibleVariables = "*"
LanguageMode = "RestrictedLanguage"
ExecutionPolicy = "AllSigned"
}
}
}
It "Validate FullyQualifiedErrorId from Test-PSSessionConfigurationFile when invalid path is provided as input" {
2018-03-23 10:36:11 -07:00
{ Test-PSSessionConfigurationFile "cert:\foo.pssc" -ErrorAction Stop } | Should -Throw -ErrorId "PSSessionConfigurationFileNotFound,Microsoft.PowerShell.Commands.TestPSSessionConfigurationFileCommand"
2017-08-08 09:36:22 -07:00
}
It "Validate FullyQualifiedErrorId from Test-PSSessionConfigurationFile when an invalid pssc file is provided as input and -Verbose parameter is specified" {
2018-07-16 19:35:48 -07:00
$configFilePath = Join-Path $TestDrive "SamplePSSessionConfigurationFile.pssc"
2017-08-08 09:36:22 -07:00
"InvalidData" | Out-File $configFilePath
2018-03-23 10:36:11 -07:00
Test-PSSessionConfigurationFile $configFilePath -Verbose -ErrorAction Stop | Should -BeFalse
2017-08-08 09:36:22 -07:00
}
It "Test case verifies that the generated config file passes validation" {
# Path the config file
2018-07-16 19:35:48 -07:00
$configFilePath = Join-Path $TestDrive "SamplePSSessionConfigurationFile.pssc"
2017-08-08 09:36:22 -07:00
$updatedFunctionDefn = @ ()
foreach ( $currentDefination in $parmMap . FunctionDefinitions )
{
$createdFunctionDefn = @ {}
foreach ( $currentDefinationKey in $currentDefination . Keys )
{
if ( $currentDefinationKey -eq "ScriptBlock" )
{
$value = [ ScriptBlock ]:: Create ( $currentDefination [ $currentDefinationKey ])
}
else
{
2017-09-11 17:45:21 -07:00
$value = $currentDefination [ $currentDefinationKey ]
2017-08-08 09:36:22 -07:00
}
$createdFunctionDefn . Add ( $currentDefinationKey , $value )
}
$updatedFunctionDefn += $createdFunctionDefn
}
$updatedVariableDefn = @ ()
foreach ( $currentDefination in $parmMap . VariableDefinitions )
{
$createdVariableDefn = @ {}
foreach ( $currentDefinationKey in $currentDefination . Keys )
{
$createdVariableDefn . Add ( $currentDefinationKey , $currentDefination [ $currentDefinationKey ])
}
$updatedVariableDefn += $createdVariableDefn
}
try
{
# Create Config file
New-PSSessionConfigurationFile `
-Path $configFilePath `
-SchemaVersion $parmMap . SchemaVersion `
-Author $parmMap . Author `
-CompanyName $parmMap . CompanyName `
-Copyright $parmMap . Copyright `
-Description $parmMap . Description `
-PowerShellVersion $parmMap . PowerShellVersion `
-SessionType $parmMap . SessionType `
-ModulesToImport $parmMap . ModulesToImport `
-AssembliesToLoad $parmMap . AssembliesToLoad `
-VisibleAliases $parmMap . VisibleAliases `
-VisibleCmdlets $parmMap . VisibleCmdlets `
-VisibleFunctions $parmMap . VisibleFunctions `
-VisibleProviders $parmMap . VisibleProviders `
-AliasDefinitions $parmMap . AliasDefinitions `
-FunctionDefinitions $updatedFunctionDefn `
-VariableDefinitions $updatedVariableDefn `
-EnvironmentVariables $parmMap . EnvironmentVariables `
-TypesToProcess $parmMap . TypesToProcess `
-FormatsToProcess $parmMap . FormatsToProcess `
-LanguageMode $parmMap . LanguageMode `
-ExecutionPolicy $parmMap . ExecutionPolicy `
-ScriptsToProcess $parmMap . ScriptsToProcess `
-GUID $parmMap . GUID
# Verify the generated config file using the Test-PSSessionConfigurationFile
$result = Test-PSSessionConfigurationFile -Path $configFilePath -Verbose
}
finally
{
if ( Test-Path $configFilePath )
{
Remove-Item $configFilePath -Force
}
}
2018-03-23 10:36:11 -07:00
$result | Should -BeTrue
2017-08-08 09:36:22 -07:00
}
}
2018-04-10 12:21:44 -07:00
Describe "Validate Enable-PSSession Cmdlet" -Tags @ ( "Feature" , 'RequireAdminOnWindows' ) {
BeforeAll {
2023-01-31 12:06:29 -08:00
if ( Test-IsWindowsArm64 ) {
Write-Verbose "remoting is not setup on ARM64, skipping tests" -Verbose
2023-08-16 11:28:59 -07:00
Push-DefaultParameterValueStack @ { "it:skip" = $true }
2023-01-31 12:06:29 -08:00
return
}
2018-04-10 12:21:44 -07:00
if ( $IsNotSkipped ) {
2023-04-25 11:06:34 -07:00
Enable-PSRemoting -SkipNetworkProfileCheck -Force
2018-04-10 12:21:44 -07:00
}
}
2023-08-16 11:28:59 -07:00
AfterAll {
if ( Test-IsWindowsArm64 ) {
Pop-DefaultParameterValueStack
}
}
2018-04-10 12:21:44 -07:00
It "Enable-PSSession Cmdlet creates a PSSession configuration with a name tied to PowerShell version." {
2018-07-16 19:35:48 -07:00
$endpointName = "PowerShell." + $PSVersionTable . GitCommitId . ToString ()
2018-04-10 12:21:44 -07:00
$matchedEndpoint = Get-PSSessionConfiguration $endpointName -ErrorAction SilentlyContinue
$matchedEndpoint | Should -Not -BeNullOrEmpty
}
It "Enable-PSSession Cmdlet creates a default PSSession configuration untied to a specific PowerShell version." {
$dotPos = $PSVersionTable . PSVersion . ToString (). IndexOf ( "." )
$endpointName = "PowerShell." + $PSVersionTable . PSVersion . ToString (). Substring ( 0 , $dotPos )
2018-08-23 17:19:45 -07:00
# any pre-release (a version with a dash) is a preview release
if ( $PSVersionTable . GitCommitId . Contains ( "-" ))
2018-07-16 19:35:48 -07:00
{
$endpointName += "-preview"
}
2018-04-10 12:21:44 -07:00
$matchedEndpoint = Get-PSSessionConfiguration $endpointName -ErrorAction SilentlyContinue
$matchedEndpoint | Should -Not -BeNullOrEmpty
}
}
2017-08-08 09:36:22 -07:00
}
finally
{
2023-08-16 11:28:59 -07:00
Pop-DefaultParameterValueStack
2017-09-11 17:45:21 -07:00
$WarningPreference = $originalWarningPreference
2017-08-08 09:36:22 -07:00
}