The code that does this is seen here: if(!$source -or !$destination) In the error that appears, we point the user to the Help text syntax. If these two parameters do not exist, we use the throw statement to display an error message and exit the script. The destination can be a UNC path, a mapped network drive, a local drive, or even another folder on the same drive as the source. The destination does not have to have a folder that is the same name as the source. The source is a local path that must exist on the machine and for which you have rights to the folder. These parameters are the –source and the –destination parameters. We need to look for the presence of the two mandatory parameters. The semicolon allows us to run two separate commands on the same line of text. If we find the $help variable, we display a progress message and call the funhelp function. The completed funhelp function is seen here: function BackupFolderToServer.ps1ĭisplays the help topic for the now need to check for the presence of the $help variable. AFter the Help text is created, we display the contents of the $helpText variable on the screen and exit the script. The Help text consists of three sections: the description of the script, the parameters the script will accept, and the syntax that is required. This saves time and reduces potential quoting errors. The here-string is used to allow us to type text in the manner in which it will be displayed on the screen. This variable will be used to store a here-string. The funhelp function begins with a declaration of the $helpText variable. The funhelp function is used to display a Help text message when the script is run with the –help parameter. This line of code is seen here: param($source, $destination, $help) Each of these parameters will be stored in the corresponding variable with the same name. This script defines three parameters: –source, –destination, and –help. It can also be called from other Windows PowerShell scripts. Such a script can be “driven” from a batch file that supplies a number of parameters. This also saves the trouble of having to edit the script before using it. These arguments control how the script will run. The BackupFolderToServer.ps1 script begins with the param statement, which allows us to specify command-line arguments to the script. ![]() ![]() It will also copy files to a user’s mapped home directory. This could be used to copy data to a portable storage device such as a flash memory card or a USB drive. 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. Try this BackupFolderToServer.ps1 -help -?”)Ĭopy-Item -Path $source -destination $destination –recurse $(throw “You must supply both source and destination. the -source and -destination parametersĭisplays the help topic for the “Obtaining help …” funhelp } destination where the files are to be copiedīackupFolderToServer.ps1 -source c:\fso -destination h:\fsoīacks up all files and folders in c:\fso on local machine toĪ mapped drive called h. source the source of the files and folders We hope you will also find it useful, too.īackupFolderToServer.ps1 param($source, $destination, $help)īackes up files in a folder to a mapped drive. When his laptop was repaired and running, he wrote the BackupFolderToServer.ps1 script you see just below. After he arrived at the Microsoft office in Quebec City, he was able to get the laptop repaired (luckily, it was only the keyboard that was damaged, and the remainder of the laptop, including the data on the hard drive, emerged unscathed). (Interestingly enough, this is about the same time that Ed quit drinking coffee.) He lost two days of work on his Windows PowerShell book and became depressed for a week. Because his laptop was not java enabled, it killed the laptop. ![]() ![]() One time a couple of years ago, while Ed was traveling to Quebec City, the airplane he was riding in hit what felt like a series of speed bumps in the sky, and his cup of hot coffee went onto his laptop. In honor of his presence, it has been sunny with clear skies all week long. He is out in Redmond, Washington, in the United States this week teaching a group of Microsoft engineers Windows PowerShell. Wait while he goes and gets a cup of tea. Scripting Guy Ed agrees with you and shares your concerns. My job requires me to travel extensively, and I am just paranoid about a laptop crash and about losing my data. I am concerned about running through the airport and having it getting bumped, dropped, or stolen. Hey, Scripting Guy! I would like to be able to perform a backup of only certain folders on my Windows Vista laptop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |