Tuesday, September 14, 2010

SCCM : converting programs to Windows 7

This is a VBS program I wrote for a customer, who has a SCCM 2007 R2 environment.

They had over 400 programs in various packages, and they weren't sure if all of them would run under Windows 7.

Visually, we had to make sure this box was ticked.

After a little digging in the SCCM SDK (link), and a script from Stuart James, the following will go through ALL programs in ALL packages. If a program is not set to "Run on all platforms", we add that it can run on "All x86 Windows 7"

It outputs changes with comma separated values (csv) so you can send this to a file and dress it up nice in Excel for your boss :-)

'===================================== 
'SetRunFromTS - Sets all programs to be able to run in Windows 7 x86
'Author: Patrick Paumier / Stuart James 
' 
'Requirements: Change line 22 to connect to your site server
' 
'Usage: CScript xxx.vbs or double click 
'===================================== 


'Check we're using CScript and if not then relaunch 
If "CSCRIPT.EXE" <> UCase(Right(WScript.Fullname, 11)) Then 
Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "CSCRIPT.EXE /nologo " & WScript.ScriptFullName 
Wscript.Quit 
End If 

' Setup a connection to the local provider. 
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set swbemServices= swbemLocator.ConnectServer("MY-SCCM-SERVER", "root\sms") 
Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation") 

For Each Location In providerLoc 
If location.ProviderForLocalSite = True Then 
Set swbemServices = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode) 
Exit For 
End If 
Next 

'Main call 
QueryPrograms swbemServices 

Sub QueryPrograms(connection) 

On Error Resume next 

Dim programs 
'Dim program 
' Run the query. 
Set programs = connection.ExecQuery("Select * From SMS_Program WHERE PackageID='TDE0024F'") 

If Err.Number<>0 Then 
Wscript.Echo "Couldn't get programs" 
Wscript.Quit 
End If 
For Each program In programs 
ModifyProgram connection,program.PackageID, program.ProgramName 
Next 
If programs.Count=0 Then 
Wscript.Echo "No packages found" 
End If 

End Sub 

Sub ModifyProgram (connection, existingPackageID, existingProgramName) 

' Build a query to get the specified package. 
packageQuery = "SMS_Package.PackageID='" & existingPackageID & "'" 

' Run the query to get the package. 
Set package = connection.Get(packageQuery) 
' Output package name and ID. 
wscript.echo VBCrLf 'New Line
Wscript.StdOut.Write package.PackageID & "," 'Package ID
Wscript.StdOut.Write package.Name & "," 'Package Name
' Build a query to get the programs for the package. 
programQuery = "SELECT * FROM SMS_Program WHERE PackageID='" & existingPackageID & "'" 
' Run the query to get the programs. 
Set allProgramsForPackage = connection.ExecQuery(programQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately) 
'The query returns a collection of program objects that needs to be enumerated. 
For Each program In allProgramsForPackage                
If program.ProgramName = existingProgramName Then 
'Get all program object properties (in this case we specifically need some lazy properties).
programPath = program.Put_
Set program = connection.Get(programPath) 

' Output the program name 
Wscript.StdOut.Write program.ProgramName & "," ' Program Name
Wscript.StdOut.Write program.ProgramFlags & "," 'Program Flags

If program.ProgramFlags AND 2^27 Then
Wscript.StdOut.Write "OK for all platforms"
Else
' RUN_ON_SPECIFIED_PLATFORMS is set.
Win7OK = FALSE
For Each myOS in program.SupportedOperatingSystems
osver = Left(myOS.MinVersion,3)
If osver="6.1" And myOS.Platform="I386" Then
Wscript.StdOut.Write "OK for Windows 7" ' Name: " & myOS.Name & " MinVersion: "& myOS.MinVersion & " MaxVersion: " & myOS.MaxVersion & " Platform: " & myOS.Platform
Win7OK = TRUE
Exit For
End If
Next
If Win7OK = FALSE Then
'Add Windows 7 32bit platform
' Create 
Set tempSupportedPlatform = connection.Get("SMS_OS_Details").SpawnInstance_
' Populate tempSupportedPlatform values.    
tempSupportedPlatform.MaxVersion = "6.10.9999.9999"
tempSupportedPlatform.MinVersion = "6.10.0000.0"
tempSupportedPlatform.Name       = "Win NT"
tempSupportedPlatform.Platform   = "I386"

' Get the array of supported operating systems.
tempSupportedPlatformsArray = program.SupportedOperatingSystems   

' Add the new supported platform values (object) to the temporary array.
ReDim Preserve tempSupportedPlatformsArray (Ubound(tempSupportedPlatformsArray) + 1)
Set tempSupportedPlatformsArray(Ubound(tempSupportedPlatformsArray)) = tempSupportedPlatform

' Replace the SupportedOperatingSystems object array with the new updated array.
program.SupportedOperatingSystems = tempSupportedPlatformsArray

' Save the program.
program.Put_

' Output success message.

Wscript.StdOut.Write "Added Win7"
End If
End If
End If        
Next 
End Sub 

No comments:

Post a Comment