1

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.

1
  • Move the $ChkFile and $myFolder lines inside the foreach-loop. The variable $server is expanded to it's current value when you set the variables, so they are STATIC with the value of $server at the time of creation. By moving them into the foreach, they will be different for each server(like you want them to). Also, move the Import-Module line 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 Commented Mar 20, 2013 at 19:02

2 Answers 2

1

You're setting $ChkFile, $myFolder and $DirExists before the loop, which means that $server doesn't have a value yet, and -- since variables are evaluated immediately -- these variables will contain garbage.

You need to move those three statements inside the foreach loop.

You also don't need to compare -eq $true; this would be simpler:

if ($dirExists) {
    # ...
}
else {
    # ...
}

Oh, and you only need to import the module once -- do it at the top of the script.

Also, in terms of style: PowerShell keywords should generally be in lowercase: foreach, if, elseif; be consistent when invoking cmdlets (you have a mixture of lower-case and Upper-Case and lower-Case. Note that these don't make any real difference, but using a consistent style makes the code easier to read for someone else coming to it. I'm basing those rules on what I've seen on TechNet, PoshCode, and here, by the way -- they're definitely subjective.

And, personally, I use $lowerCase for local variables, and $UpperCase for parameters (because it makes the auto-generated help text look nicer).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your insight! I have simplified the script significantly... The result is that the desired actions are taken against each server.
0

Give this a shot.

$specificPath = Z:\MYDir\MySubDir

if(!(Test-Path $SpecificPath))
{
    Mkdir $SpecificPath  
}
Else
{
    Get-ChildItem $specificPath
}

Explanation: This checks for the existence of the path contained in $SpecificPath using Test-Path, which will return a Boolean value. Since I used the (!()) syntax in my IF statement, it will try to evaluate the statement to false, IF the path DOES NOT EXIST, it will run the first block of code. MkDir is an alias for New-ItemProperty, if you pass just a path to Mkdir it will make a directory, similar to the windows MkDir command. If the statement contained in the IF statement does not evaluate to false, the ELSE block will run, and execute a get-childitem on the $specificpath variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.