Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Wednesday, May 2, 2012

My Powershell Profile

My Powershell Profile


Let me preface this by saying that I'm going to discuss my OLD Powershell profile. I used to use this as the default profile, but it took forever to open a Powershell instance due to all of the modules and snap-ins that had to load. In the end, what I did was have a blank profile, then created a shortcut on my taskbar to run 'powershell.exe -NoExit -File c:\powerprofile.ps1". I'm going to go through my setup one section at a time.

Variable and Aliases Section

$a = (Get-Host).UI.RawUI
$a.BackgroundColor = “Black”
$a.ForegroundColor = “Green”
new-alias gh get-help
$desktop = "C:\Users\user1\Desktop"
$documents = "O:\My Documents"
$credential = get-credential user1@foo.org
$credDomainAdmin = get-credential domainadmin@foo.org


The first bit dictates that my Powershell window is green text on a black background. It makes everything so easy to see!! I specify that 'gh' is an alias for the get-help command, since I use it a lot. I also have a couple of variables I can use when I want to refer to my Desktop or My Documents folder paths. Then, I have Powershell prompt me for my credentials, as well as the Domain Administrator credentials. We'll use those later to set up connections to various servers.

Exchange 2010 Management Shell

$sessionEXCH = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailserver.foo.org/PowerShell/ -Authentication Kerberos -Credential $credDomainAdmin
import-pssession $sessionEXCH | out-null
echo "Exchange 2010 connection loaded"



Note that the Exchange 2010 Management Console is installed locally. This makes the Exchange Powershell commands available and connects me to the mail server. Note that I piped the import-pssession command to out-null. Doing this suppresses any output to the screen. 

Lync 2010 Management Shell

$sessionLYNC = New-PSSession -ConnectionUri "https://LyncServer.foo.com/OcsPowershell" -Credential $credDomainAdmin
import-pssession $sessionLYNC | out-null



Again, the Lync 2010 Management Console is installed locally. This section makes the Lync 2010 commands available and connects me to the Lync server.

 VMware PowerCLI Connection

add-pssnapin Vmware.VimAutomation.Core
$vmware = "Vmware.VimAutomation.Core"
connect-viserver -server vCenterServer.foo.org -credential $credential


This loads the VMware PowerCLI (which, again, is installed locally) and connects me to the VMware Virtual Center Server.

Misc Modules and Snap-Ins:

I will go through the rest of these using Powershell-style comments (anything after a # symbol is a comment).

add-pssnapin SqlServerCmdletSnapin100
add-pssnapin SqlServerProviderSnapin100
#SQL Server Management

import-module activedirectory
#The Microsoft Active Directory module

import-module pscx
add-pssnapin Quest.ActiveRoles.ADManagement
import-module WASP
import-module bsonposh
#These modules and snap-ins were discussed in my post yesterday.

import-module psterminalservices
#This is a handy module for managing Microsoft Windows Terminal Servers.


After all of this, I perform the command to change my working directory to our IT Department's script folder, and then clear the screen:

cd Z:\PS
cls



Load time for this configuration is about 20 seconds or so. If I just need to run some commands, it's far faster to open Powershell with a blank profile. I could have gone about this the opposite way. The script above could serve as my default profile, and I could create a shortcut to 'powershell.exe -noprofile'. In weighing my options between the two methods, I decided to use a blank default always so that I could type Powershell at the run command and just get going on my work immediately.

Tomorrow: What kind of helpdesk tasks I've automated.

No comments:

Post a Comment