Solved

Powershell time within 2 minutes of listed

Posted on 2011-09-12
4
874 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
$day and $month round the wron way.  Thenks working nicely.
0
 
LVL 4

Author Closing Comment

by:PeterSinger
Comment Utility
Very Nice, thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now