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 Get-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 Get-Coalesce:::Test { Get-Coalesce | Assert @() 1, '' | Get-Coalesce $null 'b' | Assert '1' $null, '' | Get-Coalesce $null 'b' | Assert 'b' $null, 0 | Get-Coalesce $null 'b' | Assert 0 $null, 1 | Get-Coalesce $null 'b' | Assert 1 $null, $null | Get-Coalesce $null 'c' | Assert 'c' $null, 'a' | Get-Coalesce $null 'b' | Assert 'a' $null, $null | Get-Coalesce 'b' 'c' | Assert 'b' $null, $null | Get-Coalesce $null 'c' | Assert 'c' $null, 'a' | Get-Coalesce '' 'b' -NullValues $null, 'a' | Assert '' } |