?
Solved

Powershell time within 2 minutes of listed

Posted on 2011-09-12
4
Medium Priority
?
938 Views
Last Modified: 2012-05-12
Hi,

I have a powershell script that I am comparing the two fields;
Get-QADGroup UGL-SM-Map.Shared-Full |ft ModificationDate, Description

ModificationDate                                            Description
----------------                                            -----------
13/09/2011 10:55:37 AM                                      13/09/2011 10:55:37 AM

ModificationDate                                            Description
----------------                                            -----------
13/09/2011 10:55:37 AM                                      13/09/2011 10:55:39 AM



I want to say if the date and time is -eq or within 2 minutes of each other then return true if not return false.

Help please :)
0
Comment
Question by:PeterSinger
  • 3
4 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 36526886
You can try something like this. You can probably clean up the code but should work for what you need if the data will stay the same in the description and modification attributes..



Function format-time{
Param($Time)
$Out = $time -replace "/", " "
$out = $out -replace ":", " "
$out = $out -split " "
$day = $out[0]
$Month = $out[1]
$Year = $out[2]
$Hour = $out[3]
$Minute = $out[4]
    If ($out[6] -eq "PM"){
        [int]$hour = [int]$hour + 12
    }
get-date "$month/$day/$year" -Month $month -hour $hour -minute $minute
} #end function format-time

$Group = Get-QADGroup UGL-SM-Map.Shared-Full 
If ((New-TimeSpan (Format-time $($group.ModificationDate)) (Format-time $($Group.Description))).minutes -le "2"){
"True"
}
ELse {
"False"
}

Open in new window

0
 
LVL 4

Author Comment

by:PeterSinger
ID: 36527129
Thanks.

I get the below back;

Get-Date : Cannot bind parameter 'Date'. Cannot convert value "09/13/2011" to type "System.DateTime". Error: "String wa
s not recognized as a valid DateTime."
At D:\Scripts\SharedMailboxes\test.ps1:15 char:10
+     get-date <<<<  "$month/$day/$year" -Month $month -hour $hour -minute $minute
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

Get-Date : Cannot bind parameter 'Date'. Cannot convert value "09/13/2011" to type "System.DateTime". Error: "String wa
s not recognized as a valid DateTime."
At D:\Scripts\SharedMailboxes\test.ps1:15 char:10
+     get-date <<<<  "$month/$day/$year" -Month $month -hour $hour -minute $minute
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

New-TimeSpan : Cannot bind parameter 'End' to the target. Exception setting "End": "Object reference not set to an inst
ance of an object."
At D:\Scripts\SharedMailboxes\test.ps1:20 char:18
+ If ((New-TimeSpan <<<<  (Format-time $($group.ModificationDate)) (Format-time $($Group.Description))).minutes -le "2"
){
    + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand
0
 
LVL 4

Author Comment

by:PeterSinger
ID: 36527147
$day and $month round the wron way.  Thenks working nicely.
0
 
LVL 4

Author Closing Comment

by:PeterSinger
ID: 36527149
Very Nice, thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's a look at newsworthy articles and community happenings during the last month.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question