Blog Archives

PowerShell: Working with Modules in ISE


Now that ISE isn’t crap with V3 I find that I dont really use any other editors. The one problem I have is when I work with modules, so I did a little screwing around and came up with this.


Function Import-CurrentFileAsModule

{

[cmdletbinding()]

param()

#get paths

$filePath = $psise.CurrentFile.FullPath

$folder = split-path $filePath

#save if not already saved

if($psise.CurrentFile.IsUntitled){Write-Error "Must save file first! Sorry didn't feel like implementing the dialog box!" -ErrorAction Stop}

if(-not $psise.CurrentFile.IsSaved){$psise.CurrentFile.Save()}

$global:WorkingModule = $null

#import the folder or the file if its standalone

try{
$psise.CurrentPowerShellTab.files | ?{-not $_.issaved} |? {$_.fullpath -like "$folder*"} | %{$_.save()}
$Global:WorkingModule = Import-Module $folder -Force -ErrorAction Stop -PassThru -Verbose:$false | select -ExpandProperty name
}

catch{$folderFailed = $true}

if($folderFailed)

{

try {Import-Module $filePath -Force -ErrorAction Stop -Verbose:$false}

catch{ write-error "Not a module file!" -ErrorAction Stop}

}

##post processing

if(Test-Path function:\PostModuleProcess)

{

Write-Verbose "Processing PostModuleProcess Function"

PostModuleProcess

}

else

{

Write-Verbose "--Create a PostModuleProcess function to excute code after import--"

}

Write-Verbose "Remove -verbose tag from last cmd in this file to stop verbose messaging"

}

 

Function Get-ModuleVariable{

param($Name)

if($global:workingmodule)

{

if($name)

{

&(gmo $global:workingmodule){Get-Variable -Name $args[0] -Scope script -ValueOnly} $name

}

else

{

&(gmo $global:workingmodule){Get-Variable -Scope script -ValueOnly}

}

}

}

Function Set-ModuleVariable{

[cmdletbinding()]

param(

[parameter(manditory=$true)]

$Name,

[parameter(manditory=$true)]

$Value)

if($global:workingmodule)

{

&(gmo $global:workingmodule){Set-Variable -Name $args[0] -Value $args[1] -Scope script} $Name $Value

}

}

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Reload Module',{Import-CurrentFileAsModule -verbose},"F6")

,{Import-CurrentFileAsModule -verbose},"F6")

Just dump this in your profile or in a file and load it in your profile and F6 is ready to be used!

This create a couple of functions to allow you to look in to and edit module (script:  scope) variables, Get/Set-ModuleVariable and it will run PostModuleProcess function (if you create it) so that if you want to run any code after it loads you can easily do that.

You’ll notice in that last line that the Import-CurrentFileAsModule has the verbose tag on it, once you’re comfortable using it you can remove that.

Let me know what you think!

P.s. Thanks for the tips Jaykul!

opps! completely forgot that I should save all files in the module!

PowerShell: Microsoft Releases Script Explorer! Here is the ISE Fix


Microsoft has a pre-release of the Script Explorer which allows you to search poshcode, technet gallery as well as local repositories. Its kinda neat.

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=29101

It ties in to ISE but not so nicely, however, with most things PowerShell, you can fix that.

This will work for a 64 bit system, I think you can easily see the change if you have a 32 bit system.


$filepath=  'c:\Program Files (x86)\Microsoft Script Explorer for Windows PowerShell\Microsoft.ScriptExplorer.ps1'

$content = gc $filepath

$content[4] += " | out-null"
$content | Out-File $filepath

No more spew on the screen!

PowerShell ISE addon for “Running” tabs


If you are like me then you have multiple PowerShell tabs open when you are working, partly to keep organized and partly to run longer tasks. I often scan all of my servers for something which can some times be a lengthy process. Rather than going back and forth between tabs to see if the job has completed I wrote this little script that you can toss in your profile that will change the tab name.

Register-ObjectEvent $psise.CurrentPowerShellTab PropertyChanged -Action {
If($Event.SourceArgs[1].PropertyName -eq "StatusText")
{ $tab = $event.Sender $name = $tab.displayname
if($Event.SourceArgs[0].StatusText -like "Running*")
 { $tab.displayname = "* $name" }
elseif($Event.SourceArgs[0].StatusText -eq "Completed" -or $Event.SourceArgs[0].StatusText -eq "Stopped")
{ $Tab.DisplayName = $name -replace "\* " }
 } }

Its up on Technet as well

http://gallery.technet.microsoft.com/PowerShell-ISE-Running-876cc74c

Enjoy!