Advertisement
Yevrag35

Lazy Show Profile

Jun 13th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Show-Profile()
  2. {
  3.     [CmdletBinding(PositionalBinding=$false)]
  4.     [alias("shpf")]
  5.     param
  6.     (
  7.         # [parameter(Mandatory=$false,Position=0)]
  8.         # [ValidateSet('Notepad','Notepad++','VSCode','Console')]
  9.         # [string] $OpenIn = "Notepad"
  10.     )
  11.     DynamicParam
  12.     {
  13.         if ($null -eq $global:TextEditors)
  14.         {
  15.             $global:TextEditors=@()
  16.             # Notepad ++ -- Priority #1
  17.             $32npp = "$p86\Notepad++\notepad++.exe"
  18.             $64npp = "$p64\Notepad++\notepad++.exe"
  19.             if (Test-Path $32npp)
  20.             {
  21.                 $test = $32npp
  22.             }
  23.             elseif (Test-Path $64npp)
  24.             {
  25.                 $test = $64npp
  26.             }
  27.             if ($null -ne $test)
  28.             {
  29.                 $global:TextEditors += New-Object PSObject -Property @{
  30.                     Name = "Notepad++"
  31.                     Path = $test
  32.                     Priority = 1
  33.                 }
  34.             }
  35.             # Add Visual Studio Code...
  36.             $vsCode = "$p64\Microsoft VS Code\Code.exe"
  37.             if (Test-Path $vsCode)
  38.             {
  39.                 $global:TextEditors += New-Object PSObject -Property @{
  40.                     Name = "VSCode"
  41.                     Path = $vsCode
  42.                     Priority = 2
  43.                 }
  44.             }
  45.             # Add Stupid Notepad...
  46.             $global:TextEditors += New-Object psobject -Property @{
  47.                 Name = "Notepad"
  48.                 Path = "$env:WINDIR\System32\Notepad.exe"
  49.                 Priority = 3
  50.             }
  51.             # Add Stupid Console...
  52.             $global:TextEditors += New-Object psobject -Property @{
  53.                 Name = "Console"
  54.                 Path = "Get-Content" # little trickery...
  55.                 Priority = 4
  56.             }
  57.         }
  58.         $attName = "OpenIn"
  59.         $editors = $global:TextEditors.Name
  60.         $rtDict = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
  61.         $attCol = New-Object 'System.Collections.ObjectModel.Collection[System.Attribute]'
  62.         $pAtt = New-Object System.Management.Automation.ParameterAttribute -Property @{
  63.             Mandatory = $false
  64.             Position = 0
  65.         }
  66.         $attCol.Add($pAtt)
  67.         $alias = New-Object System.Management.Automation.AliasAttribute("o", "oi")
  68.         $attCol.Add($alias)
  69.         $valSet = New-Object System.Management.Automation.ValidateSetAttribute($editors)
  70.         $attCol.Add($valSet)
  71.         $rtParam = New-Object System.Management.Automation.RuntimeDefinedParameter($attName, [string], $attCol)
  72.         $rtDict.Add($attName, $rtParam)
  73.         return $rtDict;
  74.     }
  75.     Begin
  76.     {
  77.         $chosen = $PSBoundParameters["OpenIn"]
  78.         if ($null -eq $chosen)
  79.         {
  80.             # ...then go by priority
  81.             $OpenIn = ($global:TextEditors | Sort Priority)[0]
  82.         }
  83.         else
  84.         {
  85.             $OpenIn = $global:TextEditors | ? Name -eq $chosen
  86.         }
  87.     }
  88.     Process
  89.     {
  90.         if ($OpenIn.Path -like "*.exe")
  91.         {
  92.             Start-Process $OpenIn.Path -ArgumentList $profile
  93.         }
  94.         else
  95.         {
  96.             Invoke-Expression -Command "$($OpenIn.Path) $profile"
  97.         }
  98.     }
  99.     End
  100.     {
  101.         return
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement