|
| 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 |
0 commit comments