Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created July 5, 2018 02:48
Show Gist options
  • Save IISResetMe/e14f24b266eef7a133903fdb5d7d6204 to your computer and use it in GitHub Desktop.
Save IISResetMe/e14f24b266eef7a133903fdb5d7d6204 to your computer and use it in GitHub Desktop.
Install-Module with a compulsory clobber check
[CmdletBinding(DefaultParameterSetName='NameParameterSet', SupportsShouldProcess=$true, ConfirmImpact='Medium', HelpUri='https://go.microsoft.com/fwlink/?LinkID=398573')]
param(
[Parameter(ParameterSetName='NameParameterSet', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string[]]
${Name},
[Parameter(ParameterSetName='InputObject', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[psobject[]]
${InputObject},
[Parameter(ParameterSetName='NameParameterSet', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[version]
${MinimumVersion},
[Parameter(ParameterSetName='NameParameterSet', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[version]
${MaximumVersion},
[Parameter(ParameterSetName='NameParameterSet', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[version]
${RequiredVersion},
[Parameter(ParameterSetName='NameParameterSet')]
[ValidateNotNullOrEmpty()]
[string[]]
${Repository},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[pscredential]
[System.Management.Automation.CredentialAttribute()]
${Credential},
[ValidateSet('CurrentUser','AllUsers')]
[string]
${Scope},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[uri]
${Proxy},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[pscredential]
[System.Management.Automation.CredentialAttribute()]
${ProxyCredential},
[switch]
${AllowClobber},
[switch]
${SkipPublisherCheck},
[switch]
${Force})
begin
{
if($PSCmdlet.ParameterSetName -eq 'InputObject'){
$Name = $InputObject.Name
}
if(-not $AllowClobber){
$Commands = (Get-Command).Name
$CommandNameCollisions = (Find-Command -ModuleName $Name).Name |Where {$Commands -contains $_}
if($CommandNameCollisions) {
Write-Error "Command name overlap detected in module {1}: {0}" -f ($CommandNameCollisions -join ', '),$Name
return
}
}
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Install-Module', [System.Management.Automation.CommandTypes]::Function)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline()
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
}
<#
.ForwardHelpTargetName Install-Module
.ForwardHelpCategory Function
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment