sccm copy a file in a package then run it

3 min read 23-08-2025
sccm copy a file in a package then run it


Table of Contents

sccm copy a file in a package then run it

Deploying applications and configurations with SCCM (System Center Configuration Manager) often involves packaging files for distribution and then executing specific files within that package. This guide details how to create a package in SCCM that copies files to a designated location and then runs a specific executable. This is crucial for various scenarios, from deploying custom applications to running scripts as part of a larger deployment process.

Understanding the Process

The process involves creating a standard SCCM package. Instead of directly pointing to an executable, you'll include the entire application folder within the package. Then, within the application deployment, you'll utilize a script (often PowerShell) to first copy the files from the package's source directory to the target location, and only then execute the application. This approach offers better control, organization, and flexibility.

Creating the SCCM Package

  1. Gather Your Files: Collect all the necessary files for your application, ensuring you have the main executable and any dependencies. Organize them in a clearly structured folder.

  2. Create the SCCM Package: In the SCCM console, navigate to Software Library > Application Management > Packages. Right-click and select Create Package.

  3. Package Information: Provide a descriptive name and a relevant description for your package. Choose a relevant source directory where your application folder resides. This is crucial: Remember the source path—you'll need it later in your script. You may want to use a relatively short and simple path.

  4. Package Data: Select the appropriate option for the package type. For this purpose, we are keeping it as a Standard Program.

  5. Distribution Points: Assign the package to appropriate distribution points within your SCCM infrastructure to allow clients to download it efficiently.

Creating the Deployment Type and PowerShell Script

This is where the actual file copying and execution takes place.

  1. Create a Deployment Type: After creating the package, create a new deployment type. Within this deployment type, we'll not directly point to an executable but use a PowerShell script.

  2. PowerShell Script: Create a PowerShell script (e.g., copy_and_run.ps1) that performs the following actions:

    • Gets the Source and Destination Paths: The script needs to know where to copy from (the package's source directory) and where to copy to (the target location on the client machine). You can hardcode these, or even better, use environment variables within the SCCM deployment to make it more flexible.

    • Copies the Files: Use Copy-Item to recursively copy the files from the source to the destination. Handle potential errors gracefully, perhaps using try-catch blocks.

    • Executes the Application: After the copy operation is complete, run your application using Start-Process. Specify the full path to the executable within the target directory.

    Example copy_and_run.ps1:

    # Set source and destination paths.  Consider using SCCM environment variables for flexibility.
    $sourcePath = "C:\SCCMPackages\MyApplication" # REPLACE with your package source path
    $destinationPath = "C:\Program Files\MyApplication" # REPLACE with your desired destination path
    
    # Create the destination directory if it doesn't exist
    if (!(Test-Path -Path $destinationPath)){
        New-Item -ItemType Directory -Force -Path $destinationPath
    }
    
    
    try {
        Copy-Item -Path $sourcePath\* -Destination $destinationPath -Recurse -Force
        Write-Host "Files copied successfully."
        Start-Process -FilePath "$destinationPath\MyApp.exe"  # REPLACE with your executable name
    }
    catch {
        Write-Error "Error during file copy or execution: $($_.Exception.Message)"
        Exit 1
    }
    
  3. Deployment Type Configuration: In the SCCM deployment type, specify the following:

    • Program Type: Choose "Script"
    • Script Engine: Select "PowerShell"
    • Script: Browse to and select your copy_and_run.ps1 script.
    • Run As: Specify the appropriate user account (e.g., System, a specific user) with sufficient privileges to execute the script and run the application.
  4. Deploy the Package: Deploy the package to the target collection of devices.

Troubleshooting

  • Access Rights: Ensure the user account running the script has the necessary permissions to write to the target directory.
  • Path Errors: Double-check the source and destination paths in your script. Incorrect paths are the most common cause of failure.
  • Dependencies: Make sure all necessary application dependencies are included in the package.
  • Error Logging: Use more extensive error handling within the PowerShell script to pinpoint specific problems.
  • SCCM Logs: Review the SCCM logs for any errors related to the package deployment or script execution.

This detailed process provides a robust method for deploying applications and executing them after copying them from an SCCM package. Remember to adapt the script and paths to match your specific application and environment. Always thoroughly test your deployments in a test environment before deploying to production.