ISE profile re-visited. :)

First of all – I finally had some time so I managed to finish reading PowerShell Cookbook v.2. It was awesome, educational and fun! Thank you Lee Holmes for sharing your knowledge with us! And it was very well spent $10, so I encourage anybody who looks for a book that has both learning material and some real life use cases to read this book. If I will be able to, I will try to write a review. 😉

With that book there were so many new things I learned and refreshed that I almost felt few switches moving in my brain during reading it. Let’s take my ISE profile. If someone read my post and is knowledgeable enough he was probably looking in disbelieve: why in the world did he use Invoke-Expression there!? Doesn’t he know about [ScriptBlock]::Create() static method?

Honestly I did not, now I know so I started with re-writing this part. Old code:

 

PowerShell, using GeSHi 1.0.8.8
  1. Invoke-Expression "`$SwitchToMe = { `$tabs.SetSelectedPowerShellTab(`$tabs[$NewTabIndex-1]) }"

YAK!

New code, that uses [scritpblock] method:

PowerShell, using GeSHi 1.0.8.8
  1. $SwitchToMe = [ScriptBlock]::Create("`$tabs.SetSelectedPowerShellTab(`$tabs[$($NewTabIndex-1)])")

It maybe not so different at first, but you know, if I create variable I prefer to see that I actually do it in the first place. It also clear what type of variable I’m creating. When I will read this code somewhere in future I may know how to modify it. 😉 Not so with that Invoke-Expression stuff.

Second thing I’ve changed was setting focus on command pane rather than script pane in my ‘NoProfile’ tab. And last but not least: I’ve added ssh function that basically changes tab name to match host name and Enter-PsSession. I was thinking of an easy way to get my tabs name back and eventually decided to use Prompt for that. Works nice – I have my remote name during remote session, and once I disconnect – I get name back. Both ssh function and prompt are of course part of my re-visited profile. Whole profile can be seen here:

PowerShell, using GeSHi 1.0.8.8
  1. $tabs = $psISE.PowerShellTabs
  2. $curtab = $tabs[$tabs.Count1]
  3. $ISEOpt = $psISE.Options
  4.  
  5. $ISEOpt.OutputPaneBackgroundColor = ‘black’
  6. $ISEOpt.OutputPaneTextBackgroundColor = ‘black’
  7. $ISEOpt.OutputPaneForegroundColor = ‘white’
  8. $TabsMenu = $curtab.AddOnsMenu.Submenus.Add(‘Tabs’,$null,$null)
  9. if ($curtab -eq $tabs[0]) {
  10.     $curtab.DisplayName = ‘NoProfile’
  11.     $newTab = $tabs.Add()
  12.     } else {
  13.     function prompt {
  14.         $psISE.CurrentPowerShellTab.DisplayName = "PoSh # $($tabs.IndexOf($curtab))"
  15.         $(if (test-path variable:/PSDebugContext) { ‘[DBG]: ‘ } else { }) + ‘PS ‘ + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { ‘>>’ }) + ‘> ‘
  16.     }
  17.     $curtab.DisplayName = "PoSh # $($tabs.IndexOf($curtab))"
  18.     $NewTabIndex = $tabs.Count
  19.     $SwitchToMe = [ScriptBlock]::Create("`$tabs.SetSelectedPowerShellTab(`$tabs[$($NewTabIndex-1)])")
  20.     for ($i = 0; $i -lt $tabs.Count 1; $i++) {
  21.        
  22.         if ($NewTabIndex -le 9) {
  23.             $SwitchTo = [ScriptBlock]::Create("`$tabs.SetSelectedPowerShellTab(`$tabs[$i])")
  24.             $MyMenu = $tabs[$i].AddOnsMenu.Submenus | Where { $_.DisplayName -eq ‘Tabs’ }
  25.             try {
  26.                 [void] $Mymenu.Submenus.Add($curtab.DisplayName, $SwitchToMe ,"ALT + $NewTabIndex")
  27.             } catch {
  28.                 Write-Debug ‘This menu item is already present!’
  29.             }
  30.         } else {
  31.             # That should not happen cause limit is 8
  32.             Write-Host -ForegroundColor Red "Can not add this tab to menu – move to it using CTRL + TAB"
  33.         }
  34.         [void] $TabsMenu.Submenus.Add($tabs[$i].DisplayName, $Switchto, "ALT + $($i+1)")
  35.     }
  36.     $tabs.SetSelectedPowerShellTab($tabs[0])
  37.     $tabs[0].CommandPane.Focus()
  38.     Add-PsSnapin Quest*
  39.     ImportModule @(‘WPK’,‘IsePack’,‘Inventory’,‘ISEFun’,‘WAD’)
  40.     . "C:\Program Files\Quest Software\Management Shell for AD\qsft.ps1"
  41.     $Inventory = ImportInventoryList
  42.     $HashPC = ImportInventoryHash
  43.     $hashPrinter = Import-Csv A:\Inventory\printers.csv | ForEach-Object -Begin { $__out = @{} } -Process { $__out.Add($_.Letter,$_) } -End { $__out; Remove-Variable __out }
  44.    
  45. }
  46.  
  47. function ssh {
  48.     param ([string]$remotehost =‘pl-war-ap001’)
  49.     $oldTab = $curtab.DisplayName
  50.     $curtab.DisplayName = $remotehost
  51.     try {
  52.         EnterPSSession $remotehost
  53.     } catch {
  54.         $curtab.DisplayName = $oldTab
  55.         Write-Host -ForegroundColor Red "There was a problem: $_"
  56.     }
  57. }

Last thing I do not like about Add-Ons menu: you always get your code when you invoke something from it. I would love to have some way to get rid of it. It’s not that I want to make it secret, I just want to make output-pane unaware of this code. But this is minor, and if James Brundage could live with that (ISEPack is so much bigger than what I did…), so can I. 😉

Advertisements

One thought on “ISE profile re-visited. :)

  1. Pingback: Profile – część druga. « PowerShell po polsku

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s