Scripts/Set-LastModified.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
param ( $date = 30, [switch]$whatif )
BEGIN
{
    if ( $date -is [datetime] )
    {
        if ( $date -gt [datetime]::Now )
        {
            throw "must have a date before now"
        }
    }
    else
    {
        if ( $date -is [int] )
        {
            $date = [datetime]::Now.AddDays(-$date)
        }
        else
        {
            throw "provide a date (or days)"
        }
    }
    "Setting date to $date"
    $cstr = "Server=localhost;Database=ServiceManager;Trusted_Connection=yes"
    $c = [data.sqlclient.sqlconnection]$cstr
    $c.open()
}
PROCESS
{
    # this has to be an instance
    # we should check
    if ( $_ -is [Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject] )
    {
        $id = $_.get_id()
        $comm = $c.createcommand()
        $command = "Update BaseManagedEntity SET LastModified='" + $date + "' Where BaseManagedEntityId='$id'";
        $comm.CommandText = $command
        if ( $WhatIf )
        {
            $command
        }
        else
        {
            $r = $comm.ExecuteNonQuery()
            if ( $r -ne 1 ) { "Couldn't set BME" }
        }
    }
}

END
{
    $c.close()
}