sccm make sccm-package, program-and-queries with xml file

by medodk at 2012-12-07 04:21:20

In em new in power shell.
I use my time to make function New-SCCMPackage TO WORK ,later NEW-SCCMProgram,NEW-SCCMDP,New-SCCM Qurey.
I got inspiration from here
http://www.jbmurphy.com/2012/05/10/powe … d-queries/

but only works sccm the 2007th
I have sccm 2012 sp1

and from here
http://www.stefanringler.com/


I put all function here.
Function New-SCCMPackage {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage="SCCM Server")][Alias("Server","SmsServer")][System.Object] $SccmServer,
[Parameter(Mandatory=$true, HelpMessage="PackageName", ValueFromPipeline=$true)][String] $Name,
[Parameter(Mandatory=$false, HelpMessage="PackageManufacturer")][String] $Manufacturer = "",
[Parameter(Mandatory=$false, HelpMessage="PackageVersion")][String] $Version = "",
[Parameter(Mandatory=$false, HelpMessage="PackageLanguage")][String] $Language = "",
[Parameter(Mandatory=$false, HelpMessage="PackageComment")][String] $Description = "",
[Parameter(Mandatory=$false, HelpMessage="PackageType")][String] $PackageType = "",
[Parameter(Mandatory=$false, HelpMessage="PkgSourceFlag")][String] $PkgSourceFlag = "",
[Parameter(Mandatory=$false, HelpMessage="PackageSourcePath")][String] $PkgSourcePath = "",
[Parameter(Mandatory=$false, HelpMessage="Priority")][String] $Priority = ""
)
THIS IS FROM SCCM 2007
PROCESS {
$packageClass = [WMICLASS]"\$($SccmServer.Machine)$($SccmServer.Namespace):SMS_Package"
$newPackage = $packageClass.createInstance()

$newPackage.Name = $Name
if ($Version -ne "") { $newPackage.Version = $Version }
if ($Manufacturer -ne "") { $newPackage.Manufacturer = $Manufacturer }
if ($Language -ne "") { $newPackage.Language = $Language }
if ($Description -ne "") { $newPackage.Description = $Description }

if ($PkgSourcePath -ne "") {
$newPackage.PkgSourceFlag = 2 # Direct (3 = Compressed)
$newPackage.PkgSourcePath = $PkgSourcePath
if ($PkgShareName -ne "") {
}
} else {
$newPackage.PkgSourceFlag = 1 # No source
$newPackage.PkgSourcePath = $null
}
$newPackage.Put()

$newPackage.Get()
Write-Verbose "Return the new package with ID $($newPackage.PackageID)"
return $newPackage


[/powershell]


ForEach($Node in $XmlFile.GetElementsByTagName("New-SCCMPackage"))
{
$PackageName = $Node.Name
$PackageManufacturer = $Node.Manufacturer
$PackageVersion = $Node.Version
$PackageLanguage = $Node.Language
$PackageComment = $Node.Description
$PackageType= $Node.PackageType
$PkgSourceFlag= $Node.PkgSourceFlag
$PkgSourcePath = $Node.PkgSourcePath
$Priority = $Node.Priority

New-SCCMPackage $PackageName $PackageManufacturer $PackageVersion $PackageLanguage $PackageComment $PackageType $PkgSourceFlag $PkgSourcePath $Priority
Start-Sleep 2
}
[/powershell]

XML input file like this:
Implement,PackageName,PackageManufacturer,PackageVersion,PackageLanguage,PackageComment,PackageSourcePath,PackageFolder
Y,FireFox,Mozilla,17.0,Danish,New FireFox,\sccm13\deploy\appl\Moxila\Firefox 17,<DONT FILL IN ID BUT PACKAGEFOLDER NAME>[/b]
by DonJ at 2012-12-08 08:51:51
So, if you use the CODE or POWERSHELL buttons in the toolbar, it’ll preserve your script formatting and make it a lot easier to follow here :).

That’s a very long script, and I’m not sure what your question is. I’m not able to simply run that and tell you what’s wrong with it - I can’t run this in my own System Center environment, sorry. If you’re getting an error message that you’d like help with, just post that here. But unfortunately, I can’t really just read through a few hundred lines of code and spot the problem for you :).
by medodk at 2012-12-09 02:29:13
My question is I would like use tihis script to create ,package,program ,and queries
I got inspiration from here .
http://www.jbmurphy.com/2012/05/10/powe … d-queries/
and from this script

I try to build script but I em not shure what to use i my CM12Functions.ps1 script em I using sccm 2012 sp1



Function New-SCCMPackage {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage="SCCM Server")][Alias("Server","SmsServer")][System.Object] $SccmServer,
[Parameter(Mandatory=$true, HelpMessage="Package Name", ValueFromPipeline=$true)][String] $Name,

[Parameter(Mandatory=$false, HelpMessage="Package Version")][String] $Version = "",
[Parameter(Mandatory=$false, HelpMessage="Package Manufacturer")][String] $Manufacturer = "",
[Parameter(Mandatory=$false, HelpMessage="Package Language")][String] $Language = "",
[Parameter(Mandatory=$false, HelpMessage="Package Description")][String] $Description = "",
[Parameter(Mandatory=$false, HelpMessage="Package Data Source Path")][String] $PkgSourcePath = "",
[Parameter(Mandatory=$false, HelpMessage="Package Sharename")][String] $PkgShareName = ""
)

PROCESS {
$packageClass = [WMICLASS]"\$($SccmServer.Machine)$($SccmServer.Namespace):SMS_Package"
$newPackage = $packageClass.createInstance()

$newPackage.Name = $Name
if ($Version -ne "") { $newPackage.Version = $Version }
if ($Manufacturer -ne "") { $newPackage.Manufacturer = $Manufacturer }
if ($Language -ne "") { $newPackage.Language = $Language }
if ($Description -ne "") { $newPackage.Description = $Description }

if ($PkgSourcePath -ne "") {
$newPackage.PkgSourceFlag = 2 # Direct (3 = Compressed)
$newPackage.PkgSourcePath = $PkgSourcePath
if ($PkgShareName -ne "") {
$newPackage.ShareName = $PkgShareName
$newPackage.ShareType = 2
}
} else {
$newPackage.PkgSourceFlag = 1 # No source
$newPackage.PkgSourcePath = $null
}
$newPackage.Put()

$newPackage.Get()
Write-Verbose "Return the new package with ID $($newPackage.PackageID)"
return $newPackage
}
}




$CM12XmlFile = "FolderAndCollection_Software_Updates.xml"
$CM12ModuleFile = "CM12Functions.ps1"

$ComputerInfo = Get-WmiObject Win32_ComputerSystem
$ComputerFQDN = $ComputerInfo.Name + '.' + $ComputerInfo.Domain
$CM12ProviderLocation = Get-WmiObject -Query "Select * From SMS_ProviderLocation where ProviderForLocalSite = True" -Namespace "root\sms" -computername $ComputerFQDN
$SiteCode = $CM12ProviderLocation.SiteCode
$NamespacePath = $CM12ProviderLocation.NamespacePath -split "\", 4
$Namespace = $NamespacePath[3]

$CurrentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
If($CurrentPath.Substring($CurrentPath.Length-1) -ne '&#39:wink:
{
$CurrentPath += '&#39;
}

If(!(Test-Path -Path ($CurrentPath + $CM12ModuleFile)))
{
Write-Host "File $CM12ModuleFile does not exist in the current folder" -ForegroundColor Red
Exit
}
else
{
Import-Module -name ($CurrentPath + $CM12ModuleFile)
}

If(!(Test-Path -Path ($CurrentPath + $CM12XmlFile)))
{
Write-Host "File $CM12XmlFile does not exist in the current folder" -ForegroundColor Red
Exit
}

$LogFilePath = $CurrentPath + "_Logs&quot; + ($MyInvocation.MyCommand.Name -replace '.ps1', '') + ".log"
$LogFileComponent = ($MyInvocation.MyCommand.Name -replace '.ps1', '')

New-Item ($CurrentPath + '_Logs') -type directory -force | out-null
try
{
New-Item ($CurrentPath + '_Logs\Test.log') -type file -force | out-null
Remove-Item ($CurrentPath + '_Logs\Test.log') -force | out-null
}
catch
{
Write-Host "Unable to read/write file on $CurrentPath folder" -ForegroundColor Red
Exit
}


[XML]$XmlFile = Get-Content ($CurrentPath + $CM12XmlFile)
Write-Log -Message "Get and read XML file $CM12XmlFile" -severity 1

ForEach($Node in $XmlFile.GetElementsByTagName("Folder"))
{
$Name = $Node.Name
$FolderType = $Node.FolderType
$ParentFolder = $Node.ParentFolder

New-Folder $Name $FolderType $ParentFolder
Start-Sleep 2
}


ForEach($Node in $XmlFile.GetElementsByTagName("Collection"))
{
$Name = $Node.Name
$CollectionType = $Node.CollectionType
$LimitToCollectionID = $Node.LimitToCollectionID
$ParentFolder = $Node.ParentFolder
$Comment = $Node.Comment

$CollExist = Get-CollectionID $Name $CollectionType
If($CollExist -eq $null )
{
$CreateCollection = New-Collection $Name $CollectionType $Comment $LimitToCollectionID
$CollectionFolder = Get-CollectionFolderID $CreateCollection "5000"
Start-Sleep 2
Move-CollectionToFolder $CreateCollection "0" $ParentFolder "5000"

if ($node.HasChildNodes)
{
foreach ($Child in $node.ChildNodes)
{
if($Child.get_Name() -eq "Rule")
{
$RuleName = $Child.Name
$RuleRefreshDays = $Child.RefreshDays
$RuleStartTime = $Child.StartTime
$RuleText = $Child."#text"
Add-CollectionRuleQuery $CreateCollection $RuleRefreshDays $RuleName $RuleText
Start-Sleep 2
}

if($Child.get_Name() -eq "Variables")
{
if ($Child.HasAttributes)
{
foreach ($Att in $Child.Attributes)
{
$VariableName = $Att.Name
$VariableValue = $Att.Value
$Mask = "false"
Add-CollectionVariable $CreateCollection $VariableName $VariableValue $Mask
Start-Sleep 2
}
}
}

}
}
}
}


In function I would like Add Function New-SCCMPackage

Function New-SCCMPackage {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)][String] $Name,
[Parameter(Mandatory=$true)][String] $Manufacturer,
[Parameter(Mandatory=$true)][String] $Version,
[Parameter(Mandatory=$true)][String] $Language,
[Parameter(Mandatory=$true)][String] $Description,
[Parameter(Mandatory=$false)][String] $PackageType,
[Parameter(Mandatory=$false)][UInt32] $PkgSourceFlag,
[Parameter(Mandatory=$true)][String] $PkgSourcePath,
[Parameter(Mandatory=$true)][UInt32] $Priority
)



And here I will add

