Link to home
Get AccessLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

Help with an "if...else" statement in Powershell

Hello Experts,

I’m having the worst time with an if…else statement in Powershell.  Basically what I want to do is have Powershell check to see if a folder exists in a specified location.  If it doesn’t exist, I want it to:
1.       Create the folder
2.      Create a log file within the folder
3.      Add entries into the log file

I have this part working.  Where I’m getting messed up is in the “else” part.  Basically what I want to happen is if the folder already exists, then an entry into the log file is added.  My powershell code is as follows:
$ArchiveDate = Get-Date -Format yyyyMMdd
$Archivefile = "$ArchiveDate.txt"
$ArchiveFilePath = "\\mydomain.local\shares\users\nickd\target"




if(-not(test-path -Path "$ArchiveFilePath\$ArchiveDate.txt" ))
{
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) A process has checked to see if the archive folder exists."
New-Item -Path "$ArchiveFilePath\$ArchiveDate" -ItemType directory
New-Item -Name "$ArchiveDate`_log.txt" -Path "$ArchiveFilePath\$ArchiveDate" -ItemType file
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The archive folder '$ArchiveDate`_log.txt' has been created"
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The log file '$ArchiveDate`_log.txt' has been created"
}
Else
{
       Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) A process has checked to see if the archive folder exists." 
}

Open in new window


Thanks in advance for your help.

Nick
Avatar of jmcg
jmcg
Flag of United States of America image

Do you need a three way logic path, for the case that the folder exists but not the file that you want?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of ndalmolin_13

ASKER

Thanks for the explaination