Skip to content

Instantly share code, notes, and snippets.

@rezarahimian
Created December 9, 2019 03:29
Show Gist options
  • Save rezarahimian/1feee2eba88ee4e4d3923891d31a1e35 to your computer and use it in GitHub Desktop.
Save rezarahimian/1feee2eba88ee4e4d3923891d31a1e35 to your computer and use it in GitHub Desktop.
function Get-SCCMData
{
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$true)][ValidateSet('AllDevices','HWModel')][String] $QueryName,
[Parameter(Mandatory=$true)][String] $CredentialPath,
[Parameter(Mandatory=$false)][String] $SQLInstance = 'MSSQLSERVER\SQL_PRD',
[Parameter(Mandatory=$false)][String] $DBName = 'SCCM_DB_PRD',
[Parameter(Mandatory=$false)][String[]] $QueryParam
)
try
{
$SCCMData = $null
$Credential = [System.Management.Automation.PSCredential](Import-Clixml -Path $CredentialPath)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password))
Switch ($QueryName)
{
'AllDevices' { $Query = "select SMS_R_SYSTEM.ResourceID, SMS_R_SYSTEM.ResourceType, SMS_R_SYSTEM.Name, SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_System.OperatingSystemNameandVersion,SMS_R_SYSTEM.ResourceDomainORWorkgroup, SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.OperatingSystemNameandVersion like 'Microsoft Windows NT Server%' OR SMS_R_System.OperatingSystemNameandVersion like 'Microsoft Windows NT Advanced Server%'" }
'HWModel' { $Query = "select distinct SMS_G_System_COMPUTER_SYSTEM.Manufacturer, SMS_G_System_COMPUTER_SYSTEM.Model from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId where SMS_G_System_COMPUTER_SYSTEM.Model = '$($QueryParam[0])'"}
default { $Query = $null }
}
if ($Query)
{
Write-Verbose -Message ('Connecting to "{0}" on "{1}"...' -f $DBName, $SQLInstance)
$SCCMData = Invoke-Sqlcmd -Query $Query -Database $DBName -ServerInstance $SQLInstance -Username $Credential.UserName -Password $Password
Write-Verbose -Message ('Retrieved "{0}" records from SCCM DB...' -f ($SCCMData | Measure-Object).Count)
}
else
{
Write-Verbose -Message ('Empty SQL query!')
}
}
catch
{
Write-Verbose -Message $_.Exception.Message
}
Return $SCCMData
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment