Author Posts

June 17, 2015 at 2:16 am

Hi,

New to the forum was wondering if someone can help why my custom DSC resource is not working. What I find if the application is not present it will install the application without any issues. But what I wanted to do is make the script idempotent so as you can see if someone disables the firewall rule next type the script runs it would enable the rule again. What I am finding if the firewall is disabled, or deleted or if the service has stopped the custom dsc resource is not executing those steps. Any help would be great.

Thanks,
Viral
(New to PowerShell DSC)

function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment
)

$Configuration = @()
$firewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue
$firewallruleenabled = $firewallrule.Enabled
$ravendbservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$installed = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$destination = "C:\Temp\"
$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}

if($installed -ne $null){
$Configuration+=@{RavenDBInstalled = $true}

}

else{
$Configuration+=@{RavenDBInstalled = $false}

}

if($firewallrule -ne $null){
$Configuration+=@{FirewallRuleExists = $true}
}

else{
$Configuration+=@{FirewallRuleExists = $false}
}

if($firewallruleenabled -eq "True"){
$Configuration+=@{FirewallRuleEnabled = $true}
}

else{
$Configuration+=@{FirewallRuleEnabled = $false}

}

if($ravendbservice.State -eq "Running"){
$Configuration+=@{RavenDBServiceRunning = $true}

}

else{
$Configuration+=@{RavenDBServiceRunning = $false}

}

return $Configuration

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

}

function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure,

[ValidateSet("Running","Stopped")]
[System.String]
$ServiceStatus,

[System.String]
$DownloadURL,

[System.String]
$TargetDirectory,

[System.String]
$InstallFolder,

[System.String]
$DataDirectory,

[System.String]
$IndexDirectory
)

$currentstate = Get-TargetResource $PSBoundParameters

if($currentstate.FirewallRuleExists -eq $true){Write-Verbose "Firewall Rule Exists"}

else{
Write-Verbose "Creating Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

if($currentstate.FirewallRuleEnabled -eq $true){Write-Verbose "Firewall Rule Is Enabled"}

else{
Write-Verbose "Enabling Firewall Rule"
Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080"| Enable-NetFirewallRule
}

if($ServiceStatus -eq "Running" -and $currentstate.RavenDBServiceRunning -eq $true){Write-Verbose "RavenDB Service Is Running"}

else{
Write-Verbose "Starting RavenDB Service"
Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" | Invoke-CimMethod -MethodName StartService -Verbose

}

if($Ensure -eq "Present" -and $currentstate.RavenDBInstalled -eq $true ){Write-Verbose "RavenDB Is Installed"}

else{
Write-Verbose "Installing RavenDB"
Install-ScribestarRavenDB -Environment $Environment -TargetDirectory $TargetDirectory -InstallFolder $InstallFolder -DataDirectory $DataDirectory -IndexDirectory $IndexDirectory -DownloadURL $DownloadURL

}

if($Ensure -eq "Absent" -and $currentstate.RavenDBInstalled -eq $false ){Write-Verbose "RavenDB Is Not Installed"}

else{
Write-Verbose "Removing RavenDB"
Remove-ScribestarRavenDB -InstallFolder $InstallFolder
}

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

#Include this line if the resource requires a system reboot.
#$global:DSCMachineStatus = 1

}

function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure,

[ValidateSet("Running","Stopped")]
[System.String]
$ServiceStatus,

[System.String]
$DownloadURL,

[System.String]
$TargetDirectory,

[System.String]
$InstallFolder,

[System.String]
$DataDirectory,

[System.String]
$IndexDirectory
)

$currentstate = Get-TargetResource $PSBoundParameters

if($currentstate.FirewallRuleExists -eq $true){
#Write-Verbose "Firewall Rule Exists"
return $true
}

else{
#Write-Verbose "Firewall Rule Does Not Exist"
return $false
}

if($currentstate.FirewallRuleEnabled -eq $true){
#Write-Verbose "Firewall Rule Is Enabled"
return $true
}

else{
#Write-Verbose "Firewall Rule Is Not Enabled"
return $false
}

if($ServiceStatus -eq "Running" -and $currentstate.RavenDBServiceRunning -eq $true){
#Write-Verbose "RavenDB Service Is Running"
return $true

}

else{
#Write-Verbose "RavenDB Service Is Not Running"
return $false
}

if($ServiceStatus -eq "Stopped" -and $currentstate.RavenDBServiceRunning -eq $false){
#Write-Verbose "RavenDB Service Is Not Running"
return $true

}

else{
#Write-Verbose "RavenDB Is Running"
return $false

}

if($Ensure -eq "Present" -and $currentstate.RavenDBInstalled -eq $true){

#Write-Verbose "RavenDB Is Present"
return $true
}

