Skip to content

Commit 807f1e9

Browse files
authored
Merge pull request #159 from Outek/dev
xPowershellExecutionPolicy merge in ComputerManagementDSC
2 parents 4b2f827 + 77c780e commit 807f1e9

13 files changed

Lines changed: 450 additions & 7 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Unreleased
44

5+
- PowershellExecutionPolicy:
6+
- Updated to meet HQRM guidelines.
7+
- Migrated the xPowershellExecutionPolicy from [xPowershellExecutionPolicy](https://github.com/PowerShell/xPowerShellExecutionPolicy)
8+
and renamed to PowershellExecutionPolicy.
9+
- Moved strings to localization file.
10+
- Changed the scope from Global to Script in MSFT_ScheduledTask.Integration.Tests.ps1
11+
- Changed the scope from Global to Script ComputerManagementDsc.Common.Tests.ps1
12+
513
## 5.1.0.0
614

715
- TimeZone:
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'
2+
3+
# Import the ComputerManagementDsc Common Modules
4+
Import-Module -Name (Join-Path -Path $modulePath `
5+
-ChildPath (Join-Path -Path 'ComputerManagementDsc.Common' `
6+
-ChildPath 'ComputerManagementDsc.Common.psm1'))
7+
8+
# Import the ComputerManagementDsc Resource Helper Module
9+
Import-Module -Name (Join-Path -Path $modulePath `
10+
-ChildPath (Join-Path -Path 'ComputerManagementDsc.ResourceHelper' `
11+
-ChildPath 'ComputerManagementDsc.ResourceHelper.psm1'))
12+
13+
# Import Localization Strings
14+
$script:localizedData = Get-LocalizedData `
15+
-ResourceName 'MSFT_PowershellExecutionPolicy' `
16+
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
17+
18+
<#
19+
.SYNOPSIS
20+
Gets the current resource state.
21+
22+
.PARAMETER ExecutionPolicy
23+
Specifies the given Powershell Execution Policy
24+
25+
.PARAMETER ExecutionPolicyScope
26+
Specifies the given Powershell Execution Policy Scope
27+
#>
28+
function Get-TargetResource
29+
{
30+
[CmdletBinding()]
31+
[OutputType([System.Collections.Hashtable])]
32+
param
33+
(
34+
[Parameter(Mandatory = $true)]
35+
[ValidateSet("CurrentUser","LocalMachine","MachinePolicy","Process","UserPolicy")]
36+
[System.String]
37+
$ExecutionPolicyScope,
38+
39+
[Parameter(Mandatory = $true)]
40+
[ValidateSet("Bypass","Restricted","AllSigned","RemoteSigned","Unrestricted")]
41+
[System.String]
42+
$ExecutionPolicy
43+
)
44+
45+
Write-Verbose -Message ($localizedData.GettingPowerShellExecutionPolicy -f $ExecutionPolicyScope, $ExecutionPolicy)
46+
47+
# Gets the execution policies for the current session.
48+
$returnValue = @{
49+
ExecutionPolicyScope = $ExecutionPolicyScope
50+
ExecutionPolicy = $(Get-ExecutionPolicy -Scope $ExecutionPolicyScope)
51+
}
52+
53+
return $returnValue
54+
}
55+
56+
<#
57+
.SYNOPSIS
58+
Sets the desired resource state.
59+
60+
.PARAMETER ExecutionPolicy
61+
Specifies the given Powershell Execution Policy
62+
63+
.PARAMETER ExecutionPolicyScope
64+
Specifies the given Powershell Execution Policy Scope
65+
#>
66+
67+
function Set-TargetResource
68+
{
69+
[CmdletBinding(SupportsShouldProcess=$true)]
70+
param
71+
(
72+
[Parameter(Mandatory = $true)]
73+
[ValidateSet("CurrentUser","LocalMachine","MachinePolicy","Process","UserPolicy")]
74+
[System.String]
75+
$ExecutionPolicyScope,
76+
77+
[Parameter(Mandatory = $true)]
78+
[ValidateSet("Bypass","Restricted","AllSigned","RemoteSigned","Unrestricted")]
79+
[System.String]
80+
$ExecutionPolicy
81+
)
82+
83+
if ($PSCmdlet.ShouldProcess("$ExecutionPolicy","Set-ExecutionPolicy"))
84+
{
85+
Write-Verbose -Message ($localizedData.SettingPowerShellExecutionPolicy -f $ExecutionPolicyScope, $ExecutionPolicy)
86+
87+
try
88+
{
89+
Set-ExecutionPolicy -ExecutionPolicy $ExecutionPolicy -Scope $ExecutionPolicyScope -Force -ErrorAction Stop
90+
Write-Verbose -Message ($localizedData.UpdatePowershellExecutionPolicySuccess -f $ExecutionPolicyScope, $ExecutionPolicy)
91+
}
92+
catch
93+
{
94+
if ($_.FullyQualifiedErrorId -eq 'ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand')
95+
{
96+
Write-Verbose -Message ($localizedData.UpdatePowershellExecutionPolicySuccess -f $ExecutionPolicyScope, $ExecutionPolicy)
97+
}
98+
else
99+
{
100+
throw
101+
}
102+
}
103+
}
104+
}
105+
106+
<#
107+
.SYNOPSIS
108+
Tests if the current resource state matches the desired resource state.
109+
110+
.PARAMETER ExecutionPolicy
111+
Specifies the given Powershell Execution Policy
112+
113+
.PARAMETER ExecutionPolicyScope
114+
Specifies the given Powershell Execution Policy Scope
115+
#>
116+
117+
function Test-TargetResource
118+
{
119+
[CmdletBinding()]
120+
[OutputType([System.Boolean])]
121+
param
122+
(
123+
[Parameter(Mandatory = $true)]
124+
[ValidateSet("CurrentUser","LocalMachine","MachinePolicy","Process","UserPolicy")]
125+
[System.String]
126+
$ExecutionPolicyScope,
127+
128+
[Parameter(Mandatory = $true)]
129+
[ValidateSet("Bypass","Restricted","AllSigned","RemoteSigned","Unrestricted")]
130+
[System.String]
131+
$ExecutionPolicy
132+
)
133+
134+
Write-Verbose -Message ($localizedData.TestingPowerShellExecutionPolicy -f $ExecutionPolicyScope, $ExecutionPolicy)
135+
136+
if ((Get-ExecutionPolicy -Scope $ExecutionPolicyScope) -eq $ExecutionPolicy)
137+
{
138+
return $true
139+
}
140+
else
141+
{
142+
return $false
143+
}
144+
}
145+
146+
Export-ModuleMember -Function *-TargetResource
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[ClassVersion("1.0.0.0"), FriendlyName("PowerShellExecutionPolicy")]
2+
class MSFT_PowerShellExecutionPolicy : OMI_BaseResource
3+
{
4+
[Key, Description("Defines the scope for the preference of the Windows PowerShell execution policy."), ValueMap{"CurrentUser","LocalMachine","MachinePolicy","Process","UserPolicy"},Values{"CurrentUser","LocalMachine","MachinePolicy","Process","UserPolicy"}] String ExecutionPolicyScope;
5+
[Required, Description("Changes the preference for the Windows PowerShell execution policy."), ValueMap{"Bypass","Restricted","AllSigned","RemoteSigned","Unrestricted"}, Values{"Bypass","Restricted","AllSigned","RemoteSigned","Unrestricted"}] String ExecutionPolicy;
6+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Description
2+
3+
This resource allows configuration of the PowerShell execution
4+
policy for different execution scopes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# culture="en-US"
2+
ConvertFrom-StringData -StringData @'
3+
GettingPowerShellExecutionPolicy = The current execution policy for '{0}' is '{1}'.
4+
SettingPowerShellExecutionPolicy = Setting the execution policy for '{0}' to '{1}'.
5+
PowerShellExecutionPolicyAlreadySetMessage = Powershell execution policy already set to {0}.
6+
UpdatePowershellExecutionPolicySuccess = Updating PowerShell Execution policy for '{0}' to '{1}' successfully.
7+
UpdatePowershellExecutionPolicyFailed = Updating PowerShell Execution policy for '{0}' to '{1}' failed.
8+
TestingPowerShellExecutionPolicy = Testing the current execution policy for '{0}' is '{1}'.
9+
'@
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<#
2+
.EXAMPLE
3+
This example shows how to configure powershell's execution policy for the specified execution policy scope.
4+
#>
5+
6+
Configuration Example
7+
{
8+
Import-DscResource -ModuleName ComputerManagementDsc
9+
10+
Node localhost
11+
{
12+
PowerShellExecutionPolicy ExecutionPolicy
13+
{
14+
ExecutionPolicyScope = 'CurrentUser'
15+
ExecutionPolicy = 'RemoteSigned'
16+
} # End of PowershellExecutionPolicy Resource
17+
} # End of Node
18+
} # End of Configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<#
2+
.EXAMPLE
3+
This example shows how to configure multiple powershell's execution policy for a specified execution policy scope.
4+
#>
5+
6+
Configuration Example
7+
{
8+
Import-DscResource -ModuleName ComputerManagementDsc
9+
10+
Node localhost
11+
{
12+
PowerShellExecutionPolicy ExecutionPolicyCurrentUser
13+
{
14+
ExecutionPolicyScope = 'CurrentUser'
15+
ExecutionPolicy = 'RemoteSigned'
16+
} # End of ExecutionPolicyCurrentUser Resource
17+
18+
PowerShellExecutionPolicy ExecutionPolicyLocalMachine
19+
{
20+
ExecutionPolicyScope = 'LocalMachine'
21+
ExecutionPolicy = 'RemoteSigned'
22+
} # End of ExecutionPolicyLocalMachine Resource
23+
} # End of Node
24+
} # End of Configuration

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The **ComputerManagementDsc** module contains the following resources:
1919
- **TimeZone**: this resource is used for setting the time zone on a machine.
2020
- **VirtualMemory**: allows configuration of properties of the paging file on
2121
the local computer.
22+
- **PowerShellExecutionPolicy**: Specifies the desired PowerShell execution policy.
2223

2324
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
2425
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)

