Dwight
Dwight

Reputation: 13

Powershell: File date/time stamp check, need to output to exit code

I deploy custom code to thousands of computers and have been able to get the return code to function correctly for one or two objects in the tool I have to use to push out code. But I am looking for a way of setting up a file validator - because the Dev folks don't consistently use version numbering I have been able to use the below code to check for the date stamp for each object.

Code:

$foo1= Get-ChildItem "C:\path\file1.exe" | Where-Object {$_.LastWriteTime -gt "11/1/2013"} | Out-String
$foo2= Get-ChildItem "C:\path\file2.exe" | Where-Object {$_.LastWriteTime -gt "9/10/2013"} | Out-String
$foo3= Get-ChildItem "C:\path\file3.exe" | Where-Object {$_.LastWriteTime -gt "4/23/2013"} | Out-String
$foo4= Get-ChildItem "C:\path\file4.exe" | Where-Object {$_.LastWriteTime -gt "12/17/2012"} | Out-String

The above works but will show the object name and the last write time. I can write the exit code with this code:

if($foo1){Write-Host '0'
}else{
Write-Host '5'
Exit 5 
}

Is there a way I can state if foo1 exists (i.e. not $null) then read it as a 0 and if it is null read it as a 1 and then state that $foochecksum = $foo1 + $foo2 + $foo3 + $foo4 and do the If Else cited above just once to write the exit code to my deployment tool?

Functionally what I am looking for is a way of checking multiple file date / time stamps and then if all are good passing one 0 to the If/Else statement that will write a pass or fail to my deployment tool.

I could use multiple if/else's if need be but will need to check something like 40 files and would rather not have to have 40 different IF/Else statements.

I would also love to have something that might work in PS V2 and V3 as I have a mix of 2003 and 2008 servers in prod.

Thanks,

Dwight

Upvotes: 0

Views: 4134

Answers (2)

user189198
user189198

Reputation:

Use a variable to hold the "error state" of your script, and a HashTable to hold the Path and LastWriteTime values for each file that you are "testing."

$ErrorExists = $false;

# Declare some file/lastwritetime pairs
$FileList = @{
        1 = @{ Path = 'C:\path\file1.exe';
        LastWriteTime = '11/1/2013'; };
        2 = @{ Path = 'C:\path\file2.exe';
        LastWriteTime = '9/10/2013'; };
        3 = @{ Path = 'C:\path\file3.exe';
        LastWriteTime = '4/23/2013'; };
        4 = @{ Path = 'C:\path\file4.exe';
        LastWriteTime = '12/17/2012'; };
        };

foreach ($File in $FileList) {
    # If LastWriteTime is LESS than the value specified, raise an error
    if ((Get-Item -Path $File.Path).LastWriteTime -lt $File.LastWriteTime) {
        $ErrorExists = $true;
    }
}

if ($ErrorExists) {
    # Do something
}

Upvotes: 1

mjolinor
mjolinor

Reputation: 68273

Maybe something like this?

$foos = &{
Get-ChildItem "C:\path\file1.exe" | Where-Object {$_.LastWriteTime -gt "11/1/2013"} | select -last 1
Get-ChildItem "C:\path\file2.exe" | Where-Object {$_.LastWriteTime -gt "9/10/2013"}  | select -last 1
Get-ChildItem "C:\path\file3.exe" | Where-Object {$_.LastWriteTime -gt "4/23/2013"} | select -last 1
Get-ChildItem "C:\path\file4.exe" | Where-Object {$_.LastWriteTime -gt "12/17/2012"} | select -last 1
}

if ($foos.count -eq 4) {Write-Host '0'}
else {Write-Host '5';Return '5'}

Upvotes: 0

Related Questions