Powershell腳本不回傳使用者上次登入時間

Powershell腳本不回傳使用者上次登入時間

希望有人可以解決我一直在搞亂的一段簡單程式碼的問題。首先,我要聲明我不是程式設計師,也從未真正做過多少 powershell 工作。

問題是,一開始,這是有效的,按預期返回 LastLogonTimeStamp。

現在,當我運行它時,我在該列中根本沒有任何輸出。

我很確定這是我忽略的愚蠢的事情,但我無法弄清楚。

就像我說的 - 我確實沒有這方面的經驗 - 我不知道一半的程式碼意味著什麼。

有人可以幫我嗎?

    # Script to list member of VDI Desktop Users Group
    # and export details to c:\VDIlastlogon.csv file
    # [email protected] 24/11/14'

    # Function get-NestedMembers
    # List the members of a group including all nested members of subgroups

    Import-Module ActiveDirectory

    function get-NestedMembers ($group){
      if ($group.objectclass[1] -eq 'group') {
    write-verbose "Group $($group.cn)"
        $Group.member |% {
          $de = new-object directoryservices.directoryentry("LDAP://$_")
          if ($de.objectclass[1] -eq 'group') {
    get-NestedMembers $de
  }
  Else {
    $de
          }
        }
      }
      Else {
        Throw "$group is not a group"
      }
    }

    # get a group

    $group = new-object directoryservices.directoryentry("LDAP://CN=VDI Desktop Users,ou=Groups,ou=x,ou=uk,dc=uk,dc=x,dc=com")

    # Get all nested members and send to CSV file

    get-NestedMembers $group|FT @{l="First Name";e={$_.givenName}},@{l="Last Name";e={$_.sn}},@{l="Last Logon";e={[datetime]::FromFileTime($_.ConvertLargeItegerToInt64($_.lastLogonTimestamp[0]))}},sAMAccountName | tee c:\VDILastLogon.csv

    #Send CSV file to mail recipient

    $PSEmailServer = "mail.x.net"
    $smtpServer = "mail.x.net"
    $file = "c:\VDILastLogon.csv"
    $att = new-object Net.Mail.Attachment($file)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient ($smtpServer)
    $msg.From = "[email protected]"
    $msg.To.Add("[email protected]")
    $msg.Subject = "User logon report from VDI Solution"
    $msg.Body = "Please find attached the most recent user logon report"
    $msg.Attachments.Add($att)
    $smtp.Send($msg)
    $att.Dispose()

答案1

如果匯入 AD powershell 模組,則不需要使用額外的目錄服務物件(至少在這種情況下不需要)。您可以使用Get-ADGroupMembercmdlet,-Resursive它也應該找到您的巢狀使用者。

編輯:我-Server為 AD cmdlet 新增了參數,以便您可以指定特定的 DC。時間戳屬性可能有所不同(在我的 12 個 DC 中也是如此)。查看這個部落格為了寫出一篇像樣的文章。

這獲取了最後登入時間並且更容易閱讀:

$groupname = "name_of_AD_group"

Import-Module ActiveDirectory

Get-ADDomainController -Filter * | % {
   $DC = $_
   $group = Get-ADGroup -Identity $groupname -Server $DC.Name -ErrorAction SilentlyContinue
   If ($group) {
      $members = Get-ADGroupMember -Identity $group.Name -Recursive -Server $DC.Name -ErrorAction SilentlyContinue
      ForEach ($member In $members) {
         If (-not $member.objectClass -ieq "user") { Continue }
         $user = Get-ADUser $member.SamAccountName -Server $DC.Name -ErrorAction SilentlyContinue
         If ($user) {
            $lastlogon = ($user | Get-ADObject -Properties lastLogon).LastLogon
            New-Object PSObject -Property @{
               "First Name" = $user.GivenName
               "Last Name"  = $user.Surname
               "DC"         = $DC.Name
               "Last Logon" = [DateTime]::FromFileTime($lastlogon)
               "SamAccountName" = $user.SamAccountName
            }
         } Else {
            # $user not found on $DC
         }
      }
   } Else {
      # $groupname not found on $DC
   }
} | ft -auto

答案2

這是一個 hack,但它確實有效。從 Microsoft 文章中取得了 Get-ADUserLastLogon (http://technet.microsoft.com/en-us/library/dd378867%28v=ws.10%29.aspx

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)
  return $dt 
}

function get-NestedMembers ($group){
  if ($group.objectclass[1] -eq 'group') {
write-verbose "Group $($group.cn)"
    $Group.member |% {
      $de = new-object directoryservices.directoryentry("LDAP://$_")
      if ($de.objectclass[1] -eq 'group') {
get-NestedMembers $de
  }
  Else {
$de
      }
    }
  }
  Else {
    Throw "$group is not a group"
  }
}

# get a group

$group = new-object directoryservices.directoryentry("LDAP://CN=Domain Users,CN=Users,DC=yourdomain,DC=com")
# Get all nested members and send to CSV file
get-NestedMembers $group|FT @{l="First Name";e={$_.givenName}},@{l="Last Name";e={$_.sn}},@{l="Last Logon";e={Get-ADUserLastLogon($_.sAMAccountName)}},sAMAccountName | tee c:\VDILastLogon.csv

相關內容