|
function Invoke-MachineAccelerator { |
|
param( |
|
[Parameter(Position = 0)] |
|
[System.Management.Automation.ValidateSet( |
|
"ChildItem", "Service", "Process", "NetAdapter", "ipconfig" |
|
)] |
|
[System.Management.Automation.Alias('Type')] |
|
[System.String] $psCommandType |
|
) |
|
|
|
dynamicparam |
|
{ |
|
# Define common parameters |
|
#$commonParameters = [System.Management.Automation.Internal.CommonParameters].DeclaredProperties.Name |
|
|
|
# Define more stateful lookups to be ran |
|
#$command = $MyInvocation.MyCommand.Name -replace "LookupHandlerReplacementFor<<(.*)>>", '$1' |
|
#$machineType = $command -replace "^([a-zA-Z]{2,3}[0-9]+)([a-zA-Z]{3,4}[\d]+)([a-zA-Z]{2})([\d]+)$", '$3' |
|
|
|
# Define which parameters to apply to the command accelerator |
|
$this_lookupAction, $this_lookupActionParameterBinding = switch ($psCommandType) { |
|
"ChildItem" { "Get-ChildItem", $null } |
|
"Service" { "Get-Service", $null } |
|
"Process" { "Get-Process", $null } |
|
"NetAdapter" { "Get-NetAdapter", $null } |
|
"ipconfig" { |
|
{ |
|
[CmdletBinding()] |
|
param( |
|
$args |
|
) |
|
ipconfig $args |
|
}, $null |
|
} |
|
{ $psCommandType -eq $null } { |
|
$sb = { |
|
Get-ComputerInfo |
|
} |
|
$sb, $null |
|
} |
|
|
|
default { |
|
$null |
|
} |
|
} |
|
|
|
|
|
if ($this_lookupAction -is [ScriptBlock]) { |
|
# Use ScriptBlock |
|
$this_command = $this_lookupAction |
|
$this_commandName = "{ ScriptBlock }" |
|
$this_commandParameters = [scriptblock].GetProperties('Instance, NonPublic').Where({$_.Name -like 'RuntimeDefinedParameters'}).GetValue($this_command) |
|
} else { |
|
# Command Lookup |
|
$this_command = Microsoft.PowerShell.Core\Get-Command -Name $this_lookupAction |
|
$this_commandName = "{0}\{1}" -f $this_command.ModuleName, $this_command.Name |
|
$this_commandParameters = $this_command.Parameters |
|
} |
|
|
|
# Lookup the full command here |
|
# Export the Command Parameter Dictionary |
|
# Note: |
|
# Parameters with a Position Attribute will not show up in CommandCompletion however they still exist |
|
# |
|
$_paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary |
|
|
|
foreach ($_param in $this_commandParameters.GetEnumerator()) |
|
{ |
|
if ($null -ne $this_lookupActionParameterBinding -and $_param.Value.Name -eq $this_lookupActionParameterBinding) { continue } |
|
if ($_param.Value.Name -in [System.Management.Automation.Cmdlet]::CommonParameters ) { continue } |
|
|
|
$_attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]$_param.Value.Attributes |
|
|
|
$_parameter = New-Object System.Management.Automation.RuntimeDefinedParameter($_param.Value.Name, $_param.Value.ParameterType, $_attributeCollection) |
|
$_paramDictionary.Add($_param.Value.Name, $_parameter) |
|
} |
|
|
|
return $_paramDictionary |
|
} |
|
|
|
begin { |
|
if ($MyInvocation.MyCommand.Name -eq "Invoke-GetCentralAdminMachineAccelerator") { |
|
throw "Invoke-GetCentralAdminMachineAccelerator is not meant to be called directly. It is a proxy command." |
|
} |
|
|
|
# Bind the mapped parameter to the command to be ran |
|
$null = $PSBoundParameters.Remove("psCommandType") |
|
|
|
$commandName = $MyInvocation.MyCommand.Name -replace "LookupHandlerReplacementFor<<(.*)>>", '$1' |
|
|
|
if ($null -ne $this_lookupActionParameterBinding) { |
|
$PSBoundParameters[$this_lookupActionParameterBinding] = $commandName |
|
} |
|
|
|
# Show the actual command being ran. |
|
$_stringBoundParameters = $PSBoundParameters.GetEnumerator() | ForEach-Object { "-$($_.Key) $($_.Value)" } |
|
$_stringBoundParameters = $_stringBoundParameters -join " " |
|
Write-Host "Invoking: $this_commandName $_stringBoundParameters" -ForegroundColor Cyan |
|
|
|
if (-not $this_lookupActionParameterBinding -and $commandName -ne 'localhost') { |
|
Invoke-Command -ScriptBlock $this_command -ComputerName $commandName -ArgumentList $PSBoundParameters |
|
return |
|
} |
|
. $this_command @PSBoundParameters |
|
} |
|
|
|
process { |
|
|
|
|
|
} |
|
|
|
end { |
|
Write-Host 'Completed' |
|
} |
|
} |
|
|
|
# Lets create an accelerator |
|
$global:PSPreCommandLookupAction += @{ |
|
Name = "Pattern Match on machine" |
|
Description = "Matches pattern on MachineName" |
|
|
|
# Filter is <Datacenter><Forest><MachineType><MachineNumber> |
|
# Example PHX01FOREST01DC001 |
|
Filter = { $command -match "^([a-zA-Z]+[\d]+)([a-zA-Z]+[\d]+)([a-zA-Z]+)([\d]+)$|localhost"} |
|
|
|
Priority = -1 |
|
StopSearch = $true |
|
ScriptBlock = (Get-Command "Invoke-MachineAccelerator").ScriptBlock |
|
} |