Tests/Integration/ComputerManagementDsc.Common.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ try
5454
}
5555

5656
Mock -CommandName 'TzUtil.exe' -MockWith {
57-
$global:LASTEXITCODE = 0
57+
$Script:LASTEXITCODE = 0
5858
return 'OK'
5959
}
6060

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
$script:DSCModuleName = 'ComputerManagementDsc'
2+
$script:DSCResourceName = 'MSFT_PowerShellExecutionPolicy'
3+
4+
#region HEADER
5+
# Integration Test Template Version: 1.1.1
6+
$script:moduleRoot = Join-Path -Path $(Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))) -ChildPath 'Modules\ComputerManagementDsc'
7+
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
8+
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
9+
{
10+
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
11+
}
12+
13+
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force
14+
$TestEnvironment = Initialize-TestEnvironment `
15+
-DSCModuleName $script:DSCModuleName `
16+
-DSCResourceName $script:DSCResourceName `
17+
-TestType Integration
18+
19+
#endregion
20+
21+
try
22+
{
23+
#region Integration Tests
24+
$configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1"
25+
. $configFile
26+
27+
Describe "$($script:DSCResourceName)_Integration" {
28+
#region DEFAULT TESTS
29+
It 'Should compile and apply the MOF without throwing' {
30+
{
31+
& "$($script:DSCResourceName)_Config" -OutputPath $TestDrive
32+
33+
$startDscConfigurationParameters = @{
34+
Path = $TestDrive
35+
ComputerName = 'localhost'
36+
Wait = $true
37+
Verbose = $true
38+
Force = $true
39+
ErrorAction = 'Stop'
40+
}
41+
42+
Start-DscConfiguration @startDscConfigurationParameters
43+
} | Should -Not -Throw
44+
}
45+
46+
It 'Should be able to call Get-DscConfiguration without throwing' {
47+
{
48+
Get-DscConfiguration -Verbose -ErrorAction Stop
49+
} | Should -Not -Throw
50+
}
51+
#endregion
52+
53+
It 'Should have set the resource and all the parameters should match' {
54+
$current = Get-DscConfiguration | Where-Object -FilterScript {
55+
$_.ConfigurationName -eq "$($script:DSCResourceName)_Config"
56+
}
57+
$current.ExecutionPolicy | Should Be 'RemoteSigned'
58+
$current.ExecutionPolicyScope | Should Be 'LocalMachine'
59+
}
60+
}
61+
#endregion
62+
63+
}
64+
finally
65+
{
66+
#region FOOTER
67+
Restore-TestEnvironment -TestEnvironment $TestEnvironment
68+
#endregion
69+
}

0 commit comments

Comments
 (0)