Private/ISE-Snippets.ps1

$Title       = "1.1 LAB - Script-Folder"
$Description = "Build a folder with script"
$SnippetText = @'
Function Ensure-Folder{
     
    $Folder = "C:\Logging"
 
    # Test-Resource
    If(Test-Path $Folder){
       "Folder present :-)"
    }
 
    # Set-Resource
    Else{
       "No folder Present!"
        New-Item -Path $Folder -ItemType Directory
    }
}
 
Ensure-Folder
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "1.2 LAB - Look at Built-In DSC Resources"
$Description = "Native DSC Resources in Windows"
$SnippetText = @'
Get-DscResource -Module PSDesiredStateConfiguration
 
Get-DscResource -Name "File"
Get-DscResource -Name "File" -Syntax
Get-DscResource -Name "File" | Select-Object -ExpandProperty Properties
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "1.3 LAB - Test Built-In DSC Resources"
$Description = "Native DSC Resources in Windows verification"
$SnippetText = @'
$ResourceParameters = @{
    DestinationPath = 'C:\Loggar';
    Type = 'Directory';
}
 
Invoke-DscResource -Name "File" -Method Test -Property $ResourceParameters -ModuleName PSDesiredStateConfiguration -Verbose
 
Invoke-DscResource -Name "File" -Method Set -Property $ResourceParameters -ModuleName PSDesiredStateConfiguration -Verbose
 
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "2.1 LAB - Local Configuration Manager (LCM)"
$Description = "Configuring the DSC LCM"
$SnippetText = @'
#Look at LCM Properties
Get-DscLocalConfigurationManager
Get-DscLocalConfigurationManager | Select *
 
# Create LCM Properties: https://docs.microsoft.com/en-us/powershell/dsc/metaconfig
[DSCLocalConfigurationManager()]
configuration LCM
{
    Node localhost
    {
        Settings
        {
            ConfigurationMode = "ApplyAndAutoCorrect"
            RefreshMode = 'Push'
            DebugMode = 'All'
            RebootNodeIfNeeded = $true
        }
    }
}
 
# Create mof
LCM -OutputPath $env:TEMP
 
ise $env:TEMP\localhost.meta.mof
 
# Configure the DSCLocalConfigurationManager
Set-DSCLocalConfigurationManager -Path $env:TEMP –Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "3.1 LAB - DSC-Folder"
$Description = "Build a folder with DSC"
$SnippetText = @'
Configuration DSCFolder {
 
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    File LoggingFolder {
        Ensure = "Present"
        DestinationPath = "C:\Logging"
        Type = "Directory"
    }
}
 
DSCFolder -OutputPath $env:TEMP
 
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "3.2 LAB - DSC-Folder Add dependencies"
$Description = "Build a folder with file DSC"
$SnippetText = @'
Configuration DSCFolder
{
 
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    # Create a directory
    File LoggingFolder {
        Ensure = "Present"
        Type = "Directory"
        DestinationPath = "C:\Logging"
    }
 
    # Create a file
    File LoggingFile {
        Ensure = "Present"
        DestinationPath = "C:\Logging\Log.txt"
        Contents = "Hello World!"
        DependsOn = "[File]LoggingFolder"
    }
}
 
DSCFolder -OutputPath $env:TEMP
 
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "4.1 LAB - DSC diagnostics"
$Description = "Troubleshooting DSC"
$SnippetText = @'
#NOTE: https://docs.microsoft.com/en-us/powershell/dsc/troubleshooting
 
Get-DscConfigurationStatus | Show-Object
 
$Status = Get-DscConfigurationStatus
$Status.ResourcesInDesiredState
$status.MetaConfiguration
 
Get-DscConfiguration
 
Get-WinEvent -LogName "Microsoft-Windows-Dsc/Operational"
         
Install-Module xDscDiagnostics -Force -Verbose -RequiredVersion 2.6.0
 
Update-xDscEventLogStatus -Channel Debug -Status Enabled
 
Get-xDscOperation
 
Trace-xDscOperation -JobId <JobId>
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "5.1 LAB - Certificate for DSC encyprion"
$Description = "Build a Docusign Encryption certificate"
$SnippetText = @'
$Params = @{
    Subject = "CN=DscEncryption"
    DnsName = "DscEncryption"
    FriendlyName = 'DSC Encryption Certifificate'
    TextExtension = @('2.5.29.37={text}1.3.6.1.4.1.311.80.1')
    KeyUsage = @('KeyEncipherment', 'DataEncipherment')
    CertStoreLocation = "Cert:\LocalMachine\My"
    KeyLength = 2048
    KeyAlgorithm = 'RSA'
    HashAlgorithm = 'SHA256'
    NotBefore = (Get-Date)
    NotAfter = (Get-Date).AddYears(2)
}
 
$cert = New-SelfSignedCertificate @Params
$cert | Export-Certificate –FilePath 'C:\DSCEncrypt.cer'
Import-Certificate -FilePath 'C:\DSCEncrypt.cer' -CertStoreLocation Cert:\LocalMachine\Root
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params


$Title       = "5.2 LAB - Set LCM to use certificate"
$Description = "Use a Docusign Encryption certificate in LCM"
$SnippetText = @'
[DSCLocalConfigurationManager()]
Configuration LCM
{
    Param(
        $CertificateThumbprint
    )
    
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    Node localhost
    {
        Settings
        {
            CertificateId = $CertificateThumbprint
        }
    }
}
 
LCM -OutputPath $env:TEMP -CertificateThumbprint $cert.Thumbprint
Set-DscLocalConfigurationManager -Path $env:TEMP
'@

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params

$Title       = "5.3 LAB - Encrypt configuration and test"
$Description = "Create an encrypted configuration and test on Node"
$SnippetText = '@
Configuration EncryptedFile
{
    Param(
        [Parameter(Mandatory)]
        [ValidateNotNullorEmpty()]
        [PsCredential]$credential
    )
    
    Import-DscResource –ModuleName PSDesiredStateConfiguration
 
    Node $AllNodes.NodeName
    {
        File EncryptedInfo
        {
            DestinationPath = "C:\EncryptTest.txt"
            Contents = $credential.GetNetworkCredential().password
            Credential = $credential
        }
    }
 
}
 
$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName = "localhost"
            PSDscAllowDomainUser = $true
            CertificateFile = "C:\DSCEncrypt.cer"
        }
    )
}
 
# Create MOF job
$Cred = Get-Credential -UserName localhost\administrator -Message "Admin Credential"
EncryptedFile -OutputPath $env:TEMP -Credential $Cred -ConfigurationData $ConfigurationData
 
# Verify encrypted MOF
ise "$env:TEMP\localhost.mof"
 
# Run the MOF job
Start-DscConfiguration -Path $env:TEMP -Wait -Force -Verbose
 
# Get content of created file
Get-Content "C:\EncryptTest.txt"
@'

$Params = @{
    Force = $true
    Title = $Title
    Description = $Description
    Text = $SnippetText
}
New-IseSnippet @Params