xSBETest.psm1
enum Ensure { Absent Present } <# This resource manages the file in a specific path. [DscResource()] indicates the class is a DSC resource #> [DscResource()] class CreateSBETestFile { <# 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. #> <# 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.CreateFile() } } 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. #> [CreateSBETestFile] Get() { $present = $this.TestFilePath($this.Path) if ($present) { $file = Get-ChildItem -LiteralPath $this.Path $this.Ensure = [Ensure]::Present } else { $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] CreateFile() { [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" } if (-not $this.TestFilePath($this.Path)) { Try { Write-Verbose -Message "Creating file $($this.Path)" "Test" | Out-File -FilePath $this.Path -Encoding "UTF8" -ErrorAction Stop } Catch { throw "Failed to create $($this.Path)..." } } # DSC engine catches and reports any error that occurs } } # This module defines a class for a DSC "FileResource" provider. |