SCCM is a bit fiddly it has its “own” powershell console that is a tad inconvenient. Also a lot of assumptions are made that the user is all good to go with SCCM cmdlets and that is usually far from the truth.
The following script is does few prerequisite checks to ensure it is all good to go for the novice SCCM Powershell user. The script is self documenting as to what checks I am doing. These check have been learnt the hard way. 🙂
For the novice:
All SCCM Cmdlets have to run from the $SCCMSiteCodePath Location
Assumption is that the Security group ($SecurityGroupthatHasSSCMRights in the code) does have the correct rights to run the powershell functions against SCCM
#Script does a bunch of checks to ensure that the user is setup to be able to run SCCM Powershell commands
#All SCCM Cmdlets have to run from the $SCCMSiteCodePath Location
#Assumption is that the Security group $SecurityGroupthatHasSSCMRights does have the correct rights to run the powershell functions against SCCM
$SiteServer = (Get-CMSite).Servername
$SCCMSiteCode = (Get-CMSite).Sitecode
$SCCMSiteCodePath = $SCCMSiteCode + ":"
$SecurityGroupthatHasSSCMRights = "SEC_SCCM_Admin"
#Check if SCCM Console has been run on this user profile before
if (($env:SMS_ADMIN_UI_PATH) -ne $null)
Write-Host "SCCM Environment path exists:" $env:SMS_ADMIN_UI_PATH -ForegroundColor Green
Write-Error "You have not run up the SCCM Console before .... please run up SCCM console first and then re-run script exiting........"
#Check if logged in with Account as required for SCCM
if ((Get-ADPrincipalGroupMembership -Identity $env:USERNAME |where name -eq $SecurityGroupthatHasSSCMRights) )
Write-Host "User: $env:USERNAME belongs to $SecurityGroupthatHasSSCMRights AD Group" -ForegroundColor Green
Write-Error "You do not belong to the $SecurityGroupthatHasSSCMRights group.. Gain access and then re-run script exiting........"
#Check for the fact the user has run SCCM Console once
#If they have then the "HKCU:\Software\Microsoft\ConfigMgr10" registry entry will exist
Set-Location "HKCU:\Software\Microsoft\ConfigMgr10" -ErrorAction Stop
Write-Host "User: $env:Username has run SCCM Console before carrying on" -ForegroundColor Green
Write-error "User: $env:Username has NOT run SCCM Console before ... Exiting"
Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
#Set current directory to SCCM site
Set-Location -Path $SCCMSiteCodePath