Background story (that you can safely ignore)
I need to publish the same app to different Delivery Groups in Citrix. There are more than 50 Machines that will need the file in that location. Some of them have a copy of the file already, some do not. (In my case, it is a PowerShell Script with a .PS1 extension) This script comes in handy and I do not need to manually copy the file to the servers. With a slight modification, you can copy a folder to the destination. This assumes the computer and the account that you run the script with have read/write permission to these remote servers.
The Script
$AdminAddress = #put in the name of the Delivery Controller here
$Machines = Get-BrokerMachine -adminaddress $AdminAddress #This grab the info of all the Machines in the Citrix Farm
$source = “\\serverName\C$\temp\App.ps1” #replace ServerName with the server’s name that host your files. You will also need to replace the path to the file.
Foreach ($Machine in $Machines){ #This is a For Loop that will loop through each machine
$Path = "\\" + $Machine.DnsName + "\C$\temp\App.ps1" #define the path to test if the file is already in this server
$Destination = "\\" + $Machine.DnsName + "\C$\Temp" #define the destination to copy the file to
If(Test-Path $Path){ #this will test the path and check if the file is already there. If it is already there, it will perform the action within {} and that is to return a text message on the PowerShell window that says the file is already there.
Write-Host 'File is already there in' $Machine.DnsName ', skipped' -ForeGroundColor Green}
ElseIf(!(Test-Path $Path)){ # if the first test return False, it will go to this ElseIf statement and perform what’s in {} and that is to copy the file from $Source to $Destination and return a message on PowerShell window saying that the file was copied to the Destination Server.
Copy-Item $Source -Destination $Destination
Write-Host 'File Copied to' $Machine.DnsName -ForeGroundColor Green}
}
If you are trying to copy a File to multiple machines in a domain, this script can also work if you have a .CSV file that has all the name of the Servers and modify it with a Get-Content command.
$Machines = Get-Content C:\Temp\Server.csv #assuming you have a .CSV file in C:\temp
Foreach ($Machine in $Machines){ #This is a For Loop that will loop through each machine
$Path = “\\” + $Machine + "\C$\temp\App.ps1" #define the path to test if the file is already in this server
$Destination = "\\" + $Machine + "\C$\Temp" #define the destination to copy the file to
If(Test-Path $Path){ #this will test the path and check if the file is already there. If it is already there, it will perform the action within {} and that is to return a text message on the PowerShell window that says the file is already there.
Write-Host 'File is already there in' $Machine ', skipped' -ForeGroundColor Green}
ElseIf(!(Test-Path $Path)){ # if the first test return False, it will go to this ElseIf statement and perform what’s in {} and that is to copy the file from $Source to $Destination and return a message on PowerShell window saying that the file was copied to the Destination Server.
Copy-Item $Source -Destination $Destination
Write-Host 'File Copied to' $Machine -ForeGroundColor Green}
}
If you need to copy multiple files from the same location, you can use a *.* wildcard. For example, if you have 10 files all with different file extensions in c:\temp, you can make the source C:\temp\*.* to copy everything in that folder. If you only want to copy the .txt files, you can define as *.txt