Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powser Shell Script to Purge Computer Records from SCCM

$
0
0

I am building a PowerShell Script to obtain the names of the computer resources stored in a SCCM 2012 collection and place them in a variable. Next, I want to obtain the computer resources (e.g., ddr records) and store them in a 2ed variable using the first variable to pipe in the data. I am using a “ForEach” looping construct to accomplish this. Lastly, I want to purge the computers records from the Configuration Manager database that are stored in the second variable which came from the computers names of the specified SCCM collection. I am attempting to use this line of code within my loop to purge the computer records from SCCM:

$compObject.psbase.syncroot | % { $_.psbase.delete() }

Here is a blog I found suggesting this line of code.

What I have discovered is rather interesting. My script works as intended, but even though the correct records  AREbeing purged. The above line of code is returning an error.

You cannot call a method on a null-valued expression.
At line:18 char:35
+ $compObject.psbase.syncroot | % { $_.psbase.delete() }
+                                 ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

 

I have a few questions:

1.     Does anyone know why this is occurring?

2.     Is there a different approach or code I should be using instead to purge the collected data?

 

Here is my full script:

#// required parameters

$SCCMServer="doit-sccm-site"

$sitename="PS1"

$collectionID="PS1009c8"

 

 

#// Obtain collection members from $collectionID

$SMSClients=Get-WmiObject-Query"SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='$collectionID' order by name"  -ComputerName$SCCMServer-Namespace "ROOT\SMS\site_$sitename"

 

#//looping construct to loop through all the colection members

ForEach ($SMSclientin$SMSclients) {

 

#// Loads computer records one at a time into $compobject variable using a ForEachloop to be deleted

$compObject=get-wmiobject-query"select * from SMS_R_SYSTEM WHERE Name='$($smsclient.name)'"-computername$SCCMServer-namespace"ROOT\SMS\site_$sitename"

 

#// Delete’s computer objects one at a time in $compObject variable from CM12 database

 

$compObject.psbase.syncroot |% { $_.psbase.delete() }

 

 

--Tony


Viewing all articles
Browse latest Browse all 6937

Trending Articles