PoshCode Archive  Artifact Content

Artifact 2bee156839f6460038bb318962c0bb8b27e35456d0436ebd7fee94fcb92841e9:

  • File Set-PSObjectDefaultPrope.ps1 — part of check-in [4064869c88] at 2018-06-10 12:58:06 on branch trunk — Set-PSObjectDefaultProperties sets the default properties for a PSObject. This is for PowerShell V2 to work around a bug with V2. (user: unknown size: 1473)

# encoding: ascii
# api: powershell
# title: Set-PSObjectDefaultPrope
# description: Set-PSObjectDefaultProperties sets the default properties for a PSObject. This is for PowerShell V2 to work around a bug with V2.
# version: 1.0
# type: function
# license: CC0
# function: Set-PSObjectDefaultProperties
# x-poshcode-id: 1523
# x-archived: 2009-12-14T21:28:01
#
#
function Set-PSObjectDefaultProperties {
    param(
          [PSObject]$Object,
          [string[]]$DefaultProperties
         )
    
    $name = $Object.PSObject.TypeNames[0]     
    
    $xml = "<?xml version='1.0' encoding='utf-8' ?><Types><Type>"
    
    $xml += "<Name>$($name)</Name>"
    
    $xml += "<Members><MemberSet><Name>PSStandardMembers</Name><Members>"
    
    $xml += "<PropertySet><Name>DefaultDisplayPropertySet</Name><ReferencedProperties>"
    
    foreach( $default in $DefaultProperties ) {
        $xml += "<Name>$($default)</Name>"
    }
    
    $xml += "</ReferencedProperties></PropertySet></Members></MemberSet></Members>"

	$xml += "</Type></Types>"
    
    $file = "$($env:Temp)\$name.ps1xml"
    
    Out-File -FilePath $file -Encoding "UTF8" -InputObject $xml -Force
    
    $typeLoaded = $host.Runspace.RunspaceConfiguration.Types | where { $_.FileName -eq  $file }
    
    if( $typeLoaded -ne $null ) {
        Write-Host "Type Loaded"
        Update-TypeData
    }
    else {
        Update-TypeData $file
    }
}