Question

Powershell gurus, can you assist me with a fun script? :)

There are 5 servers in my company, all with the same directory structure. I have a file, servers.txt, laid out with 2 fields. First, the name of the server, and second, whether it should be deployed to for this run. For example....

MyServer1 Yes
MyServer2 No
MyServer3 No
MyServer4 Yes
MyServer5 Yes

The second file, files.txt, contains the name of the file(s) that will be deployed to the "Yes" servers, copied to the appropriate dir on the server, dirs that are off of the "root". (The root for the servers are \SERVER_NAME\Projects\MyProject) Example for files.txt

file1.aspx
bin\myproject.dll
css\style1.css
secure\file2.aspx
secure\file3.aspx

So, for example, bin\myproject.dll would be copied to \MyServer1\Projects\MyProject\bin\myproject.dll, but only for servers that are marked "Yes" in servers.txt.

I'm trying to write a script to do this deployment, reading both files and doing the copies. Any powershell gurus that can assist? I would appreciate it so much!

Was it helpful?

Solution

This is untested, but see if it works. It assumes that files.txt, servers.txt, and all source files are in the same directory as the script file itself.

$ServerList = Import-Csv -Path $PSScriptRoot\servers.txt -Delimiter ' ' -Header 'Name', 'CopyFiles';
$FileList = Get-Content -Path $PSScriptRoot\files.txt;

foreach ($Server in $ServerList) {
    if ($Server.CopyFiles -eq 'Yes') {
        foreach ($File in $FileList) {
            Copy-Item -Path "$PSScriptRoot\$File" -Destination ('\\{0}\Projects\MyProject\{1}' -f $Server.Name, $File);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top