else{
#Write-Verbose "RavenDB Is Not Present"
return $false
}

if($Ensure -eq "Absent" -and $currentstate.RavenDBInstalled -eq $false){
#Write-Verbose "RavenDB Is Absent"
return $false
}

else{
#Write-Verbose "RavenDB Is Present"
return $true
}

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

}

function Install-ScribestarRavenDB {
param(
[Parameter(Mandatory=$true)]
[string]$Environment,
[Parameter(Mandatory=$true)]
[string] $DownloadURL,
[Parameter(Mandatory=$true)]
[string]$TargetDirectory,
[Parameter(Mandatory=$true)]
[string]$InstallFolder,
[Parameter(Mandatory=$true)]
[string]$DataDirectory,
[Parameter(Mandatory=$true)]
[string]$IndexDirectory
)

$software = Get-CimInstance -ClassName Win32_Product -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$ravenservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$source = $DownloadURL
$destination = "C:\Temp\"
$downloaddestination = "C:\Temp\RavenDB.exe"
if(!(Test-Path $destination)){New-Item -Path $destination -ItemType Directory}
$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
$packageName = $package.FullName

if($ravenservice -ne $null) {
Write-Verbose "RavenDB Is Installed"
}
else{
Write-Verbose "Installing RavenDB"
if($package -ne $null) {
& $packageName /quiet /msicl "RAVEN_TARGET_ENVIRONMENT=$Environment TARGETDIR=$TargetDirectory INSTALLFOLDER=$InstallFolder RAVEN_DATA_DIR=$DataDirectory RAVEN_INDEX_DIR=$IndexDirectory RAVEN_INSTALLATION_TYPE=SERVICE REMOVE=IIS ADDLOCAL=Service"

$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue

if($checkfirewallrule -eq $null) {
Write-Verbose "Adding Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

else {
Write-Verbose "Firewall Rule All Exists"
}

}

else{
Write-Verbose "Downloading RavenDB"
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($source,$downloaddestination)

do{
$checkfile = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
Start-Sleep -Seconds 20
Write-Verbose "Waiting For File To Be Downloaded"
}

until($checkfile -ne $null)

$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
$packageName = $package.FullName
& $packageName /quiet /msicl "RAVEN_TARGET_ENVIRONMENT=$Environment TARGETDIR=$TargetDirectory INSTALLFOLDER=$InstallFolder RAVEN_DATA_DIR=$DataDirectory RAVEN_INDEX_DIR=$IndexDirectory RAVEN_INSTALLATION_TYPE=SERVICE REMOVE=IIS ADDLOCAL=Service"
$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue

if($checkfirewallrule -eq $null) {
Write-Verbose "Adding Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

else {
Write-Verbose "Firewall Rule All Exists"
}

}
}

}

function Remove-ScribestarRavenDB {
param(
[Parameter(Mandatory=$true)]
[string]$InstallFolder,
[Parameter(Mandatory=$true)]
[string]$DataDirectory,
[Parameter(Mandatory=$true)]
[string]$IndexDirectory
)

$software = Get-CimInstance -ClassName Win32_Product -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$ravenservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
if($ravenservice -ne $null){
Write-Verbose "Uninstalling RavenDB"
$ravenservice | Invoke-CimMethod -MethodName StopService
$ravenservice | Invoke-CimMethod -MethodName Delete
$InstallFol = Get-ChildItem -Path $InstallFolder -ErrorAction SilentlyContinue
$DataDir = Get-ChildItem -Path $DataDirectory -ErrorAction SilentlyContinue
$IndexDir = Get-ChildItem -Path $IndexDirectory -ErrorAction SilentlyContinue
$InstallFol | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue
$DataDir | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue
$IndexDir | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue

if($software -ne $null){$software | Invoke-CimMethod -MethodName Uninstall}
$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue
if($checkfirewallrule -ne $null){
Write-Verbose "Removing Firewall Rule"
$checkfirewallrule | Remove-NetFirewallRule
}

}

else{
Write-Verbose "RavenDB Is Uninstalled"

}

}

Export-ModuleMember -Function *-TargetResource

June 17, 2015 at 8:17 am

That's a lot of code to look at unformatted. Could you please either enclose it in PRE blocks as indicated above the posting form? Alternately, you can create a Gist from it (info at https://powershell.org/faq-page/) and just paste the Gist URL.

What sort of testing have you done? For example, you can manually load the module by using Import-Module and specifying the path to your PSM1 file. That would allow you to manually run your Get-, Set-, and Test- functions interactively, to see any errors that are occurring.

Have you considered not coding the firewall stuff yourself, but instead using the xFirewall resource?

June 17, 2015 at 10:51 am

Hi Don,

function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment
)

$Configuration = @()
$firewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue
$firewallruleenabled = $firewallrule.Enabled
$ravendbservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$installed = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$destination = "C:\Temp\"
$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}

