sccm copy a file in a package then run it

3 min read 24-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 using SCCM (System Center Configuration Manager) often involves distributing files and then executing them. This guide details how to create a package in SCCM that copies a file to a specific location and then runs it. We'll cover the steps, best practices, and troubleshooting tips to ensure a smooth deployment.

Understanding the Process

The core process involves creating a SCCM package containing the file you want to deploy and then using a script (usually a batch file or PowerShell script) within the package to:

  1. Copy the file: Transfer the file from the package source directory to the desired destination on the client machine.
  2. Execute the file: Run the file using the appropriate command.

Creating the SCCM Package

  1. Source Files: Gather all necessary files, including the file you want to copy and the script to execute it (e.g., copy_and_run.bat). Make sure the file permissions are correct.
  2. Create a Package: In the SCCM console, navigate to "Software Library" -> "Application Management" -> "Packages". Click "Create Package".
  3. Package Information: Provide a descriptive name and optionally a comment. Select a source directory containing your files.
  4. Program: This is where the magic happens. You won't add a program directly to install the file, but rather a program to run your script. Click "Add Program."
  5. Program Properties:
    • Name: Give it a descriptive name (e.g., "Copy and Run File").
    • Command line: This is where you specify your script and the parameters needed for copying and running the file. This will depend on the scripting language used.
    • Working directory: This should be the destination directory where the file will be copied. Use absolute paths for reliability.

Example Script (Batch File - copy_and_run.bat):

This batch file copies my_file.exe from the package source directory to C:\Deploy\ and then executes it. Replace placeholders with your actual file paths and names.

@echo off
xcopy "%~dp0my_file.exe" "C:\Deploy\" /Y /I
"C:\Deploy\my_file.exe"
  • %~dp0: This variable represents the path of the batch file within the package.
  • /Y: This suppresses the confirmation prompt during the copy process.
  • /I: This creates the destination directory if it doesn't exist.
  • "C:\Deploy\my_file.exe": The full path to the executable after copying. Ensure this path is correct and accessible.

PowerShell Script Example:

PowerShell offers more robust error handling and flexibility. This script mirrors the batch file's functionality:

# Get the script's directory
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition

# Copy the file
Copy-Item -Path "$ScriptDir\my_file.exe" -Destination "C:\Deploy\" -Force

# Execute the file
& "C:\Deploy\my_file.exe"
  • $ScriptDir: Gets the script's directory.
  • -Force: Overwrites the file if it already exists.
  • &: The call operator, which executes the command.

Adding the Program to the SCCM Package (Continued)

After creating the batch file (or PowerShell script), add it as a program in your SCCM package. In the "Program Properties" command line, specify the path to your script: copy_and_run.bat (or the path to your PowerShell script and powershell.exe).

Deployment and Testing

  1. Create an Advertisement: Create a deployment type for your package, targeting the appropriate computer collections.
  2. Deployment Settings: Choose the appropriate deployment method (e.g., required, available).
  3. Testing: Deploy to a test group of machines to ensure everything works as expected before deploying to a larger audience. Monitor the deployment status in the SCCM console.

Troubleshooting

  • Access Rights: Ensure the account running the script has the necessary permissions to copy files to the destination directory.
  • Path Issues: Double-check all file paths for accuracy. Use absolute paths whenever possible to prevent issues.
  • Error Logging: Incorporate robust error handling in your script to log any issues encountered during the process.
  • SCCM Logs: Examine the SCCM logs for any errors related to the deployment.

Frequently Asked Questions

How can I handle errors in my script?

Error handling is crucial. In batch scripts, you can use if errorlevel to check for errors. In PowerShell, use try-catch blocks. Log errors to a file for easier troubleshooting.

What if the destination directory doesn't exist?

The examples above include options to create the destination directory if it doesn't exist (/I in the batch file, -Force in PowerShell). If using a different method, ensure the directory is created before attempting to copy the file.

Can I use other scripting languages?

Yes, you can use other scripting languages like VBScript, but batch and PowerShell are the most commonly used due to their simplicity and wide availability.

How can I monitor the execution of the file?

You can incorporate logging into your script to track its progress and any errors. You can also monitor the deployment status through the SCCM console.

By following these steps and incorporating robust error handling, you can effectively deploy files and run them using SCCM packages, automating your application deployments and configuration management. Remember to always test thoroughly in a controlled environment before deploying to production.