pf-basic.ps1
function Test-Administrator { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = new-object Security.Principal.WindowsPrincipal $identity $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Assert-Administrator { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = new-object Security.Principal.WindowsPrincipal $identity $result = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if ( -not $result ) { throw "Required Administrative privileges for '$($identity.Name)'" } } function Invoke-Dispose { param ( [ref]$obj ) process { if ($obj -and $obj.Value -and $obj.Value.Dispose) { $obj.Value.Dispose() $obj.Value = $null } } } function Remove-ComObject ($comObject) { [System.Runtime.InteropServices.Marshal]::ReleaseComObject( $comObject ) | Out-Null } function Get-Property { param( [Parameter(ValueFromPipeline=$true)] $path ) process { $item = $path | Get-PathItem foreach ($Property in $item.Property) { $prop = $item | Get-ItemProperty -Name $Property [PsCustomObject]@{Name = $Property; Value = $prop.$Property; Item = $Item} } } } function Get-Property:::Example { Get-ChildItem -path HKLM:\SOFTWARE -Recurse | Get-Property | Where-Object value -Like '*cts-ent*' } function Coalesce { $nullValues = Get-Argument 'NullValues' -default @($null, '') -remove foreach ($value in $input) { if ( $nullValues -notcontains $value ) { return $value } } foreach ($value in $args) { if ( $nullValues -notcontains $value ) { return $value } } } function Coalesce:::Test { Coalesce | Assert @() 1, '' | Coalesce $null 'b' | Assert '1' $null, '' | Coalesce $null 'b' | Assert 'b' $null, 0 | Coalesce $null 'b' | Assert 0 $null, 1 | Coalesce $null 'b' | Assert 1 $null, $null | Coalesce $null 'c' | Assert 'c' $null, 'a' | Coalesce $null 'b' | Assert 'a' $null, $null | Coalesce 'b' 'c' | Assert 'b' $null, $null | Coalesce $null 'c' | Assert 'c' $null, 'a' | Coalesce '' 'b' -NullValues $null, 'a' | Assert '' } |