if($installed -ne $null){
$Configuration+=@{RavenDBInstalled = $true}

}

else{
$Configuration+=@{RavenDBInstalled = $false}

}

if($firewallrule -ne $null){
$Configuration+=@{FirewallRuleExists = $true}
}

else{
$Configuration+=@{FirewallRuleExists = $false}
}

if($firewallruleenabled -eq "True"){
$Configuration+=@{FirewallRuleEnabled = $true}
}

else{
$Configuration+=@{FirewallRuleEnabled = $false}

}

if($ravendbservice.State -eq "Running"){
$Configuration+=@{RavenDBServiceRunning = $true}

}

else{
$Configuration+=@{RavenDBServiceRunning = $false}

}

return $Configuration

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

}

function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure,

[ValidateSet("Running","Stopped")]
[System.String]
$ServiceStatus,

[System.String]
$DownloadURL,

[System.String]
$TargetDirectory,

[System.String]
$InstallFolder,

[System.String]
$DataDirectory,

[System.String]
$IndexDirectory
)

$currentstate = Get-TargetResource $PSBoundParameters

if($currentstate.FirewallRuleExists -eq $true){Write-Verbose "Firewall Rule Exists"}

else{
Write-Verbose "Creating Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

if($currentstate.FirewallRuleEnabled -eq $true){Write-Verbose "Firewall Rule Is Enabled"}

else{
Write-Verbose "Enabling Firewall Rule"
Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080"| Enable-NetFirewallRule
}

if($ServiceStatus -eq "Running" -and $currentstate.RavenDBServiceRunning -eq $true){Write-Verbose "RavenDB Service Is Running"}

else{
Write-Verbose "Starting RavenDB Service"
Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" | Invoke-CimMethod -MethodName StartService -Verbose

}

if($Ensure -eq "Present" -and $currentstate.RavenDBInstalled -eq $true ){Write-Verbose "RavenDB Is Installed"}

else{
Write-Verbose "Installing RavenDB"
Install-ScribestarRavenDB -Environment $Environment -TargetDirectory $TargetDirectory -InstallFolder $InstallFolder -DataDirectory $DataDirectory -IndexDirectory $IndexDirectory -DownloadURL $DownloadURL

}

if($Ensure -eq "Absent" -and $currentstate.RavenDBInstalled -eq $false ){Write-Verbose "RavenDB Is Not Installed"}

else{
Write-Verbose "Removing RavenDB"
Remove-ScribestarRavenDB -InstallFolder $InstallFolder
}

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

#Include this line if the resource requires a system reboot.
#$global:DSCMachineStatus = 1

}

function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Environment,

[ValidateSet("Present","Absent")]
[System.String]
$Ensure,

[ValidateSet("Running","Stopped")]
[System.String]
$ServiceStatus,

[System.String]
$DownloadURL,

[System.String]
$TargetDirectory,

[System.String]
$InstallFolder,

[System.String]
$DataDirectory,

[System.String]
$IndexDirectory
)

$currentstate = Get-TargetResource $PSBoundParameters

if($currentstate.FirewallRuleExists -eq $true){
#Write-Verbose "Firewall Rule Exists"
return $true
}

else{
#Write-Verbose "Firewall Rule Does Not Exist"
return $false
}

if($currentstate.FirewallRuleEnabled -eq $true){
#Write-Verbose "Firewall Rule Is Enabled"
return $true
}

else{
#Write-Verbose "Firewall Rule Is Not Enabled"
return $false
}

if($ServiceStatus -eq "Running" -and $currentstate.RavenDBServiceRunning -eq $true){
#Write-Verbose "RavenDB Service Is Running"
return $true

}

else{
#Write-Verbose "RavenDB Service Is Not Running"
return $false
}

if($ServiceStatus -eq "Stopped" -and $currentstate.RavenDBServiceRunning -eq $false){
#Write-Verbose "RavenDB Service Is Not Running"
return $true

}

else{
#Write-Verbose "RavenDB Is Running"
return $false

}

if($Ensure -eq "Present" -and $currentstate.RavenDBInstalled -eq $true){

#Write-Verbose "RavenDB Is Present"
return $true
}

else{
#Write-Verbose "RavenDB Is Not Present"
return $false
}

if($Ensure -eq "Absent" -and $currentstate.RavenDBInstalled -eq $false){
#Write-Verbose "RavenDB Is Absent"
return $false
}

else{
#Write-Verbose "RavenDB Is Present"
return $true
}

