Unlocking Locked Objects in the SCCM Console using Powershell

Have you ever come across a lovely message like this from the SCCM 2012 console?  In this case I wanted to open the properties of a package but somehow SCCM still has a handle on it that it won’t release.  This could be because someone else is currently modifying it, but in this case I only just created it so I know that’s not true.

console

The solution is to delete the record for the locked object in the SQL database.  You can do this from the SQL server, but hey, why not do it with PowerShell for a quicker fix?

This script will query the ConfgMgr SQL server database for locked objects, list them in a Grid, ask you which object you want to unlock, then delete the record from the database.

Note the script assumes you are using an account that has the necessary permissions in the SQL database.

First, we get the list of locked objects and put them into a grid view so we can filter and find the object we need in case there are more than one.

grid

If it’s something that you think was locked recently, you can sort by the ‘AssignmentTime’ column and find the latest one.  Or if you think it was done by a specific user account, you can sort by ‘AssignedUser’.

The first column contains the ID for that record in the database.  The script asks you to input this record ID, then it will delete the record:

ps

Don’t forget to add your SQL server and database name to the beginning of the script.


<#

This script unlocks an object that is locked in the SCCM console.

#>

# Database info
$dataSource = “mysqlserver\INST_SCCM”
$database = “CM_ABC”

# Open a connection
cls
Write-host "Opening a connection to '$database' on '$dataSource'"
$connectionString = “Server=$dataSource;Database=$database;Integrated Security=SSPI;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()

# Get the list of locked objects, output to Grid
Write-host "Getting the list of locked objects"
$query = "select * from SEDO_LockState where LockStateID <> 0"
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()

$table = new-object “System.Data.DataTable”
$table.Load($result)
$table | Out-GridView

# Prompt for the record ID to unlock
$ID = Read-Host ">>Enter the ID number of the record you want to unlock"

# Get the LockID of the object
Write-host "Getting the LockID of this object"
$query2 = "select LockID from SEDO_LockState where ID = '$($ID)'"
$command = $connection.CreateCommand()
$command.CommandText = $query2
$result = $command.ExecuteReader()

$table = new-object “System.Data.DataTable”
$table.Load($result)
$table = $table.LockID

# Delete the object
Write-Host "Deleting the object record"
$query3 = "DELETE from SEDO_LockState where LockID = '$($table)'"
$command = $connection.CreateCommand()
$command.CommandText = $query3
$result = $command.ExecuteReader()

# Close the connection
$connection.Close()

More info here: http://myitforum.com/myitforumwp/2013/02/22/unlocking-configmgr-2012-objects/

Advertisements

2 thoughts on “Unlocking Locked Objects in the SCCM Console using Powershell

    1. True Torsten, thanks for pointing it out! The Unlock-CMObject cmdlet is available since SP1 and is good if you have the name or the package ID of the locked object. I still like this way of getting the list of locked items directly from the database and just entering the ID, though not a supported method, as you say.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s