Blog Archives
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
Enjoy!
Update: Creating a profile for a remote session
So not too long ago I found a way to create a remote profile but there was one problem with it, it was a little sloppy in that it wouldn’t close the sessions. I haven’t found a way for it to close the sessions yet, and I’m not sure I want to. What I have done is get it to manage it’s sessions a little better. Here is the updated script.
function connect ($hname){
#clean out non-open connections
gsn | Where-Object {$_.state -ne "Opened"} | rsn
#Check for Open Sessionsif($session = gsn | Where-Object {$_.computername -eq $hname})
{
Write-Host "Reusing Connection!"
}
else
{
Write-Host "Creating New Connection!"
$session = new-pssession $hname
icm -session $session -scriptblock{
####START REMOTE PROFILE####function prompt
{
Write-Host $(Get-Date -Format [HH:mm:ss]) -NoNewline -ForegroundColor Blue
write-host $(get-location) -nonewline -foregroundcolor white
return ">"
}
####END REMOTE PROFILE####
}
}
enter-pssession $session
}
As you can see there is a section marked Remote profile. I have only created a remote prompt but you can get it to do other things, for example load Quest modules or whatever it is you have in your normal profile.
Creating a profile for a remote session
This has been updated, check out the newer post!
I’ve been trying to find a way to create a profile for when I remote in to servers with winrm, and even though its documented, it doesn’t seem to be possible.
"– When you use Enter-PSSession, your user profile on the remote computer is used for the interactive session. The commands in the remote user profile, including commands to add Windows PowerShell snap-ins and to change the command prompt, run before the remote prompt is displayed."
So, apparently they forgot to add a few bits of code… I’ve posted a bug report on this (you should go vote on this) but for the time being, I’ve come up with a fairly simple way around this.
In your profile on your local machine we’re going to create a function called connect which will do this for us. In this example I’ll only modify the prompt, but obviously you can add more in.
function connect ($hname){
$session = new-pssession $hname
icm -session $session -scriptblock{
#remote profile scriptfunction prompt
{
Write-Host $(Get-Date -Format [HH:mm:ss]) -NoNewline -ForegroundColor Blue
write-host $(get-location) -nonewline -foregroundcolor green
return ">"
}}
enter-pssession $session
}
This creates the session via New-PSSession and then uses the invoke command to push your profile (that script block) to it and then enters the session for interactive use.
There is one problem with this and that is when you do Enter-PSSession from the prompt, and then Exit-PSSession from the remote session, it closes out the session, when you do it this way, the Exit-PSSession keeps the session open.
To visualize this try this code bit
gsn
etsn $server
Exit
gsn
nsn $server | etsn
exit
gsn
You’ll notice the first gsn (Get-PSSession) returns nothing because the session was closed, the second gsn shows that the session is still open..
I’m going to work on a better Connect function, but for the time being, this will work, just don’t forget the connections are staying open!
