Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requires -version 3.0
- sal C0 %;sal C1 Expand-Alias;sal C2 Export-ModuleMember
- sal C3 Get-Content;sal C4 Get-History;sal C5 Microsoft.PowerShell.Core\Get-Command
- sal C6 Microsoft.PowerShell.Core\Get-Module;sal C7 Microsoft.PowerShell.Utility\Get-Alias;sal C8 New-Object
- sal C9 Resolve-Command;sal C10 Select;sal C11 Select-Object
- sal C12 Set-Alias;sal C13 Set-Content;sal C14 Set-StrictMode
- sal C15 Where-Object;sal C16 Write-Debug;sal C17 Write-Verbose
- sal C18 Write-Warning
- C14 -Version latest
- function Resolve-Command {#.Synopsis
- [CmdletBinding()]
- param([Parameter(Mandatory=$True)]
- [String]$Name,
- [String[]]$AllowedModule=$(@(C6 | C10 -Expand Name) + 'Microsoft.PowerShell.Core'),
- [Parameter()]
- [string[]]$AllowedCommand)
- end {$V0 = $Name -replace '(.)$','[$1]'
- $V1 = @(C5 $V0 -Module $AllowedModule -ErrorAction SilentlyContinue)
- if(!$V1) {if($V2 = $AllowedCommand -match ("^[^-\\]*\\*" + $([Regex]::Escape($Name)))) {$OFS = ", "
- C17 ("Commands is empty, but AllowedCommand (" + $AllowedCommand + ") contains " + $Name + ", so:")
- $V1 = @(C5 $V2)}}
- $V3 = $NULL
- if($V1) {C17 ("Commands " + $($V1|C0 {$_.ModuleName + '\' + $_.Name}))
- if($V1.Count -gt 1) {$V3 = @($V1 | C15 {$_.Name -match ("^" + $([Regex]::Escape($Name)))})[0]} else {$V3 = $V1[0]}}
- if(!$V3 -and !$V0.Contains("-")) {$V1 = @(C5 ("Get-" + $V0) -ErrorAction SilentlyContinue -Module $AllowedModule | C15 {$_.Name -match ("^Get-" + $([Regex]::Escape($Name)))})
- if($V1) {if($V1.Count -gt 1) {$V3 = @($V1 | C15 {$_.Name -match ("^" + $([Regex]::Escape($Name)))})[0]} else {$V3 = $V1[0]}}}
- if(!$V3 -or $V3.CommandType -eq "Alias") {if(($V4 = C7 $Name -ErrorAction SilentlyContinue)) {if($V4 = $V4.ResolvedCommand) {$V3 = C9 $V4 -AllowedModule $AllowedModule -AllowedCommand $AllowedCommand -ErrorAction SilentlyContinue}}}
- if(!$V3) {if($PSBoundParameters.ContainsKey("AllowedModule")) {C18 ("No command '" + $Name + "' found in the allowed modules.")} else {C18 ("No command '" + $Name + "' found in allowed modules. Expand-Alias defaults to only loaded modules, specify -AllowedModule `"*`" to allow ANY module")}
- C17 ("The current AllowedModules are: " + $($AllowedModule -join ', '))}
- return $V3}}
- function Protect-Script {#.Synopsis
- [CmdletBinding(ConfirmImpact="low",DefaultParameterSetName="Text")]
- param ([Parameter(Mandatory=$True, ParameterSetName="Text", Position=0)]
- [Alias("Text")]
- [string]$Script,
- [Parameter(Mandatory=$True)]
- [string[]]$AllowedModule,
- [Parameter()]
- [string[]]$AllowedCommand,
- [Parameter()]
- [string[]]$AllowedVariable)
- $Script = C1 -Script:$Script -AllowedModule:$AllowedModule -AllowedCommand $AllowedCommand -AllowedVariable $AllowedVariable -WarningVariable ParseWarnings -ErrorVariable ParseErrors -ErrorAction SilentlyContinue
- foreach($V5 in $V6 | C11 -Expand Exception | C11 -Expand Errors) {C18 $(if($V5.Extent.StartLineNumber -eq $V5.Extent.EndLineNumber) {"{0} (Line {1}, Char {2}-{2})" -f $V5.Message, $V5.Extent.StartLineNumber, $V5.Extent.StartColumnNumber, $V5.Extent.EndColumnNumber} else {"{0} (l{1},c{2} - l{3},c{4})" -f $V5.Message, $V5.Extent.StartLineNumber, $V5.Extent.StartColumnNumber, $V5.Extent.EndLineNumber, $V5.Extent.EndColumnNumber})}
- if(![String]::IsNullOrWhiteSpace($Script)) {[string[]]$V1 = $AllowedCommand + (C5 -Module:$AllowedModule | C0 {"{0}\{1}" -f $_.ModuleName, $_.Name})
- [string[]]$V7 = $AllowedVariable + (C6 $AllowedModule | C11 -Expand ExportedVariables | C11 -Expand Keys)
- try {[ScriptBlock]::Create($Script).CheckRestrictedLanguage($V1, $V7, $False)
- return $Script} catch [System.Management.Automation.ParseException] {$Global:V8 = $_.Exception.GetBaseException().Errors
- foreach($V5 in $V8) {C18 $(if($V5.Extent.StartLineNumber -eq $V5.Extent.EndLineNumber) {"{0} (Line {1}, Char {2}-{2})" -f $V5.Message, $V5.Extent.StartLineNumber, $V5.Extent.StartColumnNumber, $V5.Extent.EndColumnNumber} else {"{0} (l{1},c{2} - l{3},c{4})" -f $V5.Message, $V5.Extent.StartLineNumber, $V5.Extent.StartColumnNumber, $V5.Extent.EndLineNumber, $V5.Extent.EndColumnNumber})}} catch {$Global:V8 = $_
- C18 $_}}}
- function Expand-Alias {#.Synopsis
- #.Description
- #.Example
- [CmdletBinding(ConfirmImpact="low",DefaultParameterSetName="Files")]
- param ([Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="Files")]
- [Alias("FullName","PSChildName","PSPath")]
- [IO.FileInfo]$File,
- [Parameter(ParameterSetName="Files")]
- [Switch]$InPlace,
- [Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="Text")]
- [Alias("Text")]
- [string]$Script,
- [Parameter(Position=0, Mandatory=$False, ValueFromPipeline=$True, ParameterSetName="History")]
- [Alias("Id")]
- [Int[]]$History,
- [Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="HistoryCount")]
- [Int]$Count,
- [string[]]$AllowedModule=$(@(C6 | C10 -Expand Name) + 'Microsoft.PowerShell.Core'),
- [Parameter()]
- [string[]]$AllowedCommand,
- [Parameter()]
- [string[]]$AllowedVariable,
- [Parameter()]
- [Switch]$Unqualified)
- begin {C16 $PsCmdlet.ParameterSetName}
- process {switch($PsCmdlet.ParameterSetName) {"Files" {if($File -is [System.IO.FileInfo]){$Script = (C3 $File -Delim ([char]0))}}
- "History" {$Script = (C4 -Id $History | C11 -Expand CommandLine) -Join "`n"}
- "HistoryCount" {$Script = (C4 -Count $Count | C11 -Expand CommandLine) -Join "`n"}
- "Text" {}
- default {throw ("ParameterSet: " + $($PsCmdlet.ParameterSetName))}}
- $V9 = $NULL
- $V10 = $NULL
- $V11 = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$V10, [ref]$V9)
- $Global:V10 = $V10
- if($V9) {foreach($V12 in $V9) {$PsCmdlet.WriteError((C8 System.Management.Automation.ErrorRecord (C8 System.Management.Automation.ParseException $V12),
- "Unexpected Exception", "InvalidResult", $_))}
- C18 "There was an error parsing script (See above). We cannot expand aliases until the script parses without errors."
- return}
- $V13 = $V10.Count + 1
- :token for($V14 = $V10.Count -1;$V14 -ge 0;$V14--) {C17 ("Token " + $V14 + " of " + $($V10.Count))
- $V15 = $V10[$V14]
- switch -Regex ($V15.Kind) {"Generic|Identifier" {if($V15.TokenFlags -eq 'CommandName') {if($V13 -ne $V14) {$OFS = ", "
- C17 ("Resolve-Command -Name " + $V15 + ".Text -AllowedModule " + $AllowedModule + " -AllowedCommand @(" + $AllowedCommand + ")")
- $V16 = C9 -Name $V15.Text -AllowedModule $AllowedModule -AllowedCommand $AllowedCommand
- if(!$V16) {return $NULL}}
- C17 ("Unqualified? " + $Unqualified)
- if(!$Unqualified -and $V16.ModuleName) {$V17 = '{0}\{1}' -f $V16.ModuleName, $V16.Name} else {$V17 = $V16.Name}
- $Script = $Script.Remove($V15.Extent.StartOffset, ($V15.Extent.EndOffset - $V15.Extent.StartOffset)).Insert($V15.Extent.StartOffset, $V17)}}
- "Parameter" {C17 ("lastCommand: " + $V13 + " (" + $V14 + ")")
- if($V13 -ge $V14) {for($V18 = $V14;$V18 -ge 0;$V18--) {C17 ("c: " + $($V10[$V18].Text) + " (" + $($V10[$V18].Kind) + " and " + $($V10[$V18].TokenFlags) + ")")
- if(("Generic","Identifier" -contains $V10[$V18].Kind) -and $V10[$V18].TokenFlags -eq "CommandName") {C17 ("Resolve-Command -Name " + $($V10[$V18].Text) + " -AllowedModule " + $AllowedModule + " -AllowedCommand " + $AllowedCommand)
- $V16 = C9 -Name $V10[$V18].Text -AllowedModule $AllowedModule -AllowedCommand $AllowedCommand
- if($V16) {C17 ("Command: " + $($V10[$V18].Text) + " => " + $($V16.Name))}
- $Global:V19 = $V16
- if(!$V16) {return $NULL}
- $V13 = $V18
- break}}}
- $V20 = "^" + $V15.ParameterName
- $V21 = @($V16.ParameterSets | C11 -ExpandProperty Parameters | C15 {$_.Name -match $V20 -or $_.Aliases -match $V20} | C11 -Unique)
- C17 ("Parameters: " + $($V21 | C10 -Expand Name))
- C17 ("Parameters: " + $($V16.ParameterSets | C11 -ExpandProperty Parameters | C10 -Expand Name) + " | ? Name -match " + $V20)
- if($V21.Count -ge 1) {if($V21[0].ParameterType -ne [System.Management.Automation.SwitchParameter]) {if($V10.Count -ge $V14 -and ("Parameter","Semi","NewLine" -contains $V10[($V14+1)].Kind)) {C18 ("No value for parameter: " + $($V21[0].Name) + ", the next token is a " + $($V10[($V14+1)].Kind) + " (Flags: " + $($V10[($V14+1)].TokenFlags) + ")")
- $Script = ""
- break token}}
- $Script = $Script.Remove($V15.Extent.StartOffset, ($V15.Extent.EndOffset - $V15.Extent.StartOffset)).Insert($V15.Extent.StartOffset, ("-" + $($V21[0].Name)))} else {C18 ("Rejecting Non-Parameter: " + $($V15.ParameterName))
- $Script = ""
- break token}
- continue}}}
- if($InPlace) {if([String]::IsNullOrWhiteSpace($Script)) {C18 ("Script is empty after Expand-Alias, File (" + $File + ") not updated")} else {C13 -Path $File -Value $Script}} else {if([String]::IsNullOrWhiteSpace($Script)) {return} else {return $Script}}}}
- C12 Resolve-Alias Expand-Alias
- C2 -Function * -Alias -Alias
Add Comment
Please, Sign In to add comment