Hi all,
I’ve written this script to automate adding applications into SCCM. I found it a boring job at the best of times, so i thought lets automate this ! Its not fully completed, need to add some comments and help to it, but i feel that its an average script, no magic in it. This is what i mean about hitting the next level. What could i do better or using less code ? Am i using the right commands for what i want to do ? Any guidance would be great.
usage:
Add-SCCMApplication -ApplicationName "AppName" -Version "1.0" -Publisher "Microsoft" -InstallerDetectionScriptFilePath "Install method provided"
The detection.txt file just contains a registry path to test against.
It all works but want to know if i can make it better and learn at the same time.
function Add-SCCMApplication { [CmdletBinding(SupportsShouldProcess=$True, ConfirmImpact='High')] Param( [Parameter(Mandatory=$false)] [string]$Folder, [Parameter(Mandatory=$false)] [string]$Publisher, [Parameter(Mandatory=$false)] [string]$ApplicationName, [Parameter(Mandatory=$false)] [string]$Version, [Parameter(Mandatory=$false)] [string]$InstallerDetectionScriptFilePath ) begin { #Genric Variables for Application creation #Dynamically get site code $SiteCode = (Get-WmiObject -namespace "root\sms" -class "__Namespace" | Select -ExpandProperty Name).Substring(5,3) #Site Server $SiteServer = $env:COMPUTERNAME #Please Run on Site Server only #Run ConfigMgr module if (!(Test-Path "$(Split-Path $env:SMS_ADMIN_UI_PATH -Parent)\ConfigurationManager.psd1")) { Write-warning -message 'Configuration Manager module not found. Is the admin console intalled?' } elseif (!(Get-Module 'ConfigurationManager')) { Import-Module "$(Split-Path $env:SMS_ADMIN_UI_PATH -Parent)\ConfigurationManager.psd1" } Set-Location "$($SiteCode):" } #end of Begin process { #Check to confirm Application is not already installed if (Get-CMApplication -Name $ApplicationName) { Write-warning -message "The application $ApplicationName already exists."} #Run Folder creation try { if ($Folder -eq $null) { #Set Folder properties $Arguments = @{ Name = "$folder"; ObjectType = 6000; ParentContainerNodeId = 0} #Does folder exist ? $Folderexist = Get-WmiObject -Namespace "root\SMS\site_$Sitecode" -Class 'SMS_ObjectContainerNode' #get class containing folders if ($Folderexist.name.Contains($folder)) { write-warning "Folder $folder already exists" } else { Set-WmiInstance -Namespace "root\SMS\site_$Sitecode" -Class 'SMS_ObjectContainerNode' -Arguments $Arguments -ComputerName $SiteServer -ErrorAction stop | Out-Null Write-host -ForegroundColor Cyan "Applcation folder $folder created successfully" } } else { write-host -ForegroundColor Cyan "Folder creation not requested" } } catch { Write-Warning $_.exception.message } ## #Application install ## Create the application container. This will hold our deployment type $NewCmApplicationParams = @{ 'Name' = $ApplicationName; 'Publisher' = $Publisher; 'SoftwareVersion' = $Version } Set-Location "$($SiteCode):" #create new container try { New-CMApplication @NewCmApplicationParams -ErrorAction stop | Out-Null Write-host -ForegroundColor Cyan "Container created" } catch { Write-Warning -message $_.exception.message } #Set detection for Deployment ##Using here-string to populate Detection type try { if (Test-Path "e:\source\applications\Detection.txt") { $Detection = @" If $(get-content "e:\source\applications\Detection.txt") { Write-Host "Installed" } else { #No Install detected } "@ write-host -ForegroundColor Cyan "detection script created ready for adding deployment type" } else { write-warning -Message "Detection.txt file does not exist, cannot create detection method for application" } } catch { Write-Warning -Message $_.exception.message } ## Create the deployment type $AddCmDeploymentTypeParams = @{ 'ApplicationName' = $ApplicationName; 'ScriptInstaller' = $true; 'DeploymentTypeName' = "Deploy $ApplicationName"; 'InstallationProgram' = $(split-path $InstallerDetectionScriptFilePath -Leaf); 'ContentLocation' = $(Split-Path $InstallerDetectionScriptFilePath -Parent) ; 'InstallationBehaviorType' = 'InstallForSystem'; 'LogonRequirementType' = 'WhetherOrNotUserLoggedOn' 'InstallationProgramVisibility' = 'Hidden'; 'RunScriptAs32bitProcessOn64bitClient' = $true; 'ScriptType' = 'Powershell'; 'ScriptContent' = $Detection } try { Add-CMDeploymentType @AddCmDeploymentTypeParams -ErrorAction stop | out-null write-host -ForegroundColor Cyan "Application now created in SCCM" } catch { Write-Warning -Message $_.exception.message } ## Set reboot behavior because it can't be set with Add-CMDeploymentType $SetCmDeploymentTypeParams = @{ 'RebootBehavior' = 'NoAction'; 'DeploymentTypeName' = "Deploy $ApplicationName"; 'ApplicationName' = $ApplicationName; 'MsiOrScriptInstaller' = $true; } try { Set-CMDeploymentType @SetCmDeploymentTypeParams -ErrorAction Stop | Out-Null write-host -ForegroundColor Cyan "Set reboot behaviour and deployment details" } catch { Write-Warning -Message $_.exception.message } write-host -ForegroundColor Cyan "Application now added to SCCM" } } #End of function
Thank you all.