Link to home
Create AccountLog in
Avatar of robertarenson
robertarenson

asked on

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.

Brian
Avatar of sirbounty
sirbounty
Flag of United States of America image

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

Avatar of robertarenson
robertarenson

ASKER

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
ScriptError.png
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

SirBounty,

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.
ScriptErrorUpdated.png
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
ScriptErrorNew.png
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.
Thank you again oBdA, there was a trailing space that was causing the problem. It is now working.
Thank you again ever so much