Advertisement
Djentacodic

Split-Command

Dec 25th, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.47 KB | Source Code | 0 0
  1. Function Split-Command {
  2. <#
  3. .SYNOPSIS
  4.  
  5. Split a command string into a ShellCommand class object.
  6.  
  7. .DESCRIPTION
  8.  
  9. Splits a command into an object consisting of the file path and an argument array.
  10.  
  11. .PARAMETER Command
  12.  
  13. The command string to split.
  14.  
  15. .INPUTS
  16.  
  17. System.String
  18.  
  19. A string object or string array may be piped into this command.
  20.  
  21. .OUTPUTS
  22.  
  23. ShellCommand
  24.  
  25. Returns a custom class object consisting of the file path and an array of arguments, as well as a method to execute the command.
  26. - FilePath
  27. - Arguments
  28. - Execute([[boolean]<wait>][, [boolean]<runas>])
  29.  
  30. .EXAMPLE
  31.  
  32. Split-Command -Command """${env:ProgramFiles}\Internet Explorer\iexplore.exe"" -no-first-run ""https://www.example.com"""
  33.  
  34. #>
  35.     [CmdletBinding(DefaultParameterSetName = "ByParameter")]
  36.     Param(
  37.         [Parameter(Mandatory = $true, ParameterSetName = "ByParameter", ValueFromPipeline = $true)]
  38.         [ValidateNotNullOrEmpty()]
  39.         [String[]]$Command
  40.     )
  41.     Begin {
  42.         Class ShellCommand {
  43.             [String]$FilePath
  44.             [String[]]$Arguments
  45.  
  46.             ShellCommand() {}
  47.  
  48.             ShellCommand([String]$FilePath) {
  49.                 $this.FilePath      = $FilePath
  50.                 $this.Arguments     = $null
  51.             }
  52.  
  53.             ShellCommand([String]$FilePath, [String[]]$Arguments) {
  54.                 $this.FilePath      = $FilePath
  55.                 $this.Arguments     = $Arguments
  56.             }
  57.  
  58.             [Int32]Execute([boolean]$Wait,[boolean]$RunAs) {
  59.                 $proc = New-Object -TypeName System.Diagnostics.Process
  60.  
  61.                 $proc.StartInfo.FileName = $this.FilePath
  62.                 $proc.StartInfo.Arguments = "$($this.Arguments)"
  63.                 $proc.StartInfo.UseShellExecute = $false
  64.  
  65.                 Switch ($RunAs) {
  66.                     $true   { $proc.StartInfo.Verb = 'runas'; break }
  67.                     Default { $proc.StartInfo.Verb = 'open' }
  68.                 }
  69.  
  70.                 $proc.Start()
  71.  
  72.                 If ($Wait) {
  73.                     Do {} Until ($proc.HasExited)
  74.                 }
  75.  
  76.                 return $proc.ExitCode
  77.             }
  78.            
  79.             [Int32]Execute([boolean]$Wait) {
  80.                 return $this.Execute($Wait,$false)
  81.             }
  82.  
  83.             [Int32]Execute() {
  84.                 return $this.Execute($true,$false)
  85.             }
  86.         }
  87.         [ShellCommand[]]$retval = $null
  88.         [String]$rgxProgPattern = '(?<=.*(?:\.exe|\.cmd|\.bat|rundll32)["]?) +'
  89.         [String]$rgxArgsPattern = ' +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)'
  90.     }
  91.     Process {
  92.         [String[]]$strArray = $Command -split $rgxProgPattern,2
  93.         Write-Debug "strArray[0] = $($strArray[0])`nstrArray[1] = $($strArray[1])"
  94.         [String]$FilePath = $strArray[0]
  95.         [String[]]$ArgArray = ($strArray[1] -split $rgxArgsPattern)
  96.  
  97.         $retval += New-Object -TypeName ShellCommand -ArgumentList @($FilePath,$ArgArray)
  98.     }
  99.     End {
  100.         return $retval
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement