Long day, might be missing something obvious, but the nested IF statements below should work. Any suggestions?
Basically, what I am trying to do:
- Take a given hostname (this will later on be incorporated into a loop).
- 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.
- 2A-1. If the folder exists, test if the file is in the folder.
- 2B. ELSE:
- 2B-1. Create the folder.
- 2B-2. Copy the file to the folder
- 2A. Test IF the folder exists:
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)
}
}