0

I have a script that works properly when run as an administrator but gives a Parse error when run as a normal user. Any Ideas?;enter image description here

SCRIPT

`NeverExpires = 9223372036854775807;
$ExpireMin = (Get-Date).AddDays(4);
$ExpireMax = (Get-Date).AddDays(9);

$Userlist = Get-ADUser -Filter * -Properties name, samaccountname, accountexpirationdate, enabled, distinguishedname, accountExpires | Where-object {($_.DistinguishedName -notlike "*OU=Terminated,OU=Users,OU=Home Office,DC=Domain,DC=com")} |
Where-Object {$_.accountExpires -ne $NeverExpires  `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpireMax `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -gt $ExpireMin }

$Userlist |  select name, samaccountname, accountexpirationdate, enabled, distinguishedname | export-csv $ReportName -notypeinformation

Send-MailMessage -To $To -From $From -Subject $Subject -Body $Body -SMTPServer $SMTPServer -Attachments $ReportName 

Get-ADUser -Filter * -Properties accountExpires | 
Where-Object {$_.accountExpires -ne $NeverExpires  `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpireMax `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -gt $ExpireMin } | ForEach { 
      $account = $_ 
      $manager = Get-ADUser -Identity $account -Properties EmailAddress,Manager | %{(Get-AdUser $_.Manager -Properties EmailAddress).EmailAddress}`
Mitul
  • 9,734
  • 4
  • 43
  • 60
user2402045
  • 71
  • 1
  • 3
  • 13
  • I suggest that you write the value `$_.accountExpires` to the output and then inspect that value when you run the script as a standard user. Perhaps that will provide further clues to the root problem. – Keith Hill Sep 27 '13 at 16:24
  • @KeithHill could be that a particular attribute is hidden for normal users? – Mitul Sep 27 '13 at 20:29
  • @Mitul Possibly but I would have expected a ArgumentNullException instead. – Keith Hill Sep 27 '13 at 23:13

1 Answers1

0

I would say that $_.accountExpires is null either because the property could not be retrieved or $_ is itself null. Powershell will quietly convert null to the empty string resulting an invalid format for parsing. Note that the Parse call is completely unnecessary because powershell will automatically try to coerce the string for you and will likely give you a much better error message. Although null will be coerced to 0 as a long.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • I added a write-host "$_.accountExpires" and regardless whether i run as admin or not the output is ".accountExpires". Should i just try removing the Parse line altogether? – user2402045 Sep 30 '13 at 18:02
  • @user2402045 well that suggests $_ might be null, but the syntax is incorrect to print the accountExpires property. Either `write-host $_.accountExpires` (note: no quotes) or `write-host "$($_.accountExpires)"` should work. – Mike Zboray Sep 30 '13 at 18:18
  • I tried it without quotes and nothing shows at all so it appears to prove thats its null. Its odd though that it appears null as admin or user and receive no message when run as admin. – user2402045 Sep 30 '13 at 18:46
  • @user2402045 What is the value of `$ErrorActionPreference` for these two users? If the admin has the value `SilentlyContinue` that would explain why there are no errors shown. – Mike Zboray Sep 30 '13 at 19:27
  • $erroractionpreference is not set when running as my admin or normal account – user2402045 Sep 30 '13 at 20:11
  • @user2402045 I would print out the actual value in several places and make sure it is Continue or Stop. SilentlyContinue hides errors and makes scripts difficult to debug. `$ErrorActionPreference` always has a value. It is a global variable. It might be set in a place you don't expect, like a profile script. – Mike Zboray Sep 30 '13 at 20:33