Last active
October 17, 2020 04:09
-
-
Save phbits/cf7eb08e6193817ac2476a5288154897 to your computer and use it in GitHub Desktop.
Generates a Service configuration for DSC based on the local system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-ServiceDscConfig | |
{ | |
<# | |
.SYNOPSIS | |
Generates a Service configuration for DSC based on the local system. | |
.EXAMPLE | |
Get-ServiceDscConfig | |
.EXAMPLE | |
Get-ServiceDscConfig -FilePath D:\SomeFolder\ | |
.EXAMPLE | |
Get-ServiceDscConfig -FilePath D:\SomeFolder\ -Width 100 | |
#> | |
[CmdletBinding()] | |
param( | |
[parameter(Mandatory=$false)] | |
[ValidateScript({Test-Path -Path $_ -PathType Container})] | |
[String] | |
# Folder to save new .ps1 file | |
$Folder = (Get-Location).Path | |
, | |
[parameter(Mandatory=$false)] | |
[ValidateRange(20,300)] | |
[Int] | |
# Description text width | |
$Width = 100 | |
) | |
$DestFile = Join-Path -Path $Folder -ChildPath "$($env:COMPUTERNAME)-ServiceDscConfig.ps1" | |
$FileContents1 = @" | |
Configuration DscServices | |
{ | |
# Generated: $(Get-Date) on $($env:COMPUTERNAME) | |
Import-DscResource -Module PSDscResources -Name Service | |
Node $env:COMPUTERNAME | |
{ | |
"@ | |
Out-File -FilePath $DestFile -InputObject $FileContents1 -Encoding ASCII -Force | |
$Services = Get-WmiObject Win32_Service | Sort-Object DisplayName | |
foreach($Service in $Services) | |
{ | |
$ServiceDscName = $Service.Name | |
$ServiceDescription = $Service.Description | |
$ServiceStart = $Service.StartMode | |
$ServiceState = 'Ignore' | |
if($ServiceStart.ToLower() -eq 'auto') | |
{ | |
$ServiceStart = 'Automatic' | |
} | |
# Ensure ServiceDscName meets ^[A-Za-z0-9_]{1,50}$ | |
if($ServiceDscName -notmatch "^[A-Za-z0-9_]{1,50}$") | |
{ | |
$ServiceDscNameArray = $ServiceDscName.ToCharArray() | |
$MaxLength = 50 | |
$ServiceDscName = '' | |
if($ServiceDscNameArray.Length -lt $MaxLength) | |
{ | |
$MaxLength = $ServiceDscNameArray.Length | |
} | |
for($i = 0; $i -lt $MaxLength; $i++) | |
{ | |
if($ServiceDscNameArray[$i] -match "^[a-zA-Z0-9_]{1}$") | |
{ | |
$ServiceDscName += $ServiceDscNameArray[$i] | |
} else { | |
# replace character with underscore | |
$ServiceDscName += '_' | |
} | |
} | |
} | |
# ServiceState should only be Stopped when StartMode=Disabled | |
# since not all services continuously run. | |
if($Service.StartMode -eq 'Disabled') | |
{ | |
$ServiceState = 'Stopped' | |
} | |
# Format service description width | |
if($ServiceDescription.Length -gt $Width) | |
{ | |
$Indent = ' ' | |
$ServiceDescription = '' | |
$WordArray = $Service.Description.Split(' ') | |
$SpaceLeft = $Width | |
foreach($word in $WordArray) | |
{ | |
if($($word.Length + 1) -gt $SpaceLeft) | |
{ | |
$ServiceDescription += "`n$Indent$word " | |
$SpaceLeft = $Width - $($word.Length + 1) | |
} else { | |
$ServiceDescription += "$word " | |
$SpaceLeft -= $($word.Length + 1) | |
} | |
} | |
} | |
$Entry = @" | |
Service $($ServiceDscName) | |
{ | |
<# | |
DisplayName = $($Service.DisplayName) | |
Description = $($ServiceDescription) | |
Path = $($Service.PathName) | |
ServiceType = $($Service.ServiceType) | |
Account = $($Service.StartName) | |
#> | |
Name = '$($Service.Name)' | |
StartupType = '$($ServiceStart)' | |
State = '$($ServiceState)' | |
} | |
"@ | |
Out-File -FilePath $DestFile -InputObject $Entry -Encoding ASCII -Append | |
} | |
$FileContents2 = @' | |
} | |
} | |
DscServices -Verbose | |
'@ | |
Out-File -FilePath $DestFile -InputObject $FileContents2 -Encoding ASCII -Append | |
Write-Output "`nFile written to: $DestFile`n" | |
} # End Function Get-ServiceDscConfig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment