Guest User

Untitled

a guest
Feb 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. #requires -version 3.0
  2. sal C0 %;sal C1 Expand-Alias;sal C2 Export-ModuleMember
  3. sal C3 Get-Content;sal C4 Get-History;sal C5 Microsoft.PowerShell.Core\Get-Command
  4. sal C6 Microsoft.PowerShell.Core\Get-Module;sal C7 Microsoft.PowerShell.Utility\Get-Alias;sal C8 New-Object
  5. sal C9 Resolve-Command;sal C10 Select;sal C11 Select-Object
  6. sal C12 Set-Alias;sal C13 Set-Content;sal C14 Set-StrictMode
  7. sal C15 Where-Object;sal C16 Write-Debug;sal C17 Write-Verbose
  8. sal C18 Write-Warning
  9.  
  10. C14 -Version latest
  11. function Resolve-Command {#.Synopsis
  12. [CmdletBinding()]
  13. param([Parameter(Mandatory=$True)]
  14. [String]$Name,
  15. [String[]]$AllowedModule=$(@(C6 | C10 -Expand Name) + 'Microsoft.PowerShell.Core'),
  16. [Parameter()]
  17. [string[]]$AllowedCommand)
  18. end {$V0 = $Name -replace '(.)$','[$1]'
  19. $V1 = @(C5 $V0 -Module $AllowedModule -ErrorAction SilentlyContinue)
  20. if(!$V1) {if($V2 = $AllowedCommand -match ("^[^-\\]*\\*" + $([Regex]::Escape($Name)))) {$OFS = ", "
  21. C17 ("Commands is empty, but AllowedCommand (" + $AllowedCommand + ") contains " + $Name + ", so:")
  22. $V1 = @(C5 $V2)}}
  23. $V3 = $NULL
  24. if($V1) {C17 ("Commands " + $($V1|C0 {$_.ModuleName + '\' + $_.Name}))
  25. if($V1.Count -gt 1) {$V3 = @($V1 | C15 {$_.Name -match ("^" + $([Regex]::Escape($Name)))})[0]} else {$V3 = $V1[0]}}
  26. if(!$V3 -and !$V0.Contains("-")) {$V1 = @(C5 ("Get-" + $V0) -ErrorAction SilentlyContinue -Module $AllowedModule | C15 {$_.Name -match ("^Get-" + $([Regex]::Escape($Name)))})
  27. if($V1) {if($V1.Count -gt 1) {$V3 = @($V1 | C15 {$_.Name -match ("^" + $([Regex]::Escape($Name)))})[0]} else {$V3 = $V1[0]}}}
  28. 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}}}
  29. 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")}
  30. C17 ("The current AllowedModules are: " + $($AllowedModule -join ', '))}
  31. return $V3}}
  32. function Protect-Script {#.Synopsis
  33. [CmdletBinding(ConfirmImpact="low",DefaultParameterSetName="Text")]
  34. param ([Parameter(Mandatory=$True, ParameterSetName="Text", Position=0)]
  35. [Alias("Text")]
  36. [string]$Script,
  37. [Parameter(Mandatory=$True)]
  38. [string[]]$AllowedModule,
  39. [Parameter()]
  40. [string[]]$AllowedCommand,
  41. [Parameter()]
  42. [string[]]$AllowedVariable)
  43. $Script = C1 -Script:$Script -AllowedModule:$AllowedModule -AllowedCommand $AllowedCommand -AllowedVariable $AllowedVariable -WarningVariable ParseWarnings -ErrorVariable ParseErrors -ErrorAction SilentlyContinue
  44. 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})}
  45. if(![String]::IsNullOrWhiteSpace($Script)) {[string[]]$V1 = $AllowedCommand + (C5 -Module:$AllowedModule | C0 {"{0}\{1}" -f $_.ModuleName, $_.Name})
  46. [string[]]$V7 = $AllowedVariable + (C6 $AllowedModule | C11 -Expand ExportedVariables | C11 -Expand Keys)
  47. try {[ScriptBlock]::Create($Script).CheckRestrictedLanguage($V1, $V7, $False)
  48. return $Script} catch [System.Management.Automation.ParseException] {$Global:V8 = $_.Exception.GetBaseException().Errors
  49. 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 = $_
  50. C18 $_}}}
  51. function Expand-Alias {#.Synopsis
  52. #.Description
  53. #.Example
  54. [CmdletBinding(ConfirmImpact="low",DefaultParameterSetName="Files")]
  55. param ([Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="Files")]
  56. [Alias("FullName","PSChildName","PSPath")]
  57. [IO.FileInfo]$File,
  58. [Parameter(ParameterSetName="Files")]
  59. [Switch]$InPlace,
  60. [Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="Text")]
  61. [Alias("Text")]
  62. [string]$Script,
  63. [Parameter(Position=0, Mandatory=$False, ValueFromPipeline=$True, ParameterSetName="History")]
  64. [Alias("Id")]
  65. [Int[]]$History,
  66. [Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="HistoryCount")]
  67. [Int]$Count,
  68. [string[]]$AllowedModule=$(@(C6 | C10 -Expand Name) + 'Microsoft.PowerShell.Core'),
  69. [Parameter()]
  70. [string[]]$AllowedCommand,
  71. [Parameter()]
  72. [string[]]$AllowedVariable,
  73. [Parameter()]
  74. [Switch]$Unqualified)
  75. begin {C16 $PsCmdlet.ParameterSetName}
  76. process {switch($PsCmdlet.ParameterSetName) {"Files" {if($File -is [System.IO.FileInfo]){$Script = (C3 $File -Delim ([char]0))}}
  77. "History" {$Script = (C4 -Id $History | C11 -Expand CommandLine) -Join "`n"}
  78. "HistoryCount" {$Script = (C4 -Count $Count | C11 -Expand CommandLine) -Join "`n"}
  79. "Text" {}
  80. default {throw ("ParameterSet: " + $($PsCmdlet.ParameterSetName))}}
  81. $V9 = $NULL
  82. $V10 = $NULL
  83. $V11 = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$V10, [ref]$V9)
  84. $Global:V10 = $V10
  85. if($V9) {foreach($V12 in $V9) {$PsCmdlet.WriteError((C8 System.Management.Automation.ErrorRecord (C8 System.Management.Automation.ParseException $V12),
  86. "Unexpected Exception", "InvalidResult", $_))}
  87. C18 "There was an error parsing script (See above). We cannot expand aliases until the script parses without errors."
  88. return}
  89. $V13 = $V10.Count + 1
  90. :token for($V14 = $V10.Count -1;$V14 -ge 0;$V14--) {C17 ("Token " + $V14 + " of " + $($V10.Count))
  91. $V15 = $V10[$V14]
  92. switch -Regex ($V15.Kind) {"Generic|Identifier" {if($V15.TokenFlags -eq 'CommandName') {if($V13 -ne $V14) {$OFS = ", "
  93. C17 ("Resolve-Command -Name " + $V15 + ".Text -AllowedModule " + $AllowedModule + " -AllowedCommand @(" + $AllowedCommand + ")")
  94. $V16 = C9 -Name $V15.Text -AllowedModule $AllowedModule -AllowedCommand $AllowedCommand
  95. if(!$V16) {return $NULL}}
  96. C17 ("Unqualified? " + $Unqualified)
  97. if(!$Unqualified -and $V16.ModuleName) {$V17 = '{0}\{1}' -f $V16.ModuleName, $V16.Name} else {$V17 = $V16.Name}
  98. $Script = $Script.Remove($V15.Extent.StartOffset, ($V15.Extent.EndOffset - $V15.Extent.StartOffset)).Insert($V15.Extent.StartOffset, $V17)}}
  99. "Parameter" {C17 ("lastCommand: " + $V13 + " (" + $V14 + ")")
  100. if($V13 -ge $V14) {for($V18 = $V14;$V18 -ge 0;$V18--) {C17 ("c: " + $($V10[$V18].Text) + " (" + $($V10[$V18].Kind) + " and " + $($V10[$V18].TokenFlags) + ")")
  101. if(("Generic","Identifier" -contains $V10[$V18].Kind) -and $V10[$V18].TokenFlags -eq "CommandName") {C17 ("Resolve-Command -Name " + $($V10[$V18].Text) + " -AllowedModule " + $AllowedModule + " -AllowedCommand " + $AllowedCommand)
  102. $V16 = C9 -Name $V10[$V18].Text -AllowedModule $AllowedModule -AllowedCommand $AllowedCommand
  103. if($V16) {C17 ("Command: " + $($V10[$V18].Text) + " => " + $($V16.Name))}
  104. $Global:V19 = $V16
  105. if(!$V16) {return $NULL}
  106. $V13 = $V18
  107. break}}}
  108. $V20 = "^" + $V15.ParameterName
  109. $V21 = @($V16.ParameterSets | C11 -ExpandProperty Parameters | C15 {$_.Name -match $V20 -or $_.Aliases -match $V20} | C11 -Unique)
  110. C17 ("Parameters: " + $($V21 | C10 -Expand Name))
  111. C17 ("Parameters: " + $($V16.ParameterSets | C11 -ExpandProperty Parameters | C10 -Expand Name) + " | ? Name -match " + $V20)
  112. 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) + ")")
  113. $Script = ""
  114. break token}}
  115. $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))
  116. $Script = ""
  117. break token}
  118. continue}}}
  119. 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}}}}
  120. C12 Resolve-Alias Expand-Alias
  121. C2 -Function * -Alias -Alias
Add Comment
Please, Sign In to add comment