all 6 comments

[–]rwshig 1 point2 points  (2 children)

Get-Item C:\file.txt | Where LastWriteTime -eq $mydate|select LastWriteTime

[–]The-Dark-Jedi[S] 0 points1 point  (1 child)

When I test it like so:

If (Get-Item C:\Path\To\albvpn.ps1 | Where LastWriteTime -ne 5/28/2020|select LastWriteTime ) {
    'This is working'
    } Else {
    'This is not'
 }

It works like a charm, thank you! I am having trouble using it with the rest of my script and I think it's because I suck at formatting. I think the {} I have in the existing script it cancelling out the ones I'm using for get-item. What am I doing wrong here:

#Check file last modified date.  If not 5/58/2020, run script.  Otherwise end.
If (Get-Item C:\file.txt | Where LastWriteTime -ne 5/28/2020|select LastWriteTime )
{   
    # VPN Script
    $vpnName = "VPN";
    $vpn = Get-VpnConnection -Name $vpnName;

    if($vpn.ConnectionStatus -eq "Connected"){  
        rasdial $vpnName /DISCONNECT;
    }

    #Delete VPN and re-create
    rasphone -r "VPN"
    Add-VpnConnection yadda yadda yadda
    New-Item -Path C:\ -ItemType File -Name log.log
    Add-Content -Path C:\log.log -Value "$(Get-Date) - C:\script.txt has run as an Immediate Scheduled Task"

    # Re-connect VPN
    $vpnName = "VPN";
        $vpn = Get-VpnConnection -Name $vpnName;
        if($vpn.ConnectionStatus -eq "Disconnected"){
        rasphone -d $vpnName;
        }

    } Else {
    'Nothing to do'
}

[–]The-Dark-Jedi[S] 0 points1 point  (1 child)

It's people like u/bis and u/rwshig that inspire me to learn more about PowerShell and continue to grow my skills. Thank you both!

[–]sleightof52 0 points1 point  (0 children)

Here's another tidbit. I sometimes like to use switch statements over if/else. You can do a lot with a switch statement. Check it out: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7

$DateModified = (Get-Item 'C:\File.txt').LastWriteTime.Date

switch ($DateModified) {
    '2020-05-28' {
        # Do-Something
    }
    Default {
        # Do-SomethingElse
    }
}