** Note ** Apologies for the late post, was working on getting one book out and just finished up the technical material for another book. ** End Note **
When creating scripts for documentation purposes, consistent coding and consistent output make for an easier read after the script is done and possibly days or weeks after as well. For the sample code below the script is documenting some settings in the Security and Compliance Center. However, the coding process could be potentially used on any workload in Office 365 or PowerShell accessible server or system.
Code as Built
First, we top the block with a line for commenting, short quick description Next, we have three lines for output to a txt file for documentation/review and in a patter of a clear line for spacing output, a description of the cmdlet run and a line of ‘-‘ to underline that for formatting/separation. Then there is a line for screen output so that the operator knows what task the PowerShell script is performing. Finally, the last line is the cmdlet, with any properties needed and exported to the txt file as well.
Example Block 1
[sourcecode language=”powershell”]
# Get-DeviceTenantPolicy
$Line = ' ' | Out-file $Destination -Append
$Line = "DeviceTenantPolicy" | Out-file $Destination -Append
$Line = "——————————–" | Out-file $Destination -Append
Write-Host ' * Get-DeviceTenantPolicy in-progress' -ForegroundColor Yellow
$Line = Get-DeviceTenantPolicy | Ft Name, Type, IsValid, Mode, ReadOnly, DistributionStatus, Workload, Priority, GUID, Comment
[/sourcecode]
Then, if we follow this up with another cmdlet, the ‘Get-DeviceTenantRule’ cmldlet, we can build another similar block of code:
[sourcecode language=”powershell”]
# Get-DeviceTenantRule
$Line = ' ' | Out-file $Destination -Append
$Line = "DeviceTenantRule" | Out-file $Destination -Append
$Line = "——————————–" | Out-file
$Destination -Append Write-Host ' * Get-DeviceTenantRule in-progress' -ForegroundColor Yellow
$Line = DeviceTenantRule | Out-file $Destination -Append
[/sourcecode]
As we can see, the blocks follow the same pattern and will produce a predictable output. Why do this? Imagine a script that calls on 50 cmdlets. If there were a consistent pattern to the output, analyzing the file would be much easier. Taking this a step further, cmdlets could be grouped by type or run alphabetically as well.