Solved

Powershell time within 2 minutes of listed

Posted on 2011-09-12
4
888 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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