#Write-Verbose "Use this cmdlet to deliver information about command processing."

#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

}

function Install-ScribestarRavenDB {
param(
[Parameter(Mandatory=$true)]
[string]$Environment,
[Parameter(Mandatory=$true)]
[string] $DownloadURL,
[Parameter(Mandatory=$true)]
[string]$TargetDirectory,
[Parameter(Mandatory=$true)]
[string]$InstallFolder,
[Parameter(Mandatory=$true)]
[string]$DataDirectory,
[Parameter(Mandatory=$true)]
[string]$IndexDirectory
)

$software = Get-CimInstance -ClassName Win32_Product -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$ravenservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$source = $DownloadURL
$destination = "C:\Temp\"
$downloaddestination = "C:\Temp\RavenDB.exe"
if(!(Test-Path $destination)){New-Item -Path $destination -ItemType Directory}
$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
$packageName = $package.FullName

if($ravenservice -ne $null) {
Write-Verbose "RavenDB Is Installed"
}
else{
Write-Verbose "Installing RavenDB"
if($package -ne $null) {
& $packageName /quiet /msicl "RAVEN_TARGET_ENVIRONMENT=$Environment TARGETDIR=$TargetDirectory INSTALLFOLDER=$InstallFolder RAVEN_DATA_DIR=$DataDirectory RAVEN_INDEX_DIR=$IndexDirectory RAVEN_INSTALLATION_TYPE=SERVICE REMOVE=IIS ADDLOCAL=Service"

$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue

if($checkfirewallrule -eq $null) {
Write-Verbose "Adding Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

else {
Write-Verbose "Firewall Rule All Exists"
}

}

else{
Write-Verbose "Downloading RavenDB"
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($source,$downloaddestination)

do{
$checkfile = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
Start-Sleep -Seconds 20
Write-Verbose "Waiting For File To Be Downloaded"
}

until($checkfile -ne $null)

$package = Get-ChildItem -Path $destination -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*RavenDB*"}
$packageName = $package.FullName
& $packageName /quiet /msicl "RAVEN_TARGET_ENVIRONMENT=$Environment TARGETDIR=$TargetDirectory INSTALLFOLDER=$InstallFolder RAVEN_DATA_DIR=$DataDirectory RAVEN_INDEX_DIR=$IndexDirectory RAVEN_INSTALLATION_TYPE=SERVICE REMOVE=IIS ADDLOCAL=Service"
$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue

if($checkfirewallrule -eq $null) {
Write-Verbose "Adding Firewall Rule"
New-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -Direction Inbound -Protocol TCP -Action Allow -LocalPort 8080
}

else {
Write-Verbose "Firewall Rule All Exists"
}

}
}

}

function Remove-ScribestarRavenDB {
param(
[Parameter(Mandatory=$true)]
[string]$InstallFolder,
[Parameter(Mandatory=$true)]
[string]$DataDirectory,
[Parameter(Mandatory=$true)]
[string]$IndexDirectory
)

$software = Get-CimInstance -ClassName Win32_Product -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
$ravenservice = Get-CimInstance -ClassName Win32_Service -Filter "Name='RavenDB'" -ErrorAction SilentlyContinue
if($ravenservice -ne $null){
Write-Verbose "Uninstalling RavenDB"
$ravenservice | Invoke-CimMethod -MethodName StopService
$ravenservice | Invoke-CimMethod -MethodName Delete
$InstallFol = Get-ChildItem -Path $InstallFolder -ErrorAction SilentlyContinue
$DataDir = Get-ChildItem -Path $DataDirectory -ErrorAction SilentlyContinue
$IndexDir = Get-ChildItem -Path $IndexDirectory -ErrorAction SilentlyContinue
$InstallFol | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue
$DataDir | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue
$IndexDir | Remove-Item -Recurse -Confirm:$false -Force -ErrorAction SilentlyContinue

if($software -ne $null){$software | Invoke-CimMethod -MethodName Uninstall}
$checkfirewallrule = Get-NetFirewallRule -DisplayName "RavenDB – Service – 8080" -ErrorAction SilentlyContinue
if($checkfirewallrule -ne $null){
Write-Verbose "Removing Firewall Rule"
$checkfirewallrule | Remove-NetFirewallRule
}

}

else{
Write-Verbose "RavenDB Is Uninstalled"

}

}

Export-ModuleMember -Function *-TargetResource

Apologies please find the below in the

 format. I have ran the each functions separately and they have not spewed any errors. I have created two separate custom resources using the same method and have not had the same issue with those. Maybe I should use the Firewall resource but I did not think couple of commands should cause for the set-targetresource not executing the firewall commands or starting the service, if the service had stopped or been stopped.