MyDscResource.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
<#
.NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.154 Generated on: 7/27/2018 2:39 PM Generated by: olgab Organization: Sapien -------------------------------------------------------------------------------- .DESCRIPTION Script generated by PowerShell Studio 2018 #> <# =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.154 Created on: 7/26/2018 2:37 PM Created by: olgab Organization: Sapien Filename: MyDscResource.psm1 ------------------------------------------------------------------------- Module Name: MyDscResource =========================================================================== #> enum Ensure { Absent Present } <# This resource manages the file in a specific path. [DscResource()] indicates the class is a DSC resource #> [DscResource()] class FileResource { <# This property is the fully qualified path to the file that is expected to be present or absent. The [DscProperty(Key)] attribute indicates the property is a key and its value uniquely identifies a resource instance. Defining this attribute also means the property is required and DSC will ensure a value is set before calling the resource. A DSC resource must define at least one key property. #> [DscProperty(Key)] [string]$Path <# This property indicates if the settings should be present or absent on the system. For present, the resource ensures the file pointed to by $Path exists. For absent, it ensures the file point to by $Path does not exist. The [DscProperty(Mandatory)] attribute indicates the property is required and DSC will guarantee it is set. If Mandatory is not specified or if it is defined as Mandatory=$false, the value is not guaranteed to be set when DSC calls the resource. This is appropriate for optional properties. #> [DscProperty(Mandatory)] [Ensure]$Ensure <# This property defines the fully qualified path to a file that will be placed on the system if $Ensure = Present and $Path does not exist. NOTE: This property is required because [DscProperty(Mandatory)] is set. #> [DscProperty(Mandatory)] [string]$SourcePath <# This property reports the file's create timestamp. [DscProperty(NotConfigurable)] attribute indicates the property is not configurable in DSC configuration. Properties marked this way are populated by the Get() method to report additional details about the resource when it is present. #> [DscProperty(NotConfigurable)] [Nullable[datetime]]$CreationTime <# This method is equivalent of the Set-TargetResource script function. It sets the resource to the desired state. #> [void] Set() { $fileExists = $this.TestFilePath($this.Path) if ($this.ensure -eq [Ensure]::Present) { if (-not $fileExists) { $this.CopyFile() } } else { if ($fileExists) { Write-Verbose -Message "Deleting the file $($this.Path)" Remove-Item -LiteralPath $this.Path -Force } } } <# 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() { $present = $this.TestFilePath($this.Path) if ($this.Ensure -eq [Ensure]::Present) { return $present } else { return -not $present } } <# 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. #> [FileResource] Get() { $present = $this.TestFilePath($this.Path) if ($present) { $file = Get-ChildItem -LiteralPath $this.Path $this.CreationTime = $file.CreationTime $this.Ensure = [Ensure]::Present } else { $this.CreationTime = $null $this.Ensure = [Ensure]::Absent } return $this } <# Helper method to check if the file exists and it is file #> [bool] TestFilePath([string]$location) { $present = $true $item = Get-ChildItem -LiteralPath $location -ea Ignore if ($item -eq $null) { $present = $false } elseif ($item.PSProvider.Name -ne "FileSystem") { throw "Path $($location) is not a file path." } elseif ($item.PSIsContainer) { throw "Path $($location) is a directory path." } return $present } <# Helper method to copy file from source to path #> [void] CopyFile() { if (-not $this.TestFilePath($this.SourcePath)) { throw "SourcePath $($this.SourcePath) is not found." } [System.IO.FileInfo]$destFileInfo = new-object System.IO.FileInfo($this.Path) if (-not $destFileInfo.Directory.Exists) { Write-Verbose -Message "Creating directory $($destFileInfo.Directory.FullName)" <# Use CreateDirectory instead of New-Item to avoid code to handle the non-terminating error #> [System.IO.Directory]::CreateDirectory($destFileInfo.Directory.FullName) } if (Test-Path -LiteralPath $this.Path -PathType Container) { throw "Path $($this.Path) is a directory path" } Write-Verbose -Message "Copying $($this.SourcePath) to $($this.Path)" # DSC engine catches and reports any error that occurs Copy-Item -LiteralPath $this.SourcePath -Destination $this.Path -Force } } # This module defines a class for a DSC "FileResource" provider. |