Private/ServiceUtil.ps1
# Determines if a Service exists with a name as defined in $ServiceName. # Returns a boolean $True or $False. Function Test-ServiceExists([string] $ServiceName) { [bool] $Return = $False if ( Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" ) { $Return = $True } Return $Return } # Deletes a Service with a name as defined in $ServiceName. # Returns a boolean $True or $False. $True if the Service didn't exist or was # successfully deleted after execution. Function Remove-Service([string] $ServiceName) { [bool] $Return = $False $Service = Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" if ( $Service ) { Remove-CimInstance -InputObject $Service if ( -Not ( Test-ServiceExists $ServiceName ) ) { $Return = $True } } else { $Return = $True } Return $Return } |