---
title: Chroma
date: 0001-01-01 00:00:00 +0000 UTC
draft: false
author: John Roos
----
title: Chroma
date: 0001-01-01 00:00:00 +0000 UTC
draft: false
author: John Roos
----
Code highlight demo using Chroma.
#REQUIRES -Version 4.0
<#
.Synopsis
Reads an ini file and creates an object based on the content of the file
.DESCRIPTION
Reads an ini file and creates an object based on the content of the file. One property per key/value. Sections will be named with surrounding brackets and will contain a list of objects based on the keys within that section.
Comments will be ignored.
Created by John Roos
Web: http://blog.roostech.se
.EXAMPLE
Get-Ini -Path "C:\config.ini"
Opens the file config.ini and creates an object based on that file.
.OUTPUTS
Outputs an custom object of the type File.Ini
#>
function Get-Ini {
[CmdletBinding(DefaultParameterSetName='Set1')]
param (
# Enter the path for the ini file
[Parameter(Mandatory,
ValueFromPipelineByPropertyName=$true,
Position=0,
ParameterSetName='Set1',
HelpMessage='Enter the path for the ini file')]
[ValidateScript({Test-Path $_})]
[string]$Path
)
Process
{
$iniFile = Get-Content $Path -Verbose:$false
$currentSection = ''
$currentKey = ''
$currentValue = ''
[hashtable]$iniSectionHash = [ordered]@{}
[hashtable]$iniConfigArray = [ordered]@{}
foreach ($line in $iniFile) {
if ( $line.Trim().StartsWith('[') -and $line.EndsWith(']') ) {
Write-Verbose "Found new section."
if ($currentSection -ne ''){
Write-Verbose "Creating section property based on array:"
$keyobj = New-Object PSObject -Property $iniConfigArray
$keyobj.PSObject.TypeNames.Insert(0,'File.Ini.Config')
$iniSectionHash.Add($currentSection,$keyobj)
[hashtable]$iniConfigArray = @{}
Write-Verbose "Created section property: $currentSection"
}
if ($iniConfigArray.count -gt 0) {
$rootSection = $iniConfigArray
[hashtable]$iniConfigArray = [ordered]@{}
}
$currentSection = $line
Write-Verbose "Current section: $currentSection"
continue
}
Write-Verbose "Parsing line: $line"
if ( $line.Contains('=') ){
$keyvalue = $line.Split('=')
[string]$currentKey = $keyvalue[0]
[string]$currentValue = $keyvalue[1]
$valuehash = @{
$currentKey = $currentValue
}
$iniConfigArray.Add($currentKey, $currentValue)
Write-Verbose "Added keyvalue: $($keyvalue[0]) = $($keyvalue[1])"
}
<# below was for handling comments, but I wont do it...
elseif ($line.Contains('#') -or $line.Contains(';')) {
[string]$currentKey = $line
[string]$currentValue = ""
$valuehash = @{
$currentKey = $currentValue
}
$iniConfigArray.Add($currentKey, $currentValue)
Write-Verbose "Added comment: $currentKey"
}#>
}
$keyobj = New-Object PSObject -Property $iniConfigArray
$keyobj.PSObject.TypeNames.Insert(0,'File.ini.Section')
$iniSectionHash.Add($currentSection,$keyobj)
Write-Verbose "Created last section property: $currentSection"
$result = New-Object PSObject -Property $iniSectionHash
if ($rootSection) {
foreach ($key in $rootSection.keys){
Add-Member -InputObject $result -MemberType NoteProperty -Name $key -Value $rootSection.$key
}
}
$result.PSObject.TypeNames.Insert(0,'File.ini')
Return $result
}
}
# Classes
class ComputerPSOverload1
{
[string] $ComputerName
[string] $Domain
[string] GetFQDN() {
if ($this.ComputerName -ne '' -and $this.Domain -ne ''){
return "$($this.ComputerName).$($this.Domain)"
}
}
[string] GetFQDN([string]$ComputerName, [string]$Domain) {
if ($ComputerName -ne '' -and $Domain -ne ''){
return "$ComputerName.$Domain"
}
}
}
class ComputerPSOverload2 : ComputerPSOverload1
{
[string] GetFQDN([string]$prefix) {
if ($this.ComputerName -ne '' -and $this.Domain -ne ''){
return "$prefix$($this.ComputerName).$($this.Domain)"
}
}
}
# Pester
Describe 'Pester 5 Example' {
foreach ($number in 1..10) {
It "$number is a number" -TestCases @{'digit' = $number} {
$digit | Should -BeOfType [int]
}
}
}
# Discovery
BeforeAll {
# Run
}
Describe 'Discovery' -Tag 'Discovery' -Skip:$Discovery {
# Discovery
BeforeAll {
# Run
}
BeforeEach {
# Run
}
Context 'Discovery' -Tag 'Discovery' -Skip:$Discovery {
BeforeAll {
# Run
}
BeforeEach {
# Run
}
# Discovery
It "Discovery" -Tag 'Discovery' -TestCases @{'Discovery' = 'Discovery'} -Skip:$Discovery {
# Run
}
AfterEach {
# Run
}
AfterAll {
# Run
}
}
AfterEach {
# Run
}
AfterAll {
# Run
}
}
AfterAll {
# Run
}
# Discovery
# Strings
$str = @"
This is my $string. There are ${many} like it, but this one is mine.
"@
$str = @'
This is my $string. There are ${many} like it, but this one is mine.
'@
$str = "This is my $string. There are ${many} like it, but this one is mine."
$str = 'This is my $string. There are ${many} like it, but this one is mine.'
Chroma
Code highlight demo using Chroma.
#REQUIRES -Version 4.0
<#
.Synopsis
Reads an ini file and creates an object based on the content of the file
.DESCRIPTION
Reads an ini file and creates an object based on the content of the file. One property per key/value. Sections will be named with surrounding brackets and will contain a list of objects based on the keys within that section.
Comments will be ignored.
Created by John Roos
Web: http://blog.roostech.se
.EXAMPLE
Get-Ini -Path "C:\config.ini"
Opens the file config.ini and creates an object based on that file.
.OUTPUTS
Outputs an custom object of the type File.Ini
#>
function Get-Ini {
[CmdletBinding(DefaultParameterSetName='Set1')]
param (
# Enter the path for the ini file
[Parameter(Mandatory,
ValueFromPipelineByPropertyName=$true,
Position=0,
ParameterSetName='Set1',
HelpMessage='Enter the path for the ini file')]
[ValidateScript({Test-Path $_})]
[string]$Path
)
Process
{
$iniFile = Get-Content $Path -Verbose:$false
$currentSection = ''
$currentKey = ''
$currentValue = ''
[hashtable]$iniSectionHash = [ordered]@{}
[hashtable]$iniConfigArray = [ordered]@{}
foreach ($line in $iniFile) {
if ( $line.Trim().StartsWith('[') -and $line.EndsWith(']') ) {
Write-Verbose "Found new section."
if ($currentSection -ne ''){
Write-Verbose "Creating section property based on array:"
$keyobj = New-Object PSObject -Property $iniConfigArray
$keyobj.PSObject.TypeNames.Insert(0,'File.Ini.Config')
$iniSectionHash.Add($currentSection,$keyobj)
[hashtable]$iniConfigArray = @{}
Write-Verbose "Created section property: $currentSection"
}
if ($iniConfigArray.count -gt 0) {
$rootSection = $iniConfigArray
[hashtable]$iniConfigArray = [ordered]@{}
}
$currentSection = $line
Write-Verbose "Current section: $currentSection"
continue
}
Write-Verbose "Parsing line: $line"
if ( $line.Contains('=') ){
$keyvalue = $line.Split('=')
[string]$currentKey = $keyvalue[0]
[string]$currentValue = $keyvalue[1]
$valuehash = @{
$currentKey = $currentValue
}
$iniConfigArray.Add($currentKey, $currentValue)
Write-Verbose "Added keyvalue: $($keyvalue[0]) = $($keyvalue[1])"
}
<# below was for handling comments, but I wont do it...
elseif ($line.Contains('#') -or $line.Contains(';')) {
[string]$currentKey = $line
[string]$currentValue = ""
$valuehash = @{
$currentKey = $currentValue
}
$iniConfigArray.Add($currentKey, $currentValue)
Write-Verbose "Added comment: $currentKey"
}#>
}
$keyobj = New-Object PSObject -Property $iniConfigArray
$keyobj.PSObject.TypeNames.Insert(0,'File.ini.Section')
$iniSectionHash.Add($currentSection,$keyobj)
Write-Verbose "Created last section property: $currentSection"
$result = New-Object PSObject -Property $iniSectionHash
if ($rootSection) {
foreach ($key in $rootSection.keys){
Add-Member -InputObject $result -MemberType NoteProperty -Name $key -Value $rootSection.$key
}
}
$result.PSObject.TypeNames.Insert(0,'File.ini')
Return $result
}
}
# Classes
class ComputerPSOverload1
{
[string] $ComputerName
[string] $Domain
[string] GetFQDN() {
if ($this.ComputerName -ne '' -and $this.Domain -ne ''){
return "$($this.ComputerName).$($this.Domain)"
}
}
[string] GetFQDN([string]$ComputerName, [string]$Domain) {
if ($ComputerName -ne '' -and $Domain -ne ''){
return "$ComputerName.$Domain"
}
}
}
class ComputerPSOverload2 : ComputerPSOverload1
{
[string] GetFQDN([string]$prefix) {
if ($this.ComputerName -ne '' -and $this.Domain -ne ''){
return "$prefix$($this.ComputerName).$($this.Domain)"
}
}
}
# Pester
Describe 'Pester 5 Example' {
foreach ($number in 1..10) {
It "$number is a number" -TestCases @{'digit' = $number} {
$digit | Should -BeOfType [int]
}
}
}
# Discovery
BeforeAll {
# Run
}
Describe 'Discovery' -Tag 'Discovery' -Skip:$Discovery {
# Discovery
BeforeAll {
# Run
}
BeforeEach {
# Run
}
Context 'Discovery' -Tag 'Discovery' -Skip:$Discovery {
BeforeAll {
# Run
}
BeforeEach {
# Run
}
# Discovery
It "Discovery" -Tag 'Discovery' -TestCases @{'Discovery' = 'Discovery'} -Skip:$Discovery {
# Run
}
AfterEach {
# Run
}
AfterAll {
# Run
}
}
AfterEach {
# Run
}
AfterAll {
# Run
}
}
AfterAll {
# Run
}
# Discovery
# Strings
$str = @"
This is my $string. There are ${many} like it, but this one is mine.
"@
$str = @'
This is my $string. There are ${many} like it, but this one is mine.
'@
$str = "This is my $string. There are ${many} like it, but this one is mine."
$str = 'This is my $string. There are ${many} like it, but this one is mine.'
- Written by John Roos