代码错误"在哈希文字中键入后缺少=运算符,哈希文字不完整"

时间:2017-04-04 14:26:11

标签: windows powershell active-directory

我有一个CSV文件中的用户列表,我想知道他们上次登录的时间是什么时候。我可以获取个人帐户的这些信息,但我需要将其写入多个帐户的文件中。 / p>

我得到的错误:

At line:5 char:7 
import-module activedirectory
Missing = operator after key in hash literal
hash literal was incomplete

我的代码:

$resultList = @()
Import-Csv C:\Users\admin\Desktop\SamAccountName.csv -header("SamAccountName") | Foreach-Object{
$user = ([adsisearcher]"(samAccountName=$($_.SamAccountName))").FindOne()
$resultList += New-Object -TypeName PSObject -Property @{
           SamAccountName = $_.SamAccountName

Import-Module ActiveDirectory
function Get-ADUserLastLogon([string]$_.userName)
{
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $time = 0
  foreach($dc in $dcs)
  { 
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
    if($user.LastLogon -gt $time) 
    {
      $time = $user.LastLogon
    }
  }
  $dt = [DateTime]::FromFileTime($time)
  Write-Host $username "last logged on at:" $dt }

Get-ADUserLastLogon -UserName $user
                        }
       }
}

$resultList | export-csv -Path c:\users\admin\desktop\SamAccountName_results.csv -NoTypeInfo

1 个答案:

答案 0 :(得分:2)

而不是:

$user = ([adsisearcher]"(samAccountName=$($_.SamAccountName))").FindOne()
$resultList += New-Object -TypeName PSObject -Property @{
           SamAccountName = $_.SamAccountName

请这样做:

$user = ([adsisearcher]"(samAccountName=$($_.SamAccountName))").FindOne()
$resultList += New-Object -TypeName PSObject -Property @{
           SamAccountName = $_.SamAccountName
           }