Last week I wrote a blog article about a “ PowerShell Desired State Configuration Class Based Resource for Configuring Remote Desktop “. Since then I’ve discovered and learned a couple of new things about enumerations in PowerShell that can be used to simply the code even further.

My original code used a couple of enumerations which I’ve removed to show how they can be used to further simply the code:

class RemoteDesktop { [DscProperty(Key)] [string]$UserAuthenication [DscProperty(Mandatory)] [string]$Ensure [RemoteDesktop]Get() { $this.UserAuthenication = $this.GetAuthSetting() $this.Ensure = $this.GetTSSetting() Return $this } [string]GetAuthSetting(){ $AuthCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name 'UserAuthentication' | Select-Object -ExpandProperty UserAuthentication $AuthSetting = switch ($AuthCurrentSetting) { 0 {'NonSecure'; Break} 1 {'Secure'; Break} } Return $AuthSetting } [string]GetTSSetting(){ $TSCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' | Select-Object -ExpandProperty fDenyTSConnections $TSSetting = switch ($TSCurrentSetting) { 0 {'Present'; Break} 1 {'Absent'; Break} } Return $TSSetting } }

The code shown in the previous example uses switch statements to translate the numeric values returned from the registry into human readable names.There are several different ways to instantiate a copy of the RemoteDesktop class and call the Get() method:

(New-Object -TypeName RemoteDesktop).Get() ([RemoteDesktop]::new()).Get() $RDP = [RemoteDesktop]::new() $RDP.Get()
Simplifying my PowerShell version 5 Class Based DSC Resource for Configuring Rem ...

I’ll remove the switch statements to show that numeric values are indeed returned without them:

class RemoteDesktop { [DscProperty(Key)] [string]$UserAuthenication [DscProperty(Mandatory)] [string]$Ensure [RemoteDesktop]Get() { $this.UserAuthenication = $this.GetAuthSetting() $this.Ensure = $this.GetTSSetting() Return $this } [string]GetAuthSetting(){ $AuthCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name 'UserAuthentication' | Select-Object -ExpandProperty UserAuthentication Return $AuthCurrentSetting } [string]GetTSSetting(){ $TSCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' | Select-Object -ExpandProperty fDenyTSConnections Return $TSCurrentSetting } }
Simplifying my PowerShell version 5 Class Based DSC Resource for Configuring Rem ...

My original code from the previously referenced blog article used enumerations instead of ValidateSet, but I’ve since learned that enumerations in PowerShell offer a lot more functionality than just input validation.

enum UserAuthenication { NonSecure Secure } enum Ensure { Absent Present } class RemoteDesktop { [DscProperty(Key)] [UserAuthenication]$UserAuthenication [DscProperty(Mandatory)] [Ensure]$Ensure [RemoteDesktop]Get() { $this.UserAuthenication = $this.GetAuthSetting() $this.Ensure = $this.GetTSSetting() Return $this } [UserAuthenication]GetAuthSetting(){ $AuthCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name 'UserAuthentication' | Select-Object -ExpandProperty UserAuthentication Return $AuthCurrentSetting } [Ensure]GetTSSetting(){ $TSCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' | Select-Object -ExpandProperty fDenyTSConnections Return $TSCurrentSetting } }
Simplifying my PowerShell version 5 Class Based DSC Resource for Configuring Rem ...

By simply using the enumerations, the values are automatically translated from their numeric values returned from the registry into their human readable names that are defined in the enumeration so there’s no need to perform the translation with switch statements.

The only problem at this point is the incorrect value is being return by the Ensureenumeration.By default the first item in the enumeration is 0, the second one is 1, and so on. I could simply swap the order of the two items in the Ensure enumeration to correct this problem:

enum Ensure { Present Absent }

A better option is to be more declarative and define the value of each item in the enumerations so the order doesn’t matter:

enum UserAuthenication { NonSecure = 0 Secure = 1 } enum Ensure { Absent = 1 Present = 0 }

Although the code was already simple, the end result after refactoring it is even simpler code and less of it:

enum UserAuthenication { NonSecure = 0 Secure = 1 } enum Ensure { Present = 0 Absent = 1 } [DscResource()] class RemoteDesktop { [DscProperty(Key)] [UserAuthenication]$UserAuthenication [DscProperty(Mandatory)] [Ensure]$Ensure [RemoteDesktop]Get() { $this.UserAuthenication = $this.GetAuthSetting() $this.Ensure = $this.GetTSSetting() Return $this } [void]Set(){ if ($this.TestAuthSetting() -eq $false) { Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name 'UserAuthentication' -Value $this.UserAuthenication } if ($this.TestTSSetting() -eq $false) { Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' -Value $this.Ensure } } [bool]Test(){ if ($this.TestAuthSetting() -and $this.TestTSSetting() -eq $true) { Return $true } else { Return $false } } [UserAuthenication]GetAuthSetting(){ $AuthCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name 'UserAuthentication' | Select-Object -ExpandProperty UserAuthentication Return $AuthCurrentSetting } [Ensure]GetTSSetting(){ $TSCurrentSetting = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' | Select-Object -ExpandProperty fDenyTSConnections Return $TSCurrentSetting } [bool]TestAuthSetting(){ if ($this.UserAuthenication -eq $this.GetAuthSetting()){ Return $true } else { Return $false } } [bool]TestTSSetting(){ if ($this.Ensure -eq $this.GetTSSetting()){ Return $true } else { Return $false } } } As shown in the previous code example, I also decided to move the code from the SetAuthSetting() and SetTSSetting() methods back into the actual Set()

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShell
分页:12
转载请注明
本文标题:Simplifying my PowerShell version 5 Class Based DSC Resource for Configuring Rem ...
本站链接:http://www.codesec.net/view/483916.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(56)