异常调用" Parse"带有1个参数:输入字符串的格式不正确

时间:2013-09-27 14:15:55

标签: parsing powershell linefeed

我有一个脚本,当以管理员身份运行时可以正常工作,但在以普通用户身份运行时会出现Parse错误。任何想法?; 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}`

1 个答案:

答案 0 :(得分:0)

我会说$_.accountExpires为空,因为无法检索属性或$_本身为空。 Powershell会悄悄地将null转换为空字符串,从而导致解析格式无效。请注意,Parse调用是完全没必要的,因为powershell会自动尝试强制为您提供字符串,并且可能会给您一个更好的错误消息。虽然null将被强制为0作为长整数。