sccm copy a file in a package then run it

sccm copy a file in a package then run it


Table of Contents

sccm copy a file in a package then run it

This guide details how to create an SCCM (System Center Configuration Manager) package that copies a file to a specific location and then executes it. This is a common requirement for deploying applications or scripts that need to be placed in a particular directory before running. We'll cover the process step-by-step, addressing common questions and potential pitfalls.

Understanding the Process

The core concept involves creating a standard SCCM package containing the executable file and a script (e.g., a batch file or PowerShell script) responsible for copying the file and then launching it. This separates the deployment mechanism from the application execution logic, making the process more robust and manageable.

Creating the SCCM Package

  1. Gather Your Files: Locate the executable file you want to deploy and create a simple script to handle the copy and execution. For example, a batch file named deploy.bat could contain the following:

    @echo off
    xcopy "%~dp0MyExecutable.exe" "C:\Program Files\MyApplication" /y /i
    "C:\Program Files\MyApplication\MyExecutable.exe"
    

    This script first copies MyExecutable.exe (assuming it's in the same directory as deploy.bat) to C:\Program Files\MyApplication, overwriting existing files if necessary (/y). The /i switch creates the destination directory if it doesn't exist. Then, it executes the copied executable. Remember to adjust the paths to match your specific application and desired location. For more complex scenarios, consider using PowerShell for enhanced functionality and error handling.

  2. Create the SCCM Package: In the SCCM console, navigate to the Software Library workspace. Create a new package, selecting the appropriate package type (usually "Application"). Provide a descriptive name and choose a suitable source directory containing both MyExecutable.exe and deploy.bat.

  3. Create the Program: Within the package, create a new program. Select "Standard Program" and provide a name. Crucially, under "Start in" specify the path to the directory where deploy.bat resides within the package. Set the "Program" to deploy.bat.

  4. Deployment: Create an advertisement or a task sequence to deploy this package to your target devices. Remember to configure appropriate deployment settings based on your environment.

Troubleshooting and Best Practices

H2: What if my file needs administrator privileges?

Your script (e.g., deploy.bat) should request elevated permissions. You can do this by running the batch file as administrator, either through the SCCM deployment settings or directly within the script itself using commands like runas. For PowerShell, use the Start-Process cmdlet with the -Verb RunAs parameter.

H2: How do I handle potential errors?

Robust error handling is critical. Instead of simple commands, use more sophisticated techniques within your script to check for errors. In batch, utilize errorlevel checks. For PowerShell, use try-catch blocks to capture and handle exceptions. Log errors to a file for later review.

H2: Can I use PowerShell instead of a batch file?

Yes, PowerShell offers more flexibility and error handling. A PowerShell script might look like this:

$SourcePath = "$PSScriptRoot\MyExecutable.exe"
$DestinationPath = "C:\Program Files\MyApplication"

Copy-Item -Path $SourcePath -Destination $DestinationPath -Force
Start-Process -FilePath "$DestinationPath\MyExecutable.exe" -Verb RunAs

Remember to replace placeholders with your actual paths.

H2: What are the security implications?

Always thoroughly test your package in a test environment before deploying to production. Be mindful of the permissions assigned to the script and executable to prevent security vulnerabilities.

By following these steps and considering the best practices, you can effectively deploy applications using SCCM while ensuring the necessary files are correctly copied and executed in the designated location. Remember to always test your deployment thoroughly.