Monday, June 4, 2012

[Powershell] Creating your own Windows Powershell Profile - Part 2

---------- Go to Part 1 ----------

What goes in my profile ?

This part will be SCOM oriented by you must keep in mind that all cmdlets, scripts, functions and any command in powershell could be added to your profile.
  • Function to connect OpsMgr Environment
This function will help you to connect a management group by running a simple powershell command at prompt : Connect RMSName1

This command will open a windows that ask you to enter the password for your crédentials


Here is the few powershell line to copy in your profile file : (Replace the red words by your RMS name + your credential). Note that you can set the password in parameters of the Get-Credentials( ) but this is not really secure.
  1. function Connect  ([string]$RMS)
  2. {
  3. switch ($RMS.toupper()) {
  4.              "PROD" {$RMS="RMSName1"}
  5.              "DEV" {$RMS="RMSName2"}
  6.              "PP" {$RMS="RMSName3"}
  7.              "PREPROD" {$RMS="RMSName3"}
  8.               default {"RMS  - $RMS - not valid"; exit }
  9.              }
  10. # Load Operation Manager environment
  11. if ( (Get-pssnapin | where {$_.Name -eq "Microsoft.EnterpriseManagement.OperationsManager.Client" }).Name -ne "Microsoft.EnterpriseManagement.OperationsManager.Client" ) {
  12.               add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin; }
  13. $creds = Get-Credential("MyDomain\MyAccount");
  14. $connection = New-ManagementGroupConnection -ConnectionString: $RMS -Credential: $creds #-ErrorAction:SilentlyContinue;
  15. # Move off of a drive to avoid deadlock.
  16. Set-Location "OperationsManagerMonitoring::" | Out-Null;
  17. }
  • Function to list process information
This function will run a WMI query on processes to get all information. Running p-list alone will list all the processes, p-list with a process name in argument will only give you information about the process in argument.



Here is the code to copy in your profile file :

  1. function p-list([string]$name="*")
  2. {
  3. $WMIQuery = get-wmiobject win32_service | sort ProcessId | group-object ProcessId
  4. $Processes = @(get-process $name | sort Id)
  5. $i=0
  6. $j=0
  7. while($i -lt $Processes.count -and $j -lt $WMIQuery.count)
  8.  {
  9.       if($Processes[$i].Id -lt $WMIQuery[$j].Name)
  10.        {
  11.        $i++;
  12.        continue;
  13.        }
  14.        if($Processes[$i].id -gt $WMIQuery[$j].Name)
  15.        {
  16.        $j++;
  17.        continue;
  18.        }
  19.        if($Processes[$i].id -eq $WMIQuery[$j].Name)
  20.        {
  21.        $Processes[$i]| add-member NoteProperty service $WMIQuery[$j].group;
  22.        $i++;
  23.        $j++;
  24.        }
  25.  }
  26. $Processes;
  27. }
  • Function to list active SDK connection information
This function will list all actives SDK connections as already explained here :
 http://tetris38.blogspot.fr/2012/03/opsmgr-2007-powershell-script-to-list.html
 
  1. function List-SDK()
  2. {
  3. # Get the connected users
  4. $SDKUsers = Get-ManagementGroupConnection | foreach-object {$_.ManagementGroup.getConnectedUserNames()} | sort
  5. $SDKNumberConnections = $SDKUsers | measure-object
  6. # number of active connections
  7. $SDKNumberConnections.count
  8. # count active connections per users
  9. $hash =@{}
  10. $SDKUsers | % {$hash[$_] = $hash[$_] + 1 }
  11. $Result = $hash.getenumerator() | ? { $_.value -gt 0 }
  12. $Result | sort value -DESC
  13. }
Result is like :


  • Shell Windows tuning
You can define some parameters of your powershell Shell Windows like the title, the background color and the foreground color by copying the line below in your profile :

  1. # Change Title - to "Windows PowerShell - your account"
  2. $Host.UI.RawUI.WindowTitle = "Windows PowerShell - " + $env:Username
  3. # Change background color
  4. $Host.UI.RawUI.BackgroundColor = "DarkBlue"
  5. # Change Text color
  6. $Host.UI.RawUI.ForegroundColor = "White"


You can download all the example I've provided in the file below

This posting is provided "AS IS" with no warranties.

No comments:

Post a Comment