How to connect to Configuration Manager site with PowerShell

How to connect to Configuration Manager site with PowerShell

There are two ways to connect to Configuration Manager site:

  • From Configuration Manager console
  • From PowerShell

To connect from PowerShell, you can just click on the top-left arrow icon pointing downward to expand the menu and then select Connect via Windows PowerShell or Connect via Windows PowerShell ISE.

Alternatively, you can also just launch PowerShell as the currently logged-in user and then run the following script, which, you can get when click on Connect via Windows PowerShell ISE from SCCM console above.

# Site configuration
$SiteCode = "CON" # <-- Site code. Replace CON with the actual site code.
$ProviderMachineName = "contoso.test.com" # <-- Replace contoso.test.com with the actual FQDN of the machine name
 
# Customizations
$initParams = @{}
 
# Do not change anything below this line
 
# Import the ConfigurationManager.psd1 module 
if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
}
 
# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
 
# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams 

References

 “The Thread is not in Background Processing Mode” when trying to download updates from SCCM console (0x80070193)

“The Thread is not in Background Processing Mode” when trying to download updates from SCCM console and patchdowner.log prints “HTTP_STATUS_FORBIDDEN or HTTP_STATUS_DENIED” and 0x80070193

ISSUE

The Thread is not in Background Processing Mode” when trying to download updates from SCCM console

This can happen when you try to manually download updates from Configuration Manager console. In the patchdownloader.log you may see the following error:

HTTP_STATUS_FORBIDDEN or HTTP_STATUS_DENIED
Error: DownloadUpdateContent() failed with hr=0x80070193

Error Code: 0x80070193 (403)
Error Name: ERROR_PROCESS_MODE_NOT_BACKGROUND
Error Source: Windows
Error Message: The process is not in background processing mode.

Here I adapted what Scott Breen explained in the tech forum for reference which can put this issue through, I think:

Configuration Manager needs to internet access to download source files for Software Updates. This process goes by the following way:
 – Way 1: The Configuration Manager server during the execution of an Automated Deployment Rules (ADR);
 – Way 2: The Configuration Manager console (in the user context on the computer where the console is running) during creation of a Software Update package.

Note that the download process requires the computer or user account where the download originates to have access direct to the internet or through a proxy server.

The proxy server configured within Configuration Manager settings is for the server initiated downloads only (Way 1 above) – NOT the console initiated downloads.

If you are downloading via Configuration Manager console, you need to ensure that the account you are using has proxy settings configured in Internet Options (or Internet Explorer options) – https://support.microsoft.com/en-us/kb/2777643. Note that if you logged on with a different account, but are running the console as another user you will need to option Internet Options as that user (which you can do by running Internet Explorer as that account).

Scott Breen MSFT

For my case, the solution is to fill in the right proxy in Internet Browser. And volia, issue gone!

References

How to trigger Configuration Manager Actions with command

How to trigger Configuration Manager Actions with command

Many times, you may want to trigger SCCM Actions with command to simulate manual click.

Microsoft provides an official documentation on how to do that with PowerShell. Here is the link – https://learn.microsoft.com/en-us/mem/configmgr/develop/reference/core/clients/client-classes/triggerschedule-method-in-class-sms_client

However, you can be frustrated when it comes to triggering actions like Application Deployment Evaluation Cycle. In this case, that article is not going to help you because the PowerShell commands only work when they are run as Admin. What if I just want to simulate manual click to trigger even user-based actions?

Here is a way on how to do that. The following code is an example for Updates Source Scan Cycle in Visual Basic.

Sub InitiateSoftwareUpdatesScanCycle()

' Set the required variables.
actionNameToRun = "Updates Source Scan Cycle"

' Create a CPAppletMgr instance.
Dim oCPAppletMgr
Set oCPAppletMgr = CreateObject("CPApplet.CPAppletMgr")

' Get the available ClientActions object.
Dim oClientActions
Set oClientActions = oCPAppletMgr.GetClientActions()

' Loop through the available client actions. Run the matching client action when it is found.
Dim oClientAction
For Each oClientAction In oClientActions
If oClientAction.Name = actionNameToRun Then
oClientAction.PerformAction
End If
Next
wscript.echo "Ran: " & actionNameToRun

End Sub

Save the script in vbs file and run it in user context. There you go! It works just as the logged-in user click on that particular action.

If you need to trigger other actions, just replace with the following action names:

  • Software Metering Usage Report Cycle
  • Request & Evaluate Machine Policy
  • Updates Source Scan Cycle
  • Request & Evaluate User Policy
  • Hardware Inventory Collection Cycle
  • Software Inventory Collection Cycle
  • Application Global Evaluation Task
  • Software Updates Assignments Evaluation Cycle
  • Discovery Data Collection Cycle
  • MSI Product Source Update Cycle
  • Standard File Collection Cycle

Official document link is here – https://learn.microsoft.com/en-us/previous-versions/system-center/developer/cc144313(v=msdn.10)?redirectedfrom=MSDN

Design a site like this with WordPress.com
Get started