# The sample scripts are not supported under any Microsoft standard support # program or service. The sample scripts are provided AS IS without warranty # of any kind. Microsoft further disclaims all implied warranties including, # without limitation, any implied warranties of merchantability or of fitness # for a particular purpose. The entire risk arising out of the use or # performance of the sample scripts and documentation remains with you. In no # event shall Microsoft, its authors, or anyone else involved in the creation, # production, or delivery of the scripts be liable for any damages whatsoever # (including, without limitation, damages for loss of business profits, # business interruption, loss of business information, or other pecuniary # loss) arising out of the use of or inability to use the sample scripts or # documentation, even if Microsoft has been advised of the possibility of # such damages. # # # PowerShell script to automagically import all of the # relevant JEE MPs. This is done by first importing the MPB and # then importing all of the MP (minus the Library of course) # # Authoring: Christopher Crammond # Date Created: March 31st, 2011 # Date Modified: August 18th, 2011 # # Version: 0.0.6 # # This should be the full path the the MP(B)s, relative paths will not work!!! $localpathToMpsForImport = "C:\drop\Target\x64\Debug\ManagementPacks\ENU" # Needed for SCOM SDK $ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties() $rootMS = "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName ################################################################################# # # Method to import all of the MPs found in the global variable 'localpathToMpsForImport' # function ImportMps { $Dir = get-childitem $localpathToMpsForImport # # Import the MPB first # $mpbList = $Dir | where {$_.extension -eq ".mpb"} $mpbList | format-table name Echo "" Echo " - Importing JEE Library Bundle" Import-Module 'C:\Program Files\System Center Operations Manager 2012\Powershell\OperationsManager\OperationsManager.psd1' foreach ($mpb in $mpbList) { Echo " * Importing $mpb" Import-SCOMManagementPack -Fullname "$localpathToMpsForImport\$mpb" } $mpList = $Dir | where {$_.extension -eq ".mp" -and $_.name -ne "Microsoft.JEE.Library"} $mpList | format-table name # # Next import all of application server libraries # Echo "" Echo " - Importing Application Server Libraries" foreach ($librarymp in $mpList) { if($librarymp.name.Contains("Microsoft.JEE.Library.mp") -or $librarymp.name.Contains("Microsoft.JEE.Templates.Library.mp")) { Echo " * Skipping import of Microsoft.JEE.Library.mp" } elseif($librarymp.name.Contains(".Library.mp")) { Echo " * Importing $librarymp" Install-ManagementPack -filepath "$localpathToMpsForImport\$librarymp" } } # # Step #3: Import all Leaf MPs # Echo "" Echo " - Importing Application Server Leaf MPs" foreach ($mp in $mpList) { if($mp.name.StartsWith("Microsoft.JEE") -and $mp.name.EndsWith("Library.mp")) { #Echo "Skipping all library MPs" } elseif($mp.name.StartsWith("Microsoft.JEE")) { Echo " * Importing $mp" Install-ManagementPack -filepath "$localpathToMpsForImport\$mp" } else { #Echo "Skipping all non-library & non-Leaf MPs" } } # # Step #4: Import all Custom MPs # Echo "" Echo " - Importing Custom MPs" foreach ($mp in $mpList) { if($mp.name.StartsWith("Microsoft")) { #Echo "Skipping all JEE MPs" } else { Echo " * Importing $mp" Install-ManagementPack -filepath "$localpathToMpsForImport\$mp" } } } ####################################################################### # # Helper method that will remove the list of given Management Packs. # function RemoveMPsHelper {param($mpList) foreach ($mp in $mpList) { Echo " * Uninstalling $mp" Uninstall-ManagementPack -managementpack $mp } } ####################################################################### # # Remove 'all' of the JEE MPs as well as MPs that are dependent on them. # The remove is done by finding the base MP (Microsoft.JEE.Library) and finding # all MPs that depend on it. This list will be presented to the user prompting # if the user wants to continue and removing the list of presented MPs # function RemoveMPs { Import-Module 'C:\Program Files\System Center Operations Manager 2012\Powershell\OperationsManager\OperationsManager.psd1' $listOfManagementPacksToRemove = Get-SCOMManagementPack -Name Microsoft.JEE.Library -Recurse $listOfManagementPacksToRemove | format-table name $title = "Uninstall Management Packs" $message = "Do you want to uninstall the above management packs" $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Uninstall selected Management Packs." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not remove Management Packs." $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) $result = $host.ui.PromptForChoice($title, $message, $options, 0) switch ($result) { 0 {RemoveMPsHelper $listOfManagementPacksToRemove} 1 {"Exiting without removing any management packs"} } } ####################################################################### # Begin Script functionality # if ($args.Count -ne 1) { Echo "Improper command line arguments" Echo "" Echo "Specify a command line argument to either import or export MPs" exit 1 } else { $firstArg = $args[0] if ($firstArg -ieq "-import" -or $firstArg -ieq "-i") { if (test-path $localpathToMpsForImport -pathType container) { add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; $cwd = get-location set-location "OperationsManagerMonitoring::"; $mgConnection = new-managementGroupConnection -ConnectionString:$rootMS; ImportMps set-location $cwd remove-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; } else { Echo " Could not find $localpathToMpsForImport" Echo " Unable to location the directory containing the Management Packs." Echo " Please verify the variable 'localpathToMpsForImport' has been" Echo " properly set to the fullpath to the directory contains the MPs." Exit 31379 } } elseif ($firstArg -ieq "-remove" -or $firstArg -ieq "-r") { add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; $cwd = get-location set-location "OperationsManagerMonitoring::"; $mgConnection = new-managementGroupConnection -ConnectionString:$rootMS; RemoveMPs set-location $cwd remove-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; } else { Echo "Unknown command line arguments" Echo "" Echo "Command line argument '$args.[1]' was not understood" exit 1 } }