Friday, November 13, 2015

Using PowerShell to Query a Site Server

Quick little example of how to use Get-WMIObject to pull data from the Primary (or CAS?).

This is useful since sometimes it's very easy to put together a query in SQL Managlement Studio, the WQL translates very closely.

Enjoy!

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
FUNCTION Get-SCCMSqlQuery
{
    Param([parameter(Mandatory=$true)]$ApplicationName,$CollectionName,$SiteName,$SCCMPrimary)
        $SCCMWMI="root\sms\site_$SiteName"
    Get-WmiObject -namespace $SCCMWMI -computer $SCCMPrimary -query "SELECT AssignmentID,
    Assignment_UniqueID,CollectionID,CollectionName,ApplicationName FROM SMS_ApplicationAssignment
    WHERE ApplicationName LIKE '$ApplicationName' AND CollectionName
    LIKE '$CollectionName'"
 | Select-Object ApplicationName,CollectionName,AssignmentID
}

$App = 'TestApp'
$Collection = 'TestCollection'

#Example Returns
 Get-SCCMSqlQuery -ApplicationName $App -CollectionName $Collection -SiteName 'XYZ' -SCCMPrimary 'contoso.website.com'
(Get-SCCMSqlQuery -ApplicationName $App -CollectionName $Collection -SiteName 'XYZ' -SCCMPrimary 'contoso.website.com').AssignmentId

No comments:

Post a Comment