dimanche 16 décembre 2018

Powershell trying to backup from 2 locations using if statement

I'm trying to create a simple program in Powershell. What I'm trying to do is backup two different folders into one location, if the user chooses 'Y' by the prompt. If the user chooses 'N' then the user shouldn't be prompted for a second folder and should instead just be prompted for the backup location.

However, it's not working for me and I'm stuck. Is anyone able to help? Much appreciated.

$ErrorActionPreference = "Stop"
#If there is a error stop!
$theFolder = Read-Host -Prompt "Enter folder location you wish to backup"
# variable 'theFolder' = the entered file location.
$question = Read-Host -Prompt "Would you like to backup another folder? 'Y' or 'N'"
if ($question -eq 'Y') {$moreFolder = Read-Host "Enter second folder location"} else
# variable 'backup' = the entered backup location
{$backup = Read-Host -Prompt "Enter backup location"}
echo $thefolder
echo $backup
echo $moreFolder
# checking to see if there is a connection there.
Test-Path $theFolder
Test-Path $moreFolder
Test-Path $backup
# testing the path of both the folder and backup location
Copy-Item -Path $theFolder\* -Destination $backup -Recurse
Copy-Item -path $moreFolder\* -Destination $backup -Recurse
# copy via file pathway defined ealier. put into destination staging.
# recurse = bypass all prompts (are you sure etc)

Aucun commentaire:

Enregistrer un commentaire