MaryK
 New Member Posts:31
 |
| 18 Aug 2008 09:53 PM |
|
Hello,
I am trying to create a script that will find users created between yesterday and today's date. Howerev, I am getting some errors. Can anybody tell me what's wrong with this part of the script?
$dayStart
= "{0:yyyyMMdd}000000.0Z" -f (Get-Date)
$dayEnd
= "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1))
$newuser
= Get-User | where-object {$_.RecipientType –eq "User" -and $_.whenCreated>=$dayStart -and $_.whenCreated<=$dayEnd}
The error we are getting :
Parsing error:
At line 4, position 100
Unexpected token '-and' in expression or statement.
|
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:06 PM |
|
Your operators are wrong. In PS:
-ge: Greater than or equel
-le: Less than or equel
Check this help files for more information:
help about_Comparison_Operators
help about_logical_operator
$dayStart = get-date
$dayEnd = $dayStart.AddDays(-1) # i believe you want to subtract a day
Get-User -sizeLimit 0 | where {$_.whenCreated -ge $dayStart -and $_.whenCreated -le $dayEnd} |
|
|
|
|
bsonposh
 Basic Member Posts:388
 |
| 18 Aug 2008 10:09 PM |
|
Your problem is that you need to add () around each statement $dayStart = "{0:yyyyMMdd}000000.0Z" -f (Get-Date) $dayEnd = "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1)) $newuser = Get-User | where-object {($_.RecipientType –eq "User") -and ($_.whenCreated>=$dayStart) -and ($_.whenCreated<=$dayEnd)} |
|
|
|
|
bsonposh
 Basic Member Posts:388
 |
| 18 Aug 2008 10:10 PM |
|
what shay said... I was thinking LDAP Filter :) btw... it is absolutely absurd that get-user doesn't support LDAP filters (AFAICT) |
|
|
|
|
KarlMitschke
 Basic Member Posts:161
 |
| 18 Aug 2008 10:10 PM |
|
I'm not sure about your dates there - it looks like $daystart is today, and $dayend is tomorrow? i did this, and it worked fine:
$dayStart = ((Get-Date).AddDays(-1))
$newuser = Get-User -resultsize unlimited | where-object {$_.RecipientType -eq "User" -and $_.whenCreated -ge $dayStart}
|
|
|
|
|
bsonposh
 Basic Member Posts:388
 |
|
MaryK
 New Member Posts:31
 |
| 18 Aug 2008 10:16 PM |
|
Thanks for catching that. Howerver, I tried your suggestion but it's not working. here is what I am getting: A parameter cannot be found that matches parameter name 'sizeLimit'. At line 5, position 73 $newuser = Get-User -sizeLimit 0 | where {$_.whenCreated -ge $dayStart -and $_.whenCreated -le $dayEnd} I removed sizeLimit then I get the following: The '-ge' operator failed: Could not compare "3/26/2008 1:02:22 PM" to "20080818000000.0Z". Error: "Cannot convert value "20080818000000.0Z" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."". At line 5, position 119 $newuser = Get-User | where {$_.whenCreated -ge $dayStart -and $_.whenCreated -le $dayEnd} |
|
|
|
|
bsonposh
 Basic Member Posts:388
 |
| 18 Aug 2008 10:17 PM |
|
She wants all the users created on that date. Thus she needs a range gt that 12:AM but Less that 11:59pm. |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:17 PM |
|
Brandon, now that you mention it, I was sure that Get-User was actually Get-QADUser, LOL :)
That's whay I removed the '$_.RecipientType -eq "User" ' filter, now I see that MaryK was actually using the EX2007 cmdlet. |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:19 PM |
|
Karl,
You're code posts are *unreadable*, they appear inside a 1.5 lines text area :) |
|
|
|
|
bsonposh
 Basic Member Posts:388
 |
