I am newb in powershell but keen to put it into good use. I am working on a script which should do the following:
- Check for the existence of a specific folder in a specific location (mapped drive)
- If the folder exists, then return a listing
- If the folder does not exist, then create it.
Ideally, I would like to improve it in terms of check-if exists-remove-item (subdir); check-if not exists-create
This is to facilitate the automation of an archiving process for a specific piece of software. What I have right now sort of works but I cannot figure out how to make it do exactly what I want.
Here is the code:
$X = @("Server1", "Server2", "Server3")
$ChkFile = "f:\archive\$server\AABackup"
$myFolder = "f:\archive\$server"
$DirExists = Test-Path $ChkFile
Foreach ($server in $x){
IF ($DirExists -eq $True) {
Remove-Item $ChkFile -recurse
import-Module "AppAssurePowerShellModule"
start-archive -protectedserver $server -startdate "18/03/2013 5:30 PM" -path "f:\archive\$server"
}
Elseif ($DirExists -ne $True) {
New-Item -path $myFolder -itemType "directory"
import-Module "AppAssurePowerShellModule"
start-archive -protectedserver $server -startdate "18/03/2013 5:30 PM" -path "f:\archive\$server"
}
}
Yes I know it is rough... It's my first attempt though so I could definitely benefit from the more experienced scripters here.
Thanks in advance.
$ChkFileand$myFolderlines inside the foreach-loop. The variable$serveris expanded to it's current value when you set the variables, so they are STATIC with the value of$serverat the time of creation. By moving them into the foreach, they will be different for each server(like you want them to). Also, move theImport-Moduleline to the start of the script. When it's imported it's imported, you don't want or need to do that every time in your loop