You can retrieve a specific Package in SCCM through the SMS_Package class. In the example below we use a WQL statement to retrieve a Package where the Name is equal to “Adobe Reader”.
PS > Get-WmiObject -Namespace "root\SMS\Site_LAB" `
>> -Query "Select * from SMS_Package WHERE Name = 'Adobe Reader'"
__GENUS : 2
__CLASS : SMS_Package
__SUPERCLASS : SMS_PackageBaseclass
__DYNASTY : SMS_BaseClass
__RELPATH : SMS_Package.PackageID="LAB00016"
__PROPERTY_COUNT : 38
__DERIVATION : {SMS_PackageBaseclass, SMS_BaseClass}
__SERVER : SCCM
__NAMESPACE : root\SMS\Site_LAB
__PATH : \\SCCM\root\SMS\Site_LAB:SMS_Package.PackageID="LAB00016"
ActionInProgress : 0
AlternateContentProviders :
Description :
ExtendedData :
ExtendedDataSize : 0
ForcedDisconnectDelay : 5
ForcedDisconnectEnabled : False
ForcedDisconnectNumRetries : 2
Icon :
IconSize : 0
IgnoreAddressSchedule : False
ISVData :
ISVDataSize : 0
Language : English
LastRefreshTime : 20100926161618.000000+***
Manufacturer :
MIFFilename :
MIFName :
MIFPublisher :
MIFVersion :
Name : Adobe Reader
PackageID : LAB00016
PackageType : 0
PkgFlags : 0
PkgSourceFlag : 2
PkgSourcePath : \\sccm\Sources\Applications\Adobe Reader 9
PreferredAddressType :
Priority : 2
RefreshPkgSourceFlag : False
RefreshSchedule :
ShareName :
ShareType : 1
SourceDate : 20100926161610.000000+***
SourceSite : LAB
SourceVersion : 3
StoredPkgPath :
StoredPkgVersion : 0
Version : 9
As you can see, a package contains alot of useful information. We can also store the instance in a variable as shown below.
PS > $pkgAdobe = Get-WmiObject -Namespace "root\SMS\Site_LAB" `
>> -Query "Select * from SMS_Package WHERE Name = 'Adobe Reader'"
If we want to display specific properties we can use the Select-Object cmdlet.
PS > $pkgAdobe | Select-Object -Property Name, Description, Version |
>> Format-Table -AutoSize
Name Description Version
---- ----------- -------
Adobe Reader 9
In the example above we use Select-Object to display the Name, Description and Version properties. Notice that the Description property is empty. Let’s go ahead and change the description using Windows PowerShell.
PS > $pkgAdobe.Description = "PowerShell Rocks"
PS > $pkgAdobe.Put()
Path : \\localhost\root\SMS\Site_LAB:SMS_Package.PackageID="LAB00016"
RelativePath : SMS_Package.PackageID="LAB00016"
Server : localhost
NamespacePath : root\SMS\Site_LAB
ClassName : SMS_Package
IsClass : False
IsInstance : True
IsSingleton : False
In the example above we set the description to “PowerShell Rocks” and call the put() method to commit the changes on the package. If we Select the Description property we’ll see that its changed.
PS > $pkgAdobe | Select-Object -Property Name, Description, Version |
>> Format-Table -AutoSize
Name Description Version
---- ----------- -------
Adobe Reader PowerShell Rocks 9
Apart from displaying and modifying Packages in SCCM we can also create new Packages using Windows PowerShell. To achieve this we will use the Set-WmiInstance cmdlet. The Cmdlet requires a hashtable containing the properties and values that we want to use when creating a new package. First let’s see how we can create a hashtable:
PS > $arguments = @{Name = "Windows PowerShell";
>> Description = "Worlds Coolest Product";
>> Version = "2.0";
>> Language = "English";
>> PackageType = 0;
>> PkgSourceFlag = 2;
>> PkgSourcePath = "\\sccm\sources\Applications\PowerShell"
>> }
In the example above we specify that the Name of our new package should be “Windows PowerShell”, next we set the Description to “2.0″ and the Language to “English”. We also set the PackageType to 0 – which indicates that its a regular software distribution package and PkgSourceFlag to 2 – which indicates that the Package takes source files directly from the source without compression. Finally we set point the PkgSourcePath to the location of the actual files.
If we type $arguments the hashtable is displayed.
PS > $arguments
Name Value
---- -----
Name Windows PowerShell
PkgSourcePath \\sccm\sources\Applications\PowerShell
Language English
Version 2.0
Description Worlds Coolest Product
PackageType 0
PkgSourceFlag 2
With the HashTable in place we can go ahead and create a new package using the Set-WmiInstance cmdlet as shown below.
PS > $newPackage = Set-WmiInstance -class SMS_Package `
>> -arguments $arguments -namespace "root\SMS\Site_LAB"
Pretty cool. If we pipe the variable to the Select-Object cmdlet and display the Name, Description and PackageID you’ll notice that PackageID is empty.
PS > $newPackage | Select-Object Name, Description, PackageID
Name Description PackageID
---- ----------- ---------
Windows PowerShell Worlds Coolest Product
To generate a PackageID, the instance object needs to be called, a simple way to do this is to pipe the object to the Format-List cmdlet as shown below.
PS > $newPackage | Format-List | Out-Null
If we select the PackageID now an ID will have been generated.
PS > $newPackage | Select-Object Name, Description, PackageID
Name Description PackageID
---- ----------- ---------
Windows PowerShell Worlds Coolest Product LAB00024
When we create a new Package it’s placed directly under “Packages”.
If we want to move the package to a specific folder we use the SMS_ObjectContainerItem class. First we retrieve the “Folder” that we want to place the package in using the SMS_ObjectContainerNode class. In the WQL statement we specify that the container name is equal to “Applications” and that the ObjectType is equal to 2. Setting the ObjectType to “2″ indicates that the object placed in the container will be of the type SMS_Package.
PS > $containerNode = Get-WmiObject -Namespace "root\SMS\Site_LAB" `
>> -Query "Select * from SMS_ObjectContainerNode WHERE Name = 'Applications' AND ObjectType = '2'"
PS > $containerNode
__GENUS : 2
__CLASS : SMS_ObjectContainerNode
__SUPERCLASS : SMS_BaseClass
__DYNASTY : SMS_BaseClass
__RELPATH : SMS_ObjectContainerNode.ContainerNod
__PROPERTY_COUNT : 9
__DERIVATION : {SMS_BaseClass}
__SERVER : SCCM
__NAMESPACE : root\SMS\Site_LAB
__PATH : \\SCCM\root\SMS\Site_LAB:SMS_ObjectC
ContainerNodeID : 11
FolderFlags : 0
FolderGuid : 32538291-DAB7-4E9C-92F4-58CAE9B641C1
Name : Applications
ObjectType : 2
ParentContainerNodeID : 0
SearchFolder : False
SearchString :
SourceSite : LAB
Next we build up a HashTable containing the Property Names and Values that we want to use. In this example we use InstanceKey and set the value to the PackageID, ObjectType points to “2″ and finally we set the COntainerNodeID to the COntainerNodes ID.
PS > $arguments = @{
>> InstanceKey = $newPackage.PackageID;
>> ObjectType = 2;
>> ContainerNodeID = $containerNode.ContainerNodeID
>> }
PS > $arguments
Name Value
---- -----
ObjectType 2
InstanceKey LAB00024
ContainerNodeID 11
Finally we use the SMS_ObjectContainerItem class to move the package as demonstrated below.
PS > Set-WmiInstance -Class SMS_ObjectContainerItem `
>> -arguments $arguments -namespace "root\SMS\Site_LAB"
__GENUS : 2
__CLASS : SMS_ObjectContainerItem
__SUPERCLASS : SMS_BaseClass
__DYNASTY : SMS_BaseClass
__RELPATH : SMS_ObjectContainerItem.MemberID=137
__PROPERTY_COUNT : 6
__DERIVATION : {SMS_BaseClass}
__SERVER : SCCM
__NAMESPACE : root\SMS\Site_LAB
__PATH : \\SCCM\root\SMS\Site_LAB:SMS_ObjectContainerItem.MemberID=137
ContainerNodeID : 11
InstanceKey : LAB00024
MemberGuid : 06032D6C-6426-411E-912F-462DF35D2ADE
MemberID : 137
ObjectType : 2
SourceSite : LAB
If we take a look in the Configuration Manager Console we’ll see that the Package is moved to the “Applications” folder.
In the
next post we’ll see how to work with Programs using Windows PowerShell.