| 18 Aug 2008 10:20 PM |
|
Mary... I think the problem is we (you and I) are thinking AD, but Exchange is doing magic for you. Try this (replace day month year accordingly) $startDay = get-date -day 12 -month 12 -year 2008 -Hour 00 $endDay = get-date -day 12 -month 12 -year 2008 -Hour 23 -minute 59 |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:21 PM |
|
Time to launch my Ex2007 VM :) |
|
|
|
|
KarlMitschke
 Basic Member Posts:161
 |
| 18 Aug 2008 10:23 PM |
|
When I do that, $daystart is 20080818000000.0Z, just like it is for the OP. So, if daystart is today, and $dayend is 20080819000000.0Z (tomorrow), you don't even need to worry about -lt $dayend, because all accounts that already exist on the domain were created before tomorrow :)
But, I also get the "Could not compare "3/26/2008 1:02:22 PM" to "20080818000000.0Z"
i don't get that when I use $dayStart = ((Get-Date).AddDays(-1)) - what I do get is all accounts created from yesterday to today.
Karl |
|
|
|
|
KarlMitschke
 Basic Member Posts:161
 |
| 18 Aug 2008 10:26 PM |
|
Posted By Shay on 08/18/2008 2:19 PM
Karl,
You're code posts are *unreadable*, they appear inside a 1.5 lines text area :)
Shay;
I have seen that, and was going to ask how come yours are all color syntax and viewable? I do the < code > < /code > tags, and it's always ugly |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:33 PM |
|
Mary,
Why are you converting the dates when you perform date comparison using *regular* dates? (am i missing something)
When I get users using Get-User the WhenChanged,WhenCreated members shows up like:
WhenChanged : 06/08/2008 12:14:25
WhenCreated : 06/08/2008 11:59:12
So, I can run this without a problem:
$start = (get-date).addDays(-7)
$end = (get-date).addDays(-2)
get-user | where {$_.whenCreated -ge $start -and $_.whenCreated -le $end }
or, like brandon suggests
get-user | where { ($_.whenCreated -ge $start) -and ($_.whenCreated -le $end) } |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 10:38 PM |
|
Karl,
Maybe the code tags are the trouble maker, try to post one with code but without the code tags. As for the color, I just copied Mary's code and changed it, it retained the color. If you have PowerGUI then you can copy/paste the code it generates (i think its converted in the background to html) then you can paste it into the post editor.
|
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 18 Aug 2008 11:06 PM |
|
Brandon,
Get-User support the -filter parameter, so this is valid to execute:
PS > Get-User -Filter "whencreated -ge '$start' -and whencreated -le '$end'"
|
|
|
|
|
MaryK
 New Member Posts:31
 |
| 19 Aug 2008 01:57 AM |
|
Shay, I forgot to mention that I code I provided was courtesey of Jonathan Medd and I found it to do exactly what I was looking for (of course if it works). But I think I am still missing something since your code returns no results for me though I know there is a user I created earlier today and yesterday. I even expanded the date to no avail. $start = (get-date).addDays(-100) $end = (get-date).addDays(-1) get-user | where { ($_.whenCreated -ge $start) -and ($_.whenCreated -le $end) } | c:\chagedUsers.txt All I am looking to do is find users who were created between yeterday ad today. Thanks |
|
|
|
|
MaryK
 New Member Posts:31
 |
| 19 Aug 2008 02:57 AM |
|
It looks like Karl's code work. I am still trying to get Shay's version to work to see if I am missing anything.
Thanks
$dayStart
= ((Get-Date).AddDays(-1))
$newuser
= Get-User -resultsize unlimited | where-object {$_.RecipientType -eq "User" -and $_.whenCreated -ge $dayStart} |
|
|
|
|
Shay
 Basic Member Posts:228
 |
| 19 Aug 2008 07:33 AM |
|
What this gives?
$dayStart = (get-date).addDays(-1)
Get-User -resultSize unlimited -filter "whenCreated -ge '$dayStart'" |
|
|
|
|
MaryK
 New Member Posts:31
 |
| 19 Aug 2008 03:45 PM |
|
That worked! Thanks. |
|
|
|
|