.NET应用通过WMI远程执行SCCM应用安装/卸载的技术求助
SCCM Application Install/Uninstall via WMI in .NET: Complete Guide
Hey there! You're already off to a solid start with your VB.NET code for interacting with SCCM via WMI. Let's fill in the missing pieces and get those install/uninstall operations running smoothly.
First: Finish the Install Method
Your existing code sets up the ManagementScope and targets the CCM_Application class, but you need to populate the required parameters for the Install method. Here's what you're missing:
Required Parameters for Install
Id: The unique CI ID of your SCCM application (find this in the SCCM console under Application Properties > General > CI Unique ID)RevisionId: The revision ID of the application (also located in the same properties pane)IsMachineTarget: Set toTruefor machine-wide installs,Falsefor user-specific installsPriority: Typically2(normal priority; valid values are 1=high, 2=normal, 3=low)
Here's the complete install code snippet:
Dim wmiLoc As ManagementScope = New ManagementScope("\\" & Target & "\root\ccm\ClientSDK", Options) wmiLoc.Connect() ' Don't forget to establish the WMI connection! Dim wmiProcPath As ManagementPath = New ManagementPath("CCM_Application") Dim mc As ManagementClass = New ManagementClass(wmiLoc, wmiProcPath, Nothing) ' Get and populate install method parameters Dim inParams As ManagementBaseObject = mc.GetMethodParameters("Install") inParams("EnforcePreference") = 0 ' 0 = install as required, 1 = install as available inParams("Id") = "YOUR_APPLICATION_CI_UNIQUE_ID" ' Replace with your app's ID inParams("RevisionId") = "YOUR_APPLICATION_REVISION_ID" ' Replace with your app's revision ID inParams("IsMachineTarget") = True ' Adjust based on your target scope inParams("Priority") = 2 ' Normal priority ' Execute the install method Dim outParams As ManagementBaseObject = mc.InvokeMethod("Install", inParams, Nothing) ' Handle the return value Dim returnValue As Integer = Convert.ToInt32(outParams("ReturnValue")) Select Case returnValue Case 0 Console.WriteLine("Install initiated successfully!") Case 1 Console.WriteLine("Install failed. Check SCCM client logs for details.") Case 2 Console.WriteLine("Install is already in progress on the target machine.") ' Add more case handling for other return codes as needed End Select
Uninstall Method Implementation
Uninstalling follows a nearly identical pattern, but uses the Uninstall method of the CCM_Application class. Here's how to set it up:
' Reuse your existing ManagementScope and ManagementClass setup here Dim uninstallParams As ManagementBaseObject = mc.GetMethodParameters("Uninstall") uninstallParams("Id") = "YOUR_APPLICATION_CI_UNIQUE_ID" uninstallParams("RevisionId") = "YOUR_APPLICATION_REVISION_ID" uninstallParams("IsMachineTarget") = True uninstallParams("Priority") = 2 ' Execute the uninstall method Dim uninstallOutParams As ManagementBaseObject = mc.InvokeMethod("Uninstall", uninstallParams, Nothing) ' Handle the uninstall return value Dim uninstallReturnValue As Integer = Convert.ToInt32(uninstallOutParams("ReturnValue")) Select Case uninstallReturnValue Case 0 Console.WriteLine("Uninstall initiated successfully!") Case 1 Console.WriteLine("Uninstall failed. Check SCCM client logs for details.") Case 2 Console.WriteLine("Uninstall is already in progress on the target machine.") End Select
Critical Notes & Troubleshooting
- Permissions: The account running your .NET app must have local administrator rights on the remote machine, plus SCCM client permissions to initiate installs/uninstalls.
- WMI Connectivity: Ensure the remote machine's firewall allows WMI traffic (TCP port 135 and dynamic RPC ports). You can test connectivity with
wbemtest.exeby connecting to\\TARGET\root\ccm\ClientSDKfrom your local machine. - Parameter Accuracy: Double-check that
IdandRevisionIdmatch exactly what's in the SCCM console—typos here will cause silent failures. - Error Handling: Wrap your WMI calls in
Try/Catchblocks to handle common issues likeManagementException(WMI connectivity/permission problems) andInvalidOperationException.
Example error handling:
Try wmiLoc.Connect() ' Rest of your install/uninstall code here Catch ex As ManagementException Console.WriteLine($"WMI Error: {ex.Message} | Error Code: {ex.ErrorCode}") Catch ex As Exception Console.WriteLine($"General Error: {ex.Message}") End Try
内容的提问来源于stack exchange,提问作者Rathfon
相关产品推荐
相关产品推荐

