cExecutionPolicy.psm1
enum ExecutionPolicy { AllSigned Bypass RemoteSigned Restricted Unrestricted } [DscResource()] class cExecutionPolicy { <# This property is the name of the system - its not used for anything other than they key. #> [DscProperty(Key)] [string]$Name [DscProperty(Mandatory)] [ExecutionPolicy]$ExecutionPolicy <# This method is equivalent of the Get-TargetResource script function. The implementation should use the keys to find appropriate resources. This method returns an instance of this class with the updated key properties. #> [cExecutionPolicy] Get() { $this.Name = $Env:COMPUTERNAME #This is the default PartitionType $this.ExecutionPolicy = (Get-ExecutionPolicy).ToString() return $this } <# This method is equivalent of the Test-TargetResource script function. It should return True or False, showing whether the resource is in a desired state. #> [bool] Test() { try { Write-Verbose "cExectionPolicy:Test - Start" $TestResult = $false $CurrentExecutionPolicy = (Get-ExecutionPolicy).ToString() $TestResult = ($CurrentExecutionPolicy -eq $this.ExecutionPolicy) Write-Verbose ("cExectionPolicy:Test - CurrentExecutionPolicy '" + $CurrentExecutionPolicy + "'") Write-Verbose ("cExectionPolicy:Test - Desired State '" + $this.ExecutionPolicy + "'") return $TestResult } finally { Write-Verbose "cExectionPolicy:Test - End" } } <# This method is equivalent of the Set-TargetResource script function. It sets the resource to the desired state. #> [void] Set() { try { Write-Verbose "cExectionPolicy:Set - Start" $CurrentExecutionPolicy = Get-ExecutionPolicy Write-Verbose ("cExectionPolicy:Set - Changing from '" + $CurrentExecutionPolicy + "' to '" + $this.ExecutionPolicy + "'") Try { Set-ExecutionPolicy -ExecutionPolicy $this.ExecutionPolicy -Force -ErrorAction Stop } catch { if($_.toString() -like "Windows PowerShell updated your execution policy successfully*") { # trap this error, it set correctlly. Write-Verbose "$_" } else { throw } } } finally { Write-Verbose "cExectionPolicy:Set - End" } } } |