Hi all ,
I’m trying to build some class based DSC Resource using the latest release of PowerShell 5 April Preview .
I’m wondering how do you Debug methods inside the class ?? With functions I can specified parameters and see the output and set Breakpoints or “Write-Debug” .
So my question is how do I get inside the methods or how do I test those methods with different parameters .
This is an example :
enum Ensure {
Absent
Present
}
[DscResource()]
class ClassService {
[DscProperty(Key)]
[string]$serviceName
[DscProperty(Mandatory)]
[Ensure] $Ensure
[DscProperty(Mandatory)]
[string] $Servicestatus
[ClassService] Get() {
$serviceExists = Get-Service -Name bits -ErrorAction SilentlyContinue
If ($ServiceExists.status -eq $this.ServiceStatus) {
Write-Verbose "Service status is correct"
}Else {
write-Verbose "Service status is not correct"
}
return $this
#$returnValue = @{
# ServiceName = [System.String]$this.ServiceName
# Servicestatus = [System.String]$serviceExists.Status
# Ensure = [System.String]$this.Ensure
#}
#return $returnValue
}
[void] Set() {
Write-verbose "Changing service status"
If ($this.serviceStatus -eq "Running") {
Start-Service -name $this.serviceName
}Else {
Stop-Service -name $this.ServiceName
}
}
[bool] Test() {
$serviceExists = Get-Service -Name $this.ServiceName -ErrorAction SilentlyContinue
if ($this.Ensure -eq 'Present') {
if($serviceExists -ne $null)
{
If ($serviceExists.status -eq $this.ServiceStatus) {
Write-Verbose "Nothing to configure - Service exists and has correct status"
Return $True
}Else {
Write-Verbose "Need to configure - Status is not correct"
Return $False
}
} else {
Write-Verbose "Nothing to configure - Service name does not exist"
return $false
}
} Else {
Write-Verbose "Nothing to configure - Ensure is Absent"
return $False
}
}
}
Regards
Mariusz