Hi powershell.org! I believe I have my final config in order, and a work-methodology for fellow colleagues/techs who will get involved with the project. I just wanted to refer my setup to you since in a lot of ways it is you who co-authored this with (heck in some cases FOR me). Hoping to get pointers to clean it up, and welcome any thoughts.
Master Config
get-dscresource
function GetAllServers {
import-module ActiveDirectory
Get-ADComputer -SearchBase “OU=DSCdev,DC=yourdc,DC=contoso,DC=com” -Filter *
}
$AllServers = GetAllServersfunction Get-NodeEnvironment {
[OutputType([string])]
param (
[Parameter(Mandatory)]
[string] $DistinguishedName
)switch -Wildcard ($DistinguishedName) { '*OU=Stage*' { return 'Stage' } '*OU=QA*' { return 'QA' } '*OU=DEV*' { return 'DEV' } default { return 'Production' } }
}
function Get-NodeRole {
[OutputType([string])]
param (
[Parameter(Mandatory)]
[string] $DistinguishedName
)$cn = ($DistinguishedName -split ',')[0] switch -Wildcard ($cn) { '*IIS*' { return 'WebServer' } '*DC*' { return 'ADDS' } '*SQL*' { return 'SQL' } default { return 'Vanila' } }
}
Gather AllNodes and add an environment variable to it based on OU name.
$AllServerConfigData = @{
AllNodes = @(
foreach ($node in $AllServers) {
@{
NodeName = $node.Name
NodeGUID = $node.objectGUID
NodeEnvironment = Get-NodeEnvironment -DistinguishedName $node.DistinguishedName
NodeRole = Get-NodeRole -DistinguishedName $node.DistinguishedName
}
}
)
}Configuration WindowsBackup {
WindowsFeature WindowsSbackup
{
Name = “Windows-Server-Backup”
Ensure = “Present”
IncludeAllSubFeature = “$True”
}
}Configuration Everything
{
Import-DscResource -name *Node $allnodes.where({$_.NodeRole -eq "WebServer"}).NodeGuid { MyIISPull AllWinFeatures {} } Node $allnodes.where({$_.NodeRole -eq "ADDS"}).NodeGuid { RdsServer TerminalServices {} } Node $allnodes.where({$_.NodeRole -eq "SQL"}).NodeGuid { rFileServer WinSharesYay {} RdsServer TerminalServicesSQL{} } Node $allnodes.where({$_.NodeEnvironment -eq "Production"}).NodeGuid { WindowsBackup BackupProductionAlways {} }
}
Everything -ConfigurationData $AllServerConfigData -outpath “C:\DSCCustom\WorkDir”
write-host “##################Creating checksums…######################”
New-DSCCheckSum -ConfigurationPath “C:\DSCCustom\WorkDir” -Verbose -ForceWrite-host “##################Copying configs to the pull service configuration store###############################”
$sourcefiles = “C:\DSCCustom\WorkDir**.mof*”
$sourcechecks = “C:\DSCCustom\WorkDir**.checksum*”
$targetfiles = “$env:SystemDrive\Program Files\WindowsPowershell\DscService\Configuration”
copy-item -Recurse $sourcefiles $targetfiles -Force -Verbose
copy-item -Recurse $sourcechecks $targetfiles -force -Verbose#Run the below to add a checksum to new modules you zip and place for download
#New-DSCCheckSum -ConfigurationPath “C:\Program Files\WindowsPowerShell\DscService\Modules” -Verbose -Force
Methodology for maintaining/updating composite resources
- Added a new path to system powershell profile:
#Save the current value in the $p variable. $p = [Environment]::GetEnvironmentVariable("PSModulePath") #Add the new path to the $p variable. Begin with a semi-colon separator. $p += ";C:\DSCCustom\Making Resources\CustomComposite\WindowsPowershell\Modules" #Add the paths in $p to the PSModulePath value. [Environment]::SetEnvironmentVariable("PSModulePath",$p)
- All my techs/admins who are involved in creating composite resources create them in the new environment location using a modified version of Steve’s “New-DscCompositeResource” script that I found here: [url]https://github.com/PowerShellOrg/DSC/blob/master/Tooling/DscDevelopment/New-DscCompositeResource.ps1[/url]
The modification is simply taking the original script and modifying line 20:
original
$Path = "$($env:ProgramFiles)\WindowsPowerShell\Modules",
new
$Path = "C:\DSCCustom\Making Resources\CustomComposite\WindowsPowershell\Modules",
So a summary: Powershell has two locations now for its own modules. The original under c:\programfiles\windowspowershell\modules (which I use for unzipped modules from the resource kit) and the c:\DSCCustom.…\ path I added so that techs don’t mess around extensively in program files, creating a ‘logical’ divide between system and custom work.
- The above works, when I use the script it creates the resource and the folder/file structure as needed and my techs modify the custom composite resources they made themselves. Get-DSC resource finds all the resources and when referencing them in the configuration as such a mof is generated.
Small bug:
Unless I add get-dscresource, OR I choose to add “import-dscresource -name *” to the master config, running the configuration will generate errors stating that my resources are “unknown commands” . Is this nromal/expected?