Author Posts

July 12, 2017 at 2:09 pm

I'm trying to automate the creation of SCCM Device Collections.
Now i made the following Script to do it. if i start the Script a GUI appears. if i choice $RadioButton1 everything works fine and at the End the Collection is moved after creation. So thats fine. But if i choice $RadioButton2 i get the message
– Move-CMObject : FolderPath S01:\DeviceCollection\ESD\TRUST is invalid
I Tryed to Use the same Path like on $RadioButton1 but the same error.

If i manualy use the Collection ID like – Move-CMObject -FolderPath S01:\DeviceCollection\ESD\Test -ObjectId SMS00032 – it works.
do you have an idea why it works so strange?
Many thanks for your help.

Here the Script:

#Setting Up Variables
$PBOuPath = "OU=Test,OU=SCCM Groups,OU=Groups,OU=AW,DC=Tst,DC=wrk"
$PAOuPath = "OU=TRUST,OU=SCCM Groups,OU=Groups,OU=AW,DC=Tst,DC=wrk"
$SecurityGroupQuery = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SecurityGroupName = 'AW\\AW_GG_SCCM_$CollectionName – INSTALL' "
$SecurityGroupQueryUninst = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SecurityGroupName = 'AW\\AW_GG_SCCM_$CollectionName – UNINSTALL' "

# A function to create the form
function SCCM_Form{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Set the size and titel of your form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 500
$Form.height = 300
$Form.Text = ”Create a New SCCM Collection"

# Set the font of the text to be used within the form
$Font = New-Object System.Drawing.Font("Calibri",12)
$Form.Font = $Font

# Create a group that will contain your radio buttons
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '40,30'
$MyGroupBox.size = '400,150'
$MyGroupBox.text = "SCCM Collection"

# Create the collection of radio buttons
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,40'
$RadioButton1.size = '350,20'
$RadioButton1.Checked = $true
$RadioButton1.Text = "New PB Collection"

$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,70'
$RadioButton2.size = '350,20'
$RadioButton2.Checked = $false
$RadioButton2.Text = "New PA Collection"

$RadioButton3 = New-Object System.Windows.Forms.RadioButton
$RadioButton3.Location = '20,100'
$RadioButton3.size = '350,20'
$RadioButton3.Checked = $false
$RadioButton3.Text = "New MS Collection"

# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '130,200'
$OKButton.Size = '100,40'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK

#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '255,200'
$CancelButton.Size = '100,40'
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel

# Add all the Form controls on one line
$form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))

# Add all the GroupBox controls on one line
$MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2,$RadioButton3))

# Assign the Accept and Cancel options in the form to the corresponding buttons
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton

# Activate the form
$form.Add_Shown({$form.Activate()})

# Get the results from the button click
$dialogResult = $form.ShowDialog()

# If the OK button is selected
if ($dialogResult -eq "OK"){

# Check the current state of each radio button and respond accordingly
if ($RadioButton1.Checked){

##Start Funktion for PB
##Eingabefeld für dien Applikationsname
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$CollectionName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the Name of The Application", "New Collection", "")

#Create / Move Collection Test
New-CMCollection -CollectionType Device -Name "$CollectionName – INSTALL" -LimitingCollectionName "All Systems"
$InstallCollectionID = (Get-CMCollection -Name "$CollectionName – INSTALL" | Select-Object CollectionID).CollectionID

New-CMCollection -CollectionType Device -Name "$CollectionName – UNINSTALL" -LimitingCollectionName "All Systems"
$UninstallCollectionID = (Get-CMCollection -Name "$CollectionName – UNINSTALL" | Select-Object CollectionID).CollectionID

Move-CMObject -FolderPath S01:\DeviceCollection\ESD\Test -ObjectId $InstallCollectionID
Move-CMObject -FolderPath S01:\DeviceCollection\ESD\Test -ObjectId $UninstallCollectionID

#Create / Move AD Group Test
New-ADGroup -Path $TstOuPath -Name "AW_GG_SCCM_$CollectionName – INSTALL" -GroupScope Global
New-ADGroup -Path $TstOuPath -Name "AW_GG_SCCM_$CollectionName – UNINSTALL" -GroupScope Global

#Add Query to Collection Install
Add-CMDeviceCollectionQueryMembershipRule -CollectionId $InstallCollectionID -RuleName SCCM_Query -QueryExpression $SecurityGroupQuery
#Add Group Exclude Query to Collection Install
Add-CMDeviceCollectionExcludeMembershipRule -CollectionId $InstallCollectionID -excludeCollectionId $UninstallCollectionID

#Add Query to Collection Uninstall
Add-CMDeviceCollectionQueryMembershipRule -CollectionId $UninstallCollectionID -RuleName SCCM_Query -QueryExpression $SecurityGroupQueryUninst

}

##Stop Function for PB

elseif ($RadioButton2.Checked)
{

#Connect to SCCM Server Import the module
If (-not (Get-Module ConfigurationManager))
{
Write-Host “Configuration Manager not loaded, loading…”
import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
$PSD = Get-PSDrive -PSProvider CMSite
$SiteCode = $PSD.Name
CD “$($PSD):”
}

ELSE
{
Write-Host “Configuration Manager Already Loaded”
}

##Eingabefeld für dien Applikationsname
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$CollectionName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the Name of The Application", "New Collection", "")

#Connect to SCCM Server Drive
New-PSDrive -Name „S01“ -PSProvider CMSite -Root „AWSRV20.art.wrk“

#Create / Move Collection Test
New-CMCollection -CollectionType Device -Name "$CollectionName – INSTALL" -LimitingCollectionName "All Systems"
$InstallCollectionID = (Get-CMCollection -Name "$CollectionName – INSTALL" | Select-Object CollectionID).CollectionID

### New-CMCollection -CollectionType Device -Name "$CollectionName – UNINSTALL" -LimitingCollectionName "All Systems"
### $UninstallCollectionID = (Get-CMCollection -Name "$CollectionName – UNINSTALL" | Select-Object CollectionID).CollectionID

Move-CMObject -FolderPath S01:\DeviceCollection\ESD\Test -ObjectId $InstallCollectionID
###Move-CMObject -FolderPath "S01:\Device Collections\TRUST\ESD" -ObjectId $UninstallCollectionID

#Create / Move AD Group Test
### New-ADGroup -Path $PAOuPath -Name "AW_GG_SCCM_$CollectionName – INSTALL" -GroupScope Global
### New-ADGroup -Path $PAOuPath -Name "AW_GG_SCCM_$CollectionName – UNINSTALL" -GroupScope Global

#Add Query to Collection Install
### Add-CMDeviceCollectionQueryMembershipRule -CollectionId $InstallCollectionID -RuleName SCCM_Query -QueryExpression $SecurityGroupQuery
#Add Group Exclude Query to Collection Install
### Add-CMDeviceCollectionExcludeMembershipRule -CollectionId $InstallCollectionID -excludeCollectionId $UninstallCollectionID

#Add Query to Collection Uninstall
### Add-CMDeviceCollectionQueryMembershipRule -CollectionId $UninstallCollectionID -RuleName SCCM_Query -QueryExpression $SecurityGroupQueryUninst

}

elseif ($RadioButton2.Checked)
{

write-Host "Test"
}

}
}

# Call the function
SCCM_Form

August 16, 2017 at 3:45 pm

You have a typo in this line:

New-PSDrive -Name „S01“ -PSProvider CMSite -Root „AWSRV20.art.wrk“

Should be:

New-PSDrive -Name "S01" -PSProvider CMSite -Root "AWSRV20.art.wrk"