Friday, May 6, 2016

Powershell Script: Copy files from multiple source directories



While you can create a package or application that copies config files out to your clients after an install, there may be times when it's easier to just use a PowerShell script to copy configuration files to your client machines.

This was true in our case where we had to copy files from different source directories depending on the name of the computer.  In our case, we had to copy a different configuration file to C:\Program Files (x86)\application\ depending on which site that computer was at.  Our computers have a four digit site identifier at the beginning so we leveraged this in order to do the file copy.

You can modify the script to accommodate your computer name scheme.

We grabbed a list of computers that had the application from an SCCM report and copied the list of computers into a .csv file where the first column had the header "pcname".


Our config files were in the source directory.  Those folders were named with the site location numbers and each unique config file was in each of those folders.



$list = Import-Csv "C:\TEMP\computers.csv"
foreach ($entry in $list)
{
 try 
    {
    $machinename = $entry.pcname
    $schoolnumber = $machinename.SubString(0,4)
    $dest = "\\"+$machinename + "\c$\Program Files (x86)\appfolder\" #files copied to
    $source = "\\Server\Source\Configfiles\"+$schoolnumber + "\configfile.json"
    $dest
    #$schoolnumber
    $source
    Copy-Item -Path $source -Destination $dest
    }
    catch [Exception]
    {
    Write-Host "Copy Failure " $_
     }
}

After the software deployed, we ran the script and it pulled the configuration file from the appropriate folder in the source directory and copied it to the client program files sub directory.

No comments:

Post a Comment