How to get Management Pack Bundle file information from SCOM?

505 views Asked by At

I am using OperationsManager module to work with SCOM, I need to find the somemanagementpack.mpb file information from SCOM which is already imported in SCOM an than need to delete the same somemanagementpack.mpb file locally based on the version

Below is the command I am using

Import-Module "OperationsManager"
New-SCOMManagementGroupConnection -ComputerName "DEVSCOM"
$mp = Get-SCManagementPack -BundleFile C:\Temp\somemanagementpack.mpb

$version = $mp.Version
$localVersion = "1.0.0.0"

if($version -gt $localVersion)
{
    Remove-Item "C:\Temp\somemanagementpack.mpb" -Force
}

but when I am trying to remove it getting below error, I have also tried using Dispose method but nothing happens

The action can't be completed because the file is open

1

There are 1 answers

0
TMAC 23 On BEST ANSWER

SCOM is locking the file. The only way to get it to stop is to kill the PowerShell process.

As a workaround I recommend making a copy of each file first to another directory. Have your script get the version from the copy. Then delete the original file that won't be locked if it matches your criteria. After you are done close the PowerShell window and delete the directory with all the copied files.

    Import-Module "OperationsManager"
    New-SCOMManagementGroupConnection -ComputerName "DEVSCOM"
    Copy-Item C:\Temp\somemanagementpack.mpb C:\Temp\Copy\somemanagementpack.mpb

    $mp = Get-SCManagementPack -BundleFile C:\Temp\Copy\somemanagementpack.mpb

    $version = $mp.Version

    $localVersion = "1.0.0.0"

    if($version -gt $localVersion)
    {
    Remove-Item "C:\Temp\somemanagementpack.mpb" -Force
    }

As you can see only the copy gets locked

Copy Locked