In the BackupFolderToServer.ps1 script, we use the Copy-Item cmdlet to copy files in a particular folder to a mapped drive location on a server or some other device on which you wish to store your user data. This could be used to copy data to a portable storage device such as a flash memory card or a USB drive. It will also copy files to a user’s mapped home directory. The BackupFolderToServer.ps1 script begins with the param statement, which allows us to specify command-line arguments to the script. These arguments control how the script will run. This also saves the trouble of having to edit the script before using it. Such a script can be “driven” from a batch file that supplies a number of parameters. It can also be called from other Windows PowerShell scripts. This script defines three parameters: -source, -destination, and -help. Each of these parameters will be stored in the corresponding variable with the same name. This line of code is seen here:
Code:
param($source, $destination, $help)
We then create the funhelp function. The funhelp function is used to display a Help text message when the script is run with the -help parameter. The funhelp function begins with a declaration of the $helpText variable. This variable will be used to store a here-string. The here-string is used to allow us to type text in the manner in which it will be displayed on the screen. This saves time and reduces potential quoting errors. The Help text consists of three sections: the description of the script, the parameters the script will accept, and the syntax that is required. AFter the Help text is created, we display the contents of the $helpText variable on the screen and exit the script. The completed funhelp function is seen here:
Code:
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: BackupFolderToServer.ps1
Backes up files in a folder to a mapped drive. The destination
folder does not have to be present
PARAMETERS:
-source the source of the files and folders
-destination where the files are to be copied
-help prints help file
SYNTAX:
BackupFolderToServer.ps1 -source c:\fso -destination h:\fso
Backs up all files and folders in c:\fso on local machine to
a mapped drive called h. The \fso folder does not need to
exist on the h:\ drive.
BackupFolderToServer.ps1
generates an error. the -source and -destination parameters
must be present
BackupFolderToServer.ps1 -help ?
Displays the help topic for the script
"@
$helpText
exit
}
Bookmarks