Files
Matt Graeber 94751bc156 New-Object proxy function compatibility fix
I was calling the [Guid]::TryParse method that was only present in .NET
4 so this wasn't working in PowerShell v2.
2013-06-08 09:47:16 -04:00

90 lines
4.3 KiB
PowerShell

function New-Object
{
[CmdletBinding(DefaultParameterSetName='Net')]
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
{
Set-StrictMode -Version 2
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$ClsidPresent = $true
$Guid = [Guid]::NewGuid()
try
{
$Guid = [Guid] $PSBoundParameters['ComObject']
}
catch
{
$ClsidPresent = $false
$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
#>
}