PoshCode Archive  Artifact [c380755e78]

Artifact c380755e78d6c96c05e90da4b6e3d46bbe42f2e2cc4fb92755e11eda4a2176db:

  • File New-Object.ps1 — part of check-in [8a8a22ec26] at 2018-06-10 13:38:02 on branch trunk — Proxy function for New-Object that allows you to specify a CLSID in the ComObject parameter. Example: (user: Matt Graeber size: 2640)

# encoding: ascii
# api: powershell
# title: New-Object
# description: Proxy function for New-Object that allows you to specify a CLSID in the ComObject parameter. Example:
# version: 0.1
# type: function
# author: Matt Graeber
# license: CC0
# function: New-Object
# x-poshcode-id: 4193
# x-archived: 2016-04-08T09:31:15
# x-published: 2016-06-06T09:48:00
#
# New-Object -ComObject 72C24DD5-D70A-438B-8A42-98424B88AFB8
#
function New-Object
{
    [CmdletBinding(DefaultParameterSetName='Net', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113355')]
    param(
        [Parameter(ParameterSetName='Net', Mandatory=$true, Position=0)]
        [string]
        ${TypeName},

        [Parameter(ParameterSetName='Com', Mandatory=$true, Position=0)]
        [string]
        ${ComObject},

        [Parameter(ParameterSetName='Net', Position=1)]
        [Alias('Args')]
        [System.Object[]]
        ${ArgumentList},

        [Parameter(ParameterSetName='Com')]
        [switch]
        ${Strict},

        [System.Collections.IDictionary]
        ${Property})

    begin
    {
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
            {
                $PSBoundParameters['OutBuffer'] = 1
            }

            $ClsidPresent = $false
            $Guid = [Guid]::NewGuid()
            if ([Guid]::TryParse($PSBoundParameters['ComObject'], [ref]$Guid))
            {
                $ClsidPresent = $true
            }
            else
            {
                $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('New-Object', [System.Management.Automation.CommandTypes]::Cmdlet)
                $scriptCmd = {& $wrappedCmd @PSBoundParameters }
                $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
                $steppablePipeline.Begin($PSCmdlet)
            }
        } catch {
            throw
        }
    }

    process
    {
        if ($ClsidPresent)
        {
            [Activator]::CreateInstance([Type]::GetTypeFromCLSID($Guid), $Property)
        }
        else
        {
            try {
                $steppablePipeline.Process($_)
            } catch {
                throw
            }
        }
    }

    end
    {
        if (!$ClsidPresent)
        {
            try {
                $steppablePipeline.End()
            } catch {
                throw
            }
        }
    }
    <#

    .ForwardHelpTargetName New-Object
    .ForwardHelpCategory Cmdlet

    #>
}