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 Add-Object_PropertyMember { Param ( [Parameter(ValueFromPipeline=$true)] $InputObject, $Name, $Value ) Process { if ($InputObject) { $member = Get-Member -InputObject $InputObject -Name $Name if ($member) { $UserInfo.$Name = $value } else { Add-Member -InputObject $InputObject -MemberType NoteProperty -Name $Name -Value $value } return $InputObject } } } function Add-Object_PropertyMember:::Test { [PSCustomObject]@{ A = 1} | Add-Object_PropertyMember -Name B -Value 2 | assert (@{ A = 1; B = 2}) [PSCustomObject]@{ A = 1; B = 3 } | Add-Object_PropertyMember -Name B -Value 2 | assert (@{ A = 1; B = 2}) } 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 '' } |