0

Long day, might be missing something obvious, but the nested IF statements below should work. Any suggestions?

Basically, what I am trying to do:

  1. Take a given hostname (this will later on be incorporated into a loop).
  2. Take a log file and move it from one location to another (location is always constant).
    • 2A. Test IF the folder exists:
      • 2A-1. If the folder exists, test if the file is in the folder.
        • 2A-1a. True: Delete the file
        • 2A-1b. False: Copy the file to the folder.
      • 2A-2. Else: Copy the file to the folder.
    • 2B. ELSE:
      • 2B-1. Create the folder.
      • 2B-2. Copy the file to the folder

I did the below:

$Computer = "hostName"

Enter-PSSession -ComputerName $($Computer)

$LogFileName = "program_" + $Computer + ".log"
$LogFilePath = "\\$Computer\C$\users\$env:USERNAME\desktop\$LogFileName"
$DestPath = "\\$Computer\C$\users\$env:USERNAME\ProgramTest\"
$LogCopy = "programCopy_" + $Computer + ".log"

IF(Test-Path $($DestPath)){
   If(Test-Path "$DestPath\$LogCopy"){
      Remove-Item "$DestPath\$LogCopy"
      Copy-Item $($LogFilePath) $($DestPath)
       }
   Else{
       Copy-Item $($LogFilePath) $($DestPath)
      }
ELSE{
   New-Item -ItemType directory -Path $($DestPath)
   Copy-Item $($LogFilePath) $($DestPath)
   }
}
1
  • What is your intent? From what I can tell it is: Check for destination folder, creating it if it doesn't exist. Then copy a log file to it, overwriting any existing copy of the file if one exists. Commented Dec 10, 2014 at 22:43

2 Answers 2

4

I see you have already accepted an answer but I wanted to offer and improvement on the logic. The nested if's, while do work, have some redundancies in them. Assuming I understand your intention...

# If the folder does not exist create it. If it does move on
If(!(Test-Path $DestPath)){New-Item -ItemType directory -Path $DestPath}

# If the log file already exists. Remove It. 
If(Test-Path "$DestPath\$LogCopy"){Remove-Item "$DestPath\$LogCopy"}

# Regardless of what happens we are going to copy the file. No need to repeat the code. 
Copy-Item $($LogFilePath) $($DestPath)

While that is an improvement on the logic we can take this a little further. If you want to be sure the file is fresh and overwritten Copy-Item will handle that for you.

# If the folder does not exist create it. If it does move on
If(!(Test-Path $DestPath)){New-Item -ItemType directory -Path $DestPath}

# Regardless of what happens we are going to copy the file. No need to repeat the code. 
Copy-Item $LogFilePath $DestPath -Confirm:$false -Force

$() is the notation for a subexpression. A common use in similar context would be to ensure that a variable expands properly in a string. As shown in the following example

PS C:\Users\Cameron> $data = @{Matt="Awesome"}

PS C:\Users\Cameron> "$data.Matt"
System.Collections.Hashtable.Matt

PS C:\Users\Cameron> "$($data.Matt)"
Awesome

Currently in your code, while causing no harm, it is redundant.

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

Comments

0

Beyond the dubious use of $($Variable) (completely redundant; just use $Variable), you've misplaced a closing brace. The last five lines should look like this:

}
ELSE{
    New-Item -ItemType directory -Path $($DestPath)
    Copy-Item $($LogFilePath) $($DestPath)
}

If that doesn't work, let us know what's actually happening: errors and such when you try to run it.

2 Comments

Thanks, your suggestions helped. I am definitely still a noob, but there is no real solution to that except to code. One extra point that I worked out on my own is that $pathVar\$fileVar leads to an additional "\" in the middle (i.e. \) which breaks the code. I changed that to the pattern: Test-Path ("$pathVar" + "fileVar") in a number of places, and that fixed it with the additional points you made. Thanks so much!
Btw, I would appreciate if others UpVote Nathan's answer, as I don't quite have enough XP to do it.

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.