Help on IF Else statement

This topic contains 2 replies, has 2 voices, and was last updated by  Leon P 4 months ago.

  • Author
  • #89822

    Leon P

    I am new to PowerShell and stuck on how to complete the next step in my script. I realise it probably inst the best way to do this but its what i came up with. The scenario is that i need to check that our backups are creating a weekly archive, i have created the below and happy for suggestions on a better way to do it but not what my question is. I want to know if there is a way to have an if statement for all the other If/Else statements in the script. To expand on this is want to have an if statement so that is the other if/else statements comes back as archives are ok then it just outputs one "archives are ok" message rather than the 4 it currently outputs. Also if one fails it just tells you which has failed. Any help would be appreciated TIA Leon.

    internet = (Get-Item "THEACTUALPATH").LastWriteTime
    $a3 = (Get-Item "THEACTUALPATH").LastWriteTime
    $cn3 = (Get-Item "THEACTUALPATH").LastWriteTime
    $management = (Get-Item "THEACTUALPATH").LastWriteTime
    $date = {

    if ($internet = $date)
    { "Internet servers archives ok"}
    { "Internet servers archive failed"}

    if ($n3 = $date)
    { "N3 Servers archives ok"}
    { "N3 Server archive failed"}

    if ($cn3 = $date)
    { "CN3 Servers archives ok"}
    { "CN3 Servers archive failed"}

    if ($management = $date)
    { "Management Servers archives ok"}
    { "Management Servers archive failed"}

  • #89827

    Sam Boutros

    break your code into regions for easier readability

    #region Input
    $MyFileList = @(
        '\\\e$\Install\Scripts\AD DS Deployment.txt'
        'C:\Program Files\Common Files\microsoft shared\ClickToRun\ApiClient.dll'
    $AcceptableDays = 7 # This is how far back a file lastwritetime can be to be considered OK, in days
    #region Process
    $MyFileList | foreach {
        $FileInfo = Get-Item -Path $PSItem
        $FileAgeDays = (New-TimeSpan -Start $FileInfo.LastWriteTime -End (Get-Date)).TotalDays
        if ($FileAgeDays -le $AcceptableDays) {
            "File ($PSItem) backup OK ($([Math]::Round($FileAgeDays,1))) days"
        } else {
            "File ($PSItem) backup failed ($([Math]::Round($FileAgeDays,1))) days"
  • #89830

    Leon P

    Thanks alot Sam, your script also let me work out the answer to my main question on how to change the output to one line saying archives ok rather than several lines 🙂

You must be logged in to reply to this topic.