Wednesday, November 28, 2012

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

---------- Go to Part 1, Part 2 ----------


  • Function to Export Unsealed Management Pack from a specified management group in a specified folder

  • This function will connect the management group and add OpsMgr snapin if you're not connected, create the specified folder if it doesn't exist and ask to re-use the folder if exist. You can execute the ExportMP function with no path, a new folder will be created in C:\Temp. Don't forget to change text in red

    1. Function ExportMP ([string]$RMS, [string]$PATH)
    2. {
    3. switch ($RMS.toupper()) {
    4.  "PROD" {$RMS1="PRODRMS.Mydomain.com"}
    5.  "DEV" {$RMS1="DEVRMS.Mydomain.com"}
    6.  "PREPROD" {$RMS1="PREPRODRMS.Mydomain.com"}
    7.  default {Write-Host "RMS  - $RMS - not valid" -ForegroundColor red ;sleep 3;exit}
    8.  }
    9. if ( (Get-pssnapin | where {$_.Name -eq "Microsoft.EnterpriseManagement.OperationsManager.Client" }).Name -ne "Microsoft.EnterpriseManagement.OperationsManager.Client" )
    10.   {
    11.   add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;
    12.   $creds = Get-Credential("MyDomain\MyAccount");
    13.   $connection = New-ManagementGroupConnection -ConnectionString: $RMS1 -Credential: $creds
    14.   Set-Location "OperationsManagerMonitoring::" | Out-Null;}
    15. If ($PATH -eq "")
    16.  {
    17.  $a = Get-Date -format d
    18.  $a=$a.Replace('/', '-')
    19.  $RMS = $RMS.toupper()
    20.  $PATH = "C:\Temp\UnsealedMPs-"+$RMS+"-"+$a
    21.  $TestPath = Test-Path $PATH
    22.  if ( $TestPath -eq $False) {
    23.   "Create a new backup folder: " + $PATH
    24.   New-Item -ItemType directory -Path $PATH
    25.   $Continue= "Yes"
    26.   }
    27.  else {
    28.  Write-Host "Path  - $PATH - already exist.
    29.   
    30.  " -ForegroundColor green
    31. $Question = Read-Host "Do you want to re-use it [Y/N] ?"
    32.  Switch ($Question.toupper()){
    33.   "Y" {$Continue= "Yes"}
    34.   "N" {$Continue= "No"}
    35.   default {$Continue= "No"}
    36.   }
    37.  }
    38.  }
    39. Else { Write-Host "Folder doesn't exist - create the folder: $PATH" -ForegroundColor green
    40.   New-Item -ItemType directory -Path $PATH
    41.   $Continue= "Yes"}
    42. if ($Continue -eq "Yes") { 
    43.  $mps = get-managementpack | where-object {$_.Sealed -eq $false}
    44.  foreach ($mp in $mps)
    45.   {
    46.   export-managementpack -managementpack $mp -path $PATH
    47.   }
    48.  }
    49. Else { Write-Host "No export Done" -BackgroundColor red -ForegroundColor yellow}
    50. }

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

    No comments:

    Post a Comment