Need PowerShell Help

I am not an expert on PowerShell and I was asked by my company to come up with a way to do the following: I am not sure how to actually make this happen and was hoping to get some assistance from the experts here at Experts Exchange. Sorry that I am such a Novice.

1. If the server names contain XA65Build2 then
2. Copy epic.cli and server.bgi from Share Location to C:\ProgramData\Epic\Config and to C:\BGInfo and overwrite if present
3. If the server names contain XA65Build1 then
4. Copy epic.cli and server.bgi from Share Location to C:\ProgramData\Epic\Config and to C:\BGInfo and overwrite if present

I believe the PowerShell script could be ran from a Task Scheduler upon Startup. Just not sure how to actually make it work.

Thank you again for your help.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are the source locations the same or different?
Assuming different, just setup the source based on the name.
Something like this:

if ($servername -like '*XA65Build1*') {  $Share = '\\server\share1' }
if ($servername -like '*XA65Build2*') {  $Share = '\\server\share2' }
 foreach ($file in @('epic.cli','server.bgi') { copy "$share\$file" 'C:\ProgramData\Epic\Config', 'C:\BGInfo' }

Open in new window

robertarensonAuthor Commented:
I tried the coding but it first was complaining about a closing ) not being present, I added it but now I am seeing a different type of error
Sorry - slapped that together.  Further details can help fine tune this for you, but here's the correction on the type-o:

if ($servername -like '*XA65Build1*') {  $Share = '\\server\share1' }
if ($servername -like '*XA65Build2*') {  $Share = '\\server\share2' }
 foreach ($file in @('epic.cli','server.bgi')) { copy "$share\$file" 'C:\ProgramData\Epic\Config', 'C:\BGInfo' }

Open in new window

How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

robertarensonAuthor Commented:

I really appreciate your help, I found that the other error was because I had a space between the @ (.

I corrected it but now I am seeing that it is stating that the copy-item is having issues, everything from which you have placed looks good from what I can see so I am not sure why it is erroring out at this point.
Copy-Item accepts several source paths, but only one destination path.
And $Servername will be empty; if you want to reference the computer name, use the environment variable ComputerName.
$SourceFiles = 'epic.cli', 'server.bgi'
$TargetDirs = 'C:\ProgramData\Epic\Config', 'C:\BGInfo'
Switch -Wildcard ($ENV:ComputerName) {
	'*XA65Build1*' {$SourceDir = 'C:\Script\CommCon'}
	'*XA65Build2*' {$SourceDir = 'C:\Script\ClientName'}
	default {Exit}
$SourcePaths = $SourceFiles | ForEach-Object {"$($SourceDir)\$($_)"}
$TargetDirs | ForEach-Object {Copy-Item -Path $SourcePaths -Destination $_ -Force}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robertarensonAuthor Commented:
Thank you oBdA,

I went ahead and tested out your script suggestions and it is exactly what I am needing to have done. I am encountering an error where it is not able to find the files.

I tried taking out the \$ but that was not the problem. I also added a plain text file to the source folders but that was not copied over either. I did validate that all of the files it is looking for are in the C:\Script\CommCon and C:\Script\ClientName folders.

Thanks again
Can't reproduce, unless I change line 9 and add some spaces (though the first might be inside $SourceDir as well):
$SourcePaths = $SourceFiles | ForEach-Object {"$($SourceDir) \$ ($_)"}
#                                                           ^  ^

Open in new window

Download the script again, change only the strings inside single quotes, nothing else. Make sure you don't have leading or trailing spaces.
robertarensonAuthor Commented:
Thank you again oBdA, there was a trailing space that was causing the problem. It is now working.
robertarensonAuthor Commented:
Thank you again ever so much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.