Need PowerShell Help

robertarenson
robertarenson used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
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

Author

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
ScriptError.png
Top Expert 2007

Commented:
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

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

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
ScriptErrorNew.png
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Thank you again oBdA, there was a trailing space that was causing the problem. It is now working.

Author

Commented:
Thank you again ever so much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial