Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell Arguements

Posted on 2014-12-18
6
Medium Priority
?
136 Views
Last Modified: 2014-12-19
I am trying to utilize a powershell plugin for amazon s3 to schedule synchronizations of backup files from a local folder to s3.

I can make it work if I make a script for each folder I want to sync but what I'd like to do is pass an argument from the command line to select the local and remote folders that the job should sync.

I'm sure this is pretty basic, but my scripting abilities are minimal.

Here is what I have, but does not work:

*********************************************************
$server=$args[0]
Set-Logging -LogPath S:\ps-log -LogLevel info
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn
$s3 = Get-CloudS3Connection -Key XXXXXXXX -Secret XXXXXXXXX
$dest = $s3 | Select-CloudFolder -Path "phxbackup02/VMbackups/" + $server + "@10.99.0.25"
$src = Get-CloudFilesystemConnection | Select-CloudFolder "S:\Local_Backup\" + $server + "@10.99.0.25\"
Set-CloudOption -UseChunks 1 -ChunkSizeKB 1024000
$src | Copy-CloudSyncFolders $dest -IncludeSubfolders -DeleteOnTarget
*********************************************************************

My problems are on lines 5-6

I just want to be able to change 1 piece of both paths as the $server variable, for example, if I use the command .\script.ps1 "DC01"

Do I need to create 2 more variables for the source and destination paths?
Something like $s3path = "phxbackup02/VMbackups/" + $server + "@10.99.0.25" ?
0
Comment
Question by:bts86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 40507571
It should already work that way. Source and destination path both contain the server name provided as a parameter to the script as you showed.
.\script.ps1 "DC01"  will backup "S:\Local_Backup\DC01@10.99.0.25\" to "phxbackup02/VMbackups/DC01@10.99.0.25"
0
 
LVL 41

Accepted Solution

by:
footech earned 1000 total points
ID: 40507581
You could create more variables, but I think the following will work.
$dest = $s3 | Select-CloudFolder -Path "phxbackup02/VMbackups/$server@10.99.0.25"
$src = Get-CloudFilesystemConnection | Select-CloudFolder "S:\Local_Backup\$server@10.99.0.25\"

Open in new window


When using concatenation, this might work as well.
$dest = $s3 | Select-CloudFolder -Path ("phxbackup02/VMbackups/" + $server + "@10.99.0.25")

Open in new window

0
 
LVL 1

Author Comment

by:bts86
ID: 40508014
Ive tried both suggested ways and still have the same result.

Here is the error I receive:

Select-CloudFolder : The path specified doesn't exist.
At C:\backupsyncs3.ps1:6 char:40
+ $src = Get-CloudFilesystemConnection | Select-CloudFolder "S:\Local_Backup\$serv ...
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (S:\Local_Backup\@10.99.0.25\:String) [Select-CloudFolder], Exception
    + FullyQualifiedErrorId : The path specified doesn't exist.,CloudBerryLab.Explorer.PSSnapIn.Commands.SelectCloudFo
   lder


It doesn't seem to be inserting the variable.


Would it be correct syntax if I used this:
$s3path = ("phxbackup02/VMbackups/" + $server + "@10.99.0.25")
$localpath = ("S:\Local_Backup\" + $server + "@10.99.0.25\")
$dest = $s3 | Select-CloudFolder -Path $s3path
$src = Get-CloudFilesystemConnection | Select-CloudFolder $localpath

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 71

Expert Comment

by:Qlemo
ID: 40508034
It does insert the variable, otherwise you would see the var name literally in the path string. $server just is empty.
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 40508303
I've rewritten the script to be a function with parameters now. Import the function definition with
   . c:\pathToScript\Script.ps1
and then you can use it like this
   S3Sync DC01
or
  S3Sync 'DC01'
The name of the function is arbitrary, but of course it needs to be the same in the script and if you call it. The .PS1 file containing the function can have any name, even the same as the function. It is only used for the import (first line above).
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn

function S3Sync([String] $server)
{
  Set-Logging -LogPath S:\ps-log -LogLevel info
  $s3 = Get-CloudS3Connection -Key XXXXXXXX -Secret XXXXXXXXX
  $dest = $s3 | Select-CloudFolder -Path "phxbackup02/VMbackups/$server@10.99.0.25"
  $src = Get-CloudFilesystemConnection | Select-CloudFolder "S:\Local_Backup\$server@10.99.0.25\"
  Set-CloudOption -UseChunks 1 -ChunkSizeKB 1024000
  $src | Copy-CloudSyncFolders $dest -IncludeSubfolders -DeleteOnTarget
}

Open in new window

0
 
LVL 1

Author Comment

by:bts86
ID: 40509275
Ive got it working using variables for the paths. Maybe the PS snapin does not like concatenation.

Here is what I used to make it work:
$server=$args[0]
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn
Set-Logging -LogPath S:\ps-log\cloudberry.log -LogLevel info
$s3 = Get-CloudS3Connection -Key xxxxxxxxxxxxxxxxx -Secret xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$s3path = ("phxbackup02/VMbackups/" + $server + "@10.99.0.25")
$localpath = ("S:\Local_Backup\" + $server + "@10.99.0.25\")
$dest = $s3 | Select-CloudFolder -Path $s3path
$src = Get-CloudFilesystemConnection | Select-CloudFolder $localpath
Set-CloudOption -UseChunks 1 -ChunkSizeKB 1024000
$src | Copy-CloudSyncFolders $dest -IncludeSubfolders -DeleteOnTarget

Open in new window


Thanks for the help, will split the credit.
0

Featured Post

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question