[powershell
$CM12XmlFile = "FolderAndCollection_Software_Updates.xml"
$CM12ModuleFile = "CM12Functions.ps1"

$ComputerInfo = Get-WmiObject Win32_ComputerSystem
$ComputerFQDN = $ComputerInfo.Name + '.' + $ComputerInfo.Domain
$CM12ProviderLocation = Get-WmiObject -Query "Select * From SMS_ProviderLocation where ProviderForLocalSite = True" -Namespace "root\sms" -computername $ComputerFQDN
$SiteCode = $CM12ProviderLocation.SiteCode
$NamespacePath = $CM12ProviderLocation.NamespacePath -split "\", 4
$Namespace = $NamespacePath[3]

$CurrentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
If($CurrentPath.Substring($CurrentPath.Length-1) -ne '&#39:wink:
{
$CurrentPath += '&#39;
}

If(!(Test-Path -Path ($CurrentPath + $CM12ModuleFile)))
{
Write-Host "File $CM12ModuleFile does not exist in the current folder" -ForegroundColor Red
Exit
}
else
{
Import-Module -name ($CurrentPath + $CM12ModuleFile)
}

If(!(Test-Path -Path ($CurrentPath + $CM12XmlFile)))
{
Write-Host "File $CM12XmlFile does not exist in the current folder" -ForegroundColor Red
Exit
}

$LogFilePath = $CurrentPath + "_Logs&quot; + ($MyInvocation.MyCommand.Name -replace '.ps1', '') + ".log"
$LogFileComponent = ($MyInvocation.MyCommand.Name -replace '.ps1', '')

New-Item ($CurrentPath + '_Logs') -type directory -force | out-null
try
{
New-Item ($CurrentPath + '_Logs\Test.log') -type file -force | out-null
Remove-Item ($CurrentPath + '_Logs\Test.log') -force | out-null
}
catch
{
Write-Host "Unable to read/write file on $CurrentPath folder" -ForegroundColor Red
Exit
}


[XML]$XmlFile = Get-Content ($CurrentPath + $CM12XmlFile)
Write-Log -Message "Get and read XML file $CM12XmlFile" -severity 1

ForEach($Node in $XmlFile.GetElementsByTagName("Folder"))
{
$Name = $Node.Name
$FolderType = $Node.FolderType
$ParentFolder = $Node.ParentFolder

New-Folder $Name $FolderType $ParentFolder
Start-Sleep 2
}


ForEach($Node in $XmlFile.GetElementsByTagName("Collection"))
{
$Name = $Node.Name
$CollectionType = $Node.CollectionType
$LimitToCollectionID = $Node.LimitToCollectionID
$ParentFolder = $Node.ParentFolder
$Comment = $Node.Comment

$CollExist = Get-CollectionID $Name $CollectionType
If($CollExist -eq $null )
{
$CreateCollection = New-Collection $Name $CollectionType $Comment $LimitToCollectionID
$CollectionFolder = Get-CollectionFolderID $CreateCollection "5000"
Start-Sleep 2
Move-CollectionToFolder $CreateCollection "0" $ParentFolder "5000"

if ($node.HasChildNodes)
{
foreach ($Child in $node.ChildNodes)
{
if($Child.get_Name() -eq "Rule")
{
$RuleName = $Child.Name
$RuleRefreshDays = $Child.RefreshDays
$RuleStartTime = $Child.StartTime
$RuleText = $Child."#text"
Add-CollectionRuleQuery $CreateCollection $RuleRefreshDays $RuleName $RuleText
Start-Sleep 2
}

if($Child.get_Name() -eq "Variables")
{
if ($Child.HasAttributes)
{
foreach ($Att in $Child.Attributes)
{
$VariableName = $Att.Name
$VariableValue = $Att.Value
$Mask = "false"
Add-CollectionVariable $CreateCollection $VariableName $VariableValue $Mask
Start-Sleep 2
}
}
}

}
}
}
}

]



ForEach($Node in $XmlFile.GetElementsByTagName("New-SCCMPackage"))
{
$PkgName = $Node.Name
$PkgManufacturer = $Node.Manufacturer
$PkgVersion = $Node.Version
$Language = $Node.Language
$Description= $Node.Description
$PackageType= $Node.PackageType
$PkgSourceFlag= $Node.PkgSourceFlag
$PkgSourcePath = $Node.PkgSourcePath
$Priority = $Node.Priority

New-SCCMPackage $PkgName $PkgManufacturer $PkgVersion $Language $Description $PackageType $PkgSourceFlag $PkgSourcePath $Priority
Start-Sleep 2
[/powershell]
by medodk at 2012-12-09 02:42:27
I got inspiration from
http://www.jbmurphy.com/2012/05/10/powe … d-queries/
to make package,program and queries in this script
I started wirh create package to put in my script.



$CM12XmlFile = "FolderAndCollection_Software_Updates.xml"
$CM12ModuleFile = "CM12Functions.ps1"

$ComputerInfo = Get-WmiObject Win32_ComputerSystem
$ComputerFQDN = $ComputerInfo.Name + '.' + $ComputerInfo.Domain
$CM12ProviderLocation = Get-WmiObject -Query "Select * From SMS_ProviderLocation where ProviderForLocalSite = True" -Namespace "root\sms" -computername $ComputerFQDN
$SiteCode = $CM12ProviderLocation.SiteCode
$NamespacePath = $CM12ProviderLocation.NamespacePath -split "\", 4
$Namespace = $NamespacePath[3]

$CurrentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
If($CurrentPath.Substring($CurrentPath.Length-1) -ne '&#39:wink:
{
$CurrentPath += '&#39;
}

If(!(Test-Path -Path ($CurrentPath + $CM12ModuleFile)))
{
Write-Host "File $CM12ModuleFile does not exist in the current folder" -ForegroundColor Red
Exit
}
else
{
Import-Module -name ($CurrentPath + $CM12ModuleFile)
}

If(!(Test-Path -Path ($CurrentPath + $CM12XmlFile)))
{
Write-Host "File $CM12XmlFile does not exist in the current folder" -ForegroundColor Red
Exit
}

$LogFilePath = $CurrentPath + "_Logs&quot; + ($MyInvocation.MyCommand.Name -replace '.ps1', '') + ".log"
$LogFileComponent = ($MyInvocation.MyCommand.Name -replace '.ps1', '')

New-Item ($CurrentPath + '_Logs') -type directory -force | out-null
try
{
New-Item ($CurrentPath + '_Logs\Test.log') -type file -force | out-null
Remove-Item ($CurrentPath + '_Logs\Test.log') -force | out-null
}
catch
{
Write-Host "Unable to read/write file on $CurrentPath folder" -ForegroundColor Red
Exit
}


[XML]$XmlFile = Get-Content ($CurrentPath + $CM12XmlFile)
Write-Log -Message "Get and read XML file $CM12XmlFile" -severity 1

ForEach($Node in $XmlFile.GetElementsByTagName("Folder"))
{
$Name = $Node.Name
$FolderType = $Node.FolderType
$ParentFolder = $Node.ParentFolder

New-Folder $Name $FolderType $ParentFolder
Start-Sleep 2
}


ForEach($Node in $XmlFile.GetElementsByTagName("Collection"))
{
$Name = $Node.Name
$CollectionType = $Node.CollectionType
$LimitToCollectionID = $Node.LimitToCollectionID
$ParentFolder = $Node.ParentFolder
$Comment = $Node.Comment

$CollExist = Get-CollectionID $Name $CollectionType
If($CollExist -eq $null )
{
$CreateCollection = New-Collection $Name $CollectionType $Comment $LimitToCollectionID
$CollectionFolder = Get-CollectionFolderID $CreateCollection "5000"
Start-Sleep 2
Move-CollectionToFolder $CreateCollection "0" $ParentFolder "5000"

if ($node.HasChildNodes)
{
foreach ($Child in $node.ChildNodes)
{
if($Child.get_Name() -eq "Rule")
{
$RuleName = $Child.Name
$RuleRefreshDays = $Child.RefreshDays
$RuleStartTime = $Child.StartTime
$RuleText = $Child."#text"
Add-CollectionRuleQuery $CreateCollection $RuleRefreshDays $RuleName $RuleText
Start-Sleep 2
}

if($Child.get_Name() -eq "Variables")
{
if ($Child.HasAttributes)
{
foreach ($Att in $Child.Attributes)
{
$VariableName = $Att.Name
$VariableValue = $Att.Value
$Mask = "false"
Add-CollectionVariable $CreateCollection $VariableName $VariableValue $Mask
Start-Sleep 2
}
}
}

}
}
}
}


In function I would like Add Function New-SCCMPackage
Function New-SCCMPackage {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)][String] $Name,
[Parameter(Mandatory=$true)][String] $Manufacturer,
[Parameter(Mandatory=$true)][String] $Version,
[Parameter(Mandatory=$true)][String] $Language,
[Parameter(Mandatory=$true)][String] $Description,
[Parameter(Mandatory=$false)][String] $PackageType,
[Parameter(Mandatory=$false)][UInt32] $PkgSourceFlag,
[Parameter(Mandatory=$true)][String] $PkgSourcePath,
[Parameter(Mandatory=$true)][UInt32] $Priority
)



And here I will add


$CM12XmlFile = "FolderAndCollection_Software_Updates.xml"
$CM12ModuleFile = "CM12Functions.ps1"

$ComputerInfo = Get-WmiObject Win32_ComputerSystem
$ComputerFQDN = $ComputerInfo.Name + '.' + $ComputerInfo.Domain
$CM12ProviderLocation = Get-WmiObject -Query "Select * From SMS_ProviderLocation where ProviderForLocalSite = True" -Namespace "root\sms" -computername $ComputerFQDN
$SiteCode = $CM12ProviderLocation.SiteCode
$NamespacePath = $CM12ProviderLocation.NamespacePath -split "\", 4
$Namespace = $NamespacePath[3]

$CurrentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
If($CurrentPath.Substring($CurrentPath.Length-1) -ne '&#39:wink:
{
$CurrentPath += '&#39;
}

If(!(Test-Path -Path ($CurrentPath + $CM12ModuleFile)))
{
Write-Host "File $CM12ModuleFile does not exist in the current folder" -ForegroundColor Red
Exit
}
else
{
Import-Module -name ($CurrentPath + $CM12ModuleFile)
}

If(!(Test-Path -Path ($CurrentPath + $CM12XmlFile)))
{
Write-Host "File $CM12XmlFile does not exist in the current folder" -ForegroundColor Red
Exit
}

$LogFilePath = $CurrentPath + "_Logs&quot; + ($MyInvocation.MyCommand.Name -replace '.ps1', '') + ".log"
$LogFileComponent = ($MyInvocation.MyCommand.Name -replace '.ps1', '')

New-Item ($CurrentPath + '_Logs') -type directory -force | out-null
try
{
New-Item ($CurrentPath + '_Logs\Test.log') -type file -force | out-null
Remove-Item ($CurrentPath + '_Logs\Test.log') -force | out-null
}
catch
{
Write-Host "Unable to read/write file on $CurrentPath folder" -ForegroundColor Red
Exit
}


[XML]$XmlFile = Get-Content ($CurrentPath + $CM12XmlFile)
Write-Log -Message "Get and read XML file $CM12XmlFile" -severity 1

ForEach($Node in $XmlFile.GetElementsByTagName("Folder"))
{
$Name = $Node.Name
$FolderType = $Node.FolderType
$ParentFolder = $Node.ParentFolder

New-Folder $Name $FolderType $ParentFolder
Start-Sleep 2
}


ForEach($Node in $XmlFile.GetElementsByTagName("Collection"))
{
$Name = $Node.Name
$CollectionType = $Node.CollectionType
$LimitToCollectionID = $Node.LimitToCollectionID
$ParentFolder = $Node.ParentFolder
$Comment = $Node.Comment

$CollExist = Get-CollectionID $Name $CollectionType
If($CollExist -eq $null )
{
$CreateCollection = New-Collection $Name $CollectionType $Comment $LimitToCollectionID
$CollectionFolder = Get-CollectionFolderID $CreateCollection "5000"
Start-Sleep 2
Move-CollectionToFolder $CreateCollection "0" $ParentFolder "5000"

if ($node.HasChildNodes)
{
foreach ($Child in $node.ChildNodes)
{
if($Child.get_Name() -eq "Rule")
{
$RuleName = $Child.Name
$RuleRefreshDays = $Child.RefreshDays
$RuleStartTime = $Child.StartTime
$RuleText = $Child."#text"
Add-CollectionRuleQuery $CreateCollection $RuleRefreshDays $RuleName $RuleText
Start-Sleep 2
}

if($Child.get_Name() -eq "Variables")
{
if ($Child.HasAttributes)
{
foreach ($Att in $Child.Attributes)
{
$VariableName = $Att.Name
$VariableValue = $Att.Value
$Mask = "false"
Add-CollectionVariable $CreateCollection $VariableName $VariableValue $Mask
Start-Sleep 2
}
}
}

}
}
}
}



ForEach($Node in $XmlFile.GetElementsByTagName("New-SCCMPackage"))
{
$PkgName = $Node.Name
$PkgManufacturer = $Node.Manufacturer
$PkgVersion = $Node.Version
$Language = $Node.Language
$Description= $Node.Description
$PackageType= $Node.PackageType
$PkgSourceFlag= $Node.PkgSourceFlag
$PkgSourcePath = $Node.PkgSourcePath
$Priority = $Node.Priority

New-SCCMPackage $PkgName $PkgManufacturer $PkgVersion $Language $Description $PackageType $PkgSourceFlag $PkgSourcePath $Priority
Start-Sleep 2


I tryed to bild from this function

Function New-SCCMPackage {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true, HelpMessage="SCCM Server")][Alias("Server","SmsServer")][System.Object] $SccmServer,
[Parameter(Mandatory=$true, HelpMessage="Package Name", ValueFromPipeline=$true)][String] $Name,

[Parameter(Mandatory=$false, HelpMessage="Package Version")][String] $Version = "",
[Parameter(Mandatory=$false, HelpMessage="Package Manufacturer")][String] $Manufacturer = "",
[Parameter(Mandatory=$false, HelpMessage="Package Language")][String] $Language = "",
[Parameter(Mandatory=$false, HelpMessage="Package Description")][String] $Description = "",
[Parameter(Mandatory=$false, HelpMessage="Package Data Source Path")][String] $PkgSourcePath = "",
[Parameter(Mandatory=$false, HelpMessage="Package Sharename")][String] $PkgShareName = ""
)

PROCESS {
$packageClass = [WMICLASS]"\$($SccmServer.Machine)$($SccmServer.Namespace):SMS_Package"
$newPackage = $packageClass.createInstance()

$newPackage.Name = $Name
if ($Version -ne "") { $newPackage.Version = $Version }
if ($Manufacturer -ne "") { $newPackage.Manufacturer = $Manufacturer }
if ($Language -ne "") { $newPackage.Language = $Language }
if ($Description -ne "") { $newPackage.Description = $Description }

if ($PkgSourcePath -ne "") {
$newPackage.PkgSourceFlag = 2 # Direct (3 = Compressed)
$newPackage.PkgSourcePath = $PkgSourcePath
if ($PkgShareName -ne "") {
$newPackage.ShareName = $PkgShareName
$newPackage.ShareType = 2
}
} else {
$newPackage.PkgSourceFlag = 1 # No source
$newPackage.PkgSourcePath = $null
}
$newPackage.Put()

$newPackage.Get()
Write-Verbose "Return the new package with ID $($newPackage.PackageID)"
return $newPackage
}
}