Solved

Powershell time within 2 minutes of listed

Posted on 2011-09-12
4
903 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

740 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