# – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# Stéphane van Gulick / PowerShellDistrict.Com
# Rikard Ronnkvist / snowland.se
# Usage:
# Save the file as SCCM-Commands.psm1
# PS:>Import-Module SCCM-Commands
# PS:>Get-SCCMCommands
#
# Current Version : 1.0
#
# 2009-04-07 Michael Niehaus Original code posted at http://blogs.technet.com/mniehaus/
# 2010-03-10 Rikard Ronnkvist Major makeover and first snowland.se release
# …
# 2014-01-01 Stéphane van Gulick New Functions: [Task Sequence Packages] : Get-SccmTaskSequencePackage, Get-SCCMTaskSequenceRelatedPackages
# New Functions:[Task Sequence] : Export-SCCMTaskSequence, Import-SCCMTaskSequencePackage
# – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
#Region BaseCommands
Function Get-SCCMCommands {
# List all SCCM-commands
[CmdletBinding()]
PARAM ()
PROCESS {
return Get-Command -Name *-SCCM* -CommandType Function | Sort-Object Name | Format-Table Name, Module
}
}
Function Connect-SCCMServer {
# Connect to one SCCM server
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$false,HelpMessage=“SCCM Server Name or FQDN”,ValueFromPipeline=$true)][Alias(“ServerName”,“FQDN”,“ComputerName”)][String] $HostName = (Get-Content env:computername),
[Parameter(Mandatory=$false,HelpMessage=“Optional SCCM Site Code”,ValueFromPipelineByPropertyName=$true )][String] $siteCode = $null,
[Parameter(Mandatory=$false,HelpMessage=“Credentials to use” )][System.Management.Automation.PSCredential] $credential = $null
)
PROCESS {
# Get the pointer to the provider for the site code
if ($siteCode -eq $null -or $siteCode -eq “”) {
Write-Verbose “Getting provider location for default site on server $HostName”
if ($credential -eq $null) {
$sccmProviderLocation = Get-WmiObject -query “select * from SMS_ProviderLocation where ProviderForLocalSite = true” -Namespace “rootsms” -computername $HostName -errorAction Stop
} else {
$sccmProviderLocation = Get-WmiObject -query “select * from SMS_ProviderLocation where ProviderForLocalSite = true” -Namespace “rootsms” -computername $HostName -credential $credential -errorAction Stop
}
} else {
Write-Verbose “Getting provider location for site $siteCode on server $HostName”
if ($credential -eq $null) {
$sccmProviderLocation = Get-WmiObject -query “SELECT * FROM SMS_ProviderLocation where SiteCode = ‘$siteCode'” -Namespace “rootsms” -computername $HostName -errorAction Stop
} else {
$sccmProviderLocation = Get-WmiObject -query “SELECT * FROM SMS_ProviderLocation where SiteCode = ‘$siteCode'” -Namespace “rootsms” -computername $HostName -credential $credential -errorAction Stop
}
}
# Split up the namespace path
$parts = $sccmProviderLocation.NamespacePath -split “”, 4
Write-Verbose “Provider is located on $($sccmProviderLocation.Machine) in namespace $($parts[3])”
# Create a new object with information
$retObj = New-Object -TypeName System.Object
$retObj | add-Member -memberType NoteProperty -name Machine -Value $HostName
$retObj | add-Member -memberType NoteProperty -name Namespace -Value $parts[3]
$retObj | add-Member -memberType NoteProperty -name SccmProvider -Value $sccmProviderLocation
return $retObj
}
}
Function Get-SCCMObject {
# Generic query tool
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipelineByPropertyName=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$true, HelpMessage=“SCCM Class to query”,ValueFromPipeline=$true)][Alias(“Table”,“View”)][String] $class,
[Parameter(Mandatory=$false,HelpMessage=“Optional Filter on query”)][String] $Filter = $null
)
PROCESS {
if ($Filter -eq $null -or $Filter -eq “”)
{
Write-Verbose “WMI Query: SELECT * FROM $class”
$retObj = get-wmiobject -class $class -computername $SccmServer.Machine -namespace $SccmServer.Namespace
}
else
{
Write-Verbose “WMI Query: SELECT * FROM $class WHERE $Filter”
$retObj = get-wmiobject -query “SELECT * FROM $class WHERE $Filter” -computername $SccmServer.Machine -namespace $SccmServer.Namespace
}
return $retObj
}
}
# – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
#endregion
#region Task Sequence
# – – – – – – – – – – Task Sequences – – – – – – – – – –
Function Get-SCCMTaskSequenceRelatedPackages {
<#
.SYNOPSIS
Returnes the list of packages that a present in a Task sequence.
.DESCRIPTION
Returnes the list of packages that a present in a Task sequence.
.PARAMETER SccmServer
Sccm Connection object created with Connect-SccmServer
.EXAMPLE
$Connection = Connect-SccmServer “MyServer01”
Get-SccmTaskSequenceRelatedPackages -SccmServer $connection
.NOTES
Author: Stéphane van Gulick
version: 1.0
History:
.LINK
www.PowerShellDistrict.com
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipeline=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$false, HelpMessage=“Task Sequence name”)][String] $Name,
[Parameter(Mandatory=$false, HelpMessage=“Path to the folder”)][String] $Description,
[Parameter(Mandatory=$false, HelpMessage=“Optional Filter on query”)][String] $Sequence = $null
)
PROCESS {
$References = (Get-SCCMTaskSequencePackage -SccmServer $SccmServer -Name $Name -Full).references
$Packages = @()
Foreach ($Reference in $References){
$PackageName = $null
$PackageName = (Get-SCCMPackage -SccmServer $SccmServer -PackageID $($Reference.package)).name
if ($PackageName){
$Properties = @{‘Name’=$PackageName;‘Program’=$($Reference.Program)}
$Packages += New-Object -TypeName psobject -Property $Properties
}
}
return $Packages
}
}
Function Import-SCCMTaskSequenceXML {
<#
.SYNOPSIS
Imports a Task Sequence based on a exported XML file.
.DESCRIPTION
Returnes the list of packages that a present in a Task sequence.
.PARAMETER SccmServer
Sccm Connection object created with Connect-SccmServer
.EXAMPLE
$Connection = Connect-SccmServer “MyServer01”
.NOTES
Author: Stéphane van Gulick
version: 1.0
History:
.LINK
www.PowerShellDistrict.com
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipeline=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$false, HelpMessage=“Name of the new task sequence”)][String] $Name,
[Parameter(Mandatory=$false, HelpMessage=“Description”)][String] $Description,
[Parameter(Mandatory=$false, HelpMessage=“Path to the XML file”)][String] $XMLFile = $null,
[Parameter(Mandatory=$false, HelpMessage=“BootImageID”)][String] $BootImageID =$null,
[Parameter(Mandatory=$false, HelpMessage=“DependentProgram”)][String] $DependentProgram
)
PROCESS {
#Gathering XML content :
$XMLFileContent = $XMLFile
#Getting SMS_TaskSequencePackage class
$TaskSequencePackageClass = [WmiClass](“$($SccmServer.machine)$($SccmServer.namespace):SMS_TaskSequencePackage”)
#Importing XML
$importedsequence = ($TaskSequencePackageClass.ImportSequence($XMLFileContent)).TaskSequence
#Creating Instance
$NewInstance = $TaskSequencePackageClass.createinstance()
$NewInstance.Name = $Name
$NewInstance.Description = $Description
$NewInstance.BootImageID= $BootImageID
$NewInstance.DependentProgram=$DependentProgram
#Importing Task Sequence
$TaskSequencePackageClass.SetSequence($NewInstance,$importedsequence)
return $importedsequence
}
}
Function Export-SCCMTaskSequence {
<#
.SYNOPSIS
Exports the SCCM Task sequence to an Xml file format.
.DESCRIPTION
.PARAMETER SccmServer
Sccm Connection object created with Connect-SccmServer
.PARAMETER PackageID
The task sequence package ID of the task sequence to export.
.PARAMETER Name
The name of task sequence to export.
.PARAMETER Path
The path where the XML file should be saved.
.EXAMPLE
$Connection = Connect-SccmServer “MyServer01”
Export-SccmTaskSequence -SccmServer $connection -name “Windows 7 deployment x64” -Path “shareexportsWindows7deploymentx64.xml”
.NOTES
Author: Stéphane van Gulick
version: 1.0
History:
.LINK
www.PowerShellDistrict.com
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipeline=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$false, HelpMessage=“Gets task sequence according to name.”)][String] $Name = $null,
[Parameter(Mandatory=$false, HelpMessage=“Gets task sequence according to PAckageID.”)][String] $PackageID = $null,
[Parameter(Mandatory=$false, HelpMessage=“File with XML extension.”)][String] $Path = $null
)
PROCESS {
If ($name){
$TaskSequenceXML = (Get-SCCMTaskSequencePackage -SccmServer $sccmServer -Name $Name -Full).sequence
}
elseif($PackageID){
$TaskSequenceXML = (Get-SCCMTaskSequencePackage -SccmServer $sccmServer -Name $Name -Full).sequence
}
if ($path){
$TaskSequenceXML | Out-File $Path
}
else{
Return $TaskSequenceXML
}
}
end{
}
}
Function Get-SCCMTaskSequencePackage {
<#
.SYNOPSIS
Gets a SCCM Task sequence package.
.DESCRIPTION
.PARAMETER SccmServer
Sccm Connection object created with Connect-SccmServer
.PARAMETER PackageID
Get the TS package according to his Task sequence package ID.
.PARAMETER Name
Get the TS package according to his Task sequence name.
.PARAMETER Path
The path where the XML file should be saved.
.EXAMPLE
$Connection = Connect-SccmServer “MyServer01”
Get-SCCMTaskSequencePackage -SccmServer $connection -name “Windows 7 deployment x64”
.NOTES
Author: Stéphane van Gulick
version: 1.0
History:
.LINK
www.PowerShellDistrict.com
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipeline=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$false, HelpMessage=“Optional Filter on query”)][String] $Name = $null,
[Parameter(Mandatory=$false, HelpMessage=“Optional Filter on query”)][String] $PackageID = $null,
[Parameter(Mandatory=$false, HelpMessage=“Optional Filter on query”)][String] $Filter = $null,
[Parameter(Mandatory=$false, HelpMessage=“Full”)][Switch] $Full
)
PROCESS {
If ($name){
$res = Get-SCCMObject -sccmServer $SccmServer -class “SMS_TaskSequencePackage” -Filter “Name=’$Name'”
}elseif($PackageID){
$res = Get-SCCMObject -sccmServer $SccmServer -class “SMS_TaskSequencePackage” -Filter “PackageID=’$PackageID'”
}else{
$res = Get-SCCMObject -sccmServer $SccmServer -class “SMS_TaskSequencePackage” -Filter $Filter
}
if ($Full){
$res.get()
}
}
end{
return $res
}
}
Function Get-SCCMTaskSequence {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage=“SCCM Server”,ValueFromPipeline=$true)][Alias(“Server”,“SmsServer”)][System.Object] $SccmServer,
[Parameter(Mandatory=$false, HelpMessage=“Optional Filter on query”)][String] $Filter = $null
)
PROCESS {
return Get-SCCMObject -sccmServer $SccmServer -class “SMS_TaskSequence” -Filter $Filter
}
}
#endregion
|
Leave A Comment