Powershell के साथ अंतिम लॉगऑन समय, कंप्यूटर और उपयोगकर्ता नाम प्राप्त करें


0

मेरे पास एक स्क्रिप्ट है जो डोमेन में प्रत्येक कंप्यूटर का अंतिम लॉगऑन समय प्राप्त करता है।

मेरी स्क्रिप्ट:

$dcs = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem

foreach($dc in $dcs) { 
    Get-ADComputer $dc.Name -Properties lastlogontimestamp | 
    Select-Object @{n="Computer";e={$_.Name}}, @{Name="Lastlogon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
}

==================================

परिणाम:

कंप्यूटर Lastlogon
-------- ---------
DC1 6/06/2013 16:38:24
DC2 6/06/2013 16:30:40

=============================================

मैं यह भी जानना चाहता हूं कि इस लॉगऑन को किसने / किस खाते से बनाया है। उदाहरण के लिए:

कंप्यूटर Lastlogon उपयोगकर्ता
-------- ------------------ ----
DC1 6/06/2013 16:38:24 उपयोगकर्ता 2
DC2 6/06/2013 16:30:40 उपयोगकर्ता 1

जवाबों:


1

यह सही नहीं हो सकता है, लेकिन यह आपको सही रास्ते पर ले जाएगा। सटीक अंतिम उपयोगकर्ता प्राप्त करने के लिए, कृपया इसे देखें लिपि । यह आपको सटीक अंतिम उपयोगकर्ता को फ़िल्टर करने के तरीके के बारे में और जानकारी देगा। नीचे दिए गए उदाहरण में, मैंने उपयोग किया है, चयन-ऑब्जेक्ट -First 1 जो उपयोगकर्ता पर अंतिम लॉग का एक बहुत अच्छा संकेतक होना चाहिए। उपयोगकर्ता पर अंतिम लॉग इन करने के लिए, आपको उपयोग करने की आवश्यकता है

Get-WmiObject -Class Win32_UserProfile

Get-ADComputer और Get-WMIObject जानकारी को 'जॉइन' करने के लिए, मैंने एक हैश टेबल का उपयोग किया है।

यदि आप इसे किसी डोमेन व्यवस्थापक खाते से चला रहे हैं, तो आप -credential $ क्रेडेंशियल भाग को बाहर निकाल सकते हैं। अन्यथा, इसे अंदर छोड़ दें और आप इसे सामान्य कार्य केंद्र से आरएसएटी उपकरणों के साथ चला सकते हैं ताकि गेट-एडीकॉम उपलब्ध हो।

नीचे कोड:

$computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem
$credential = Get-Credential -Message "Please enter your administrator username and password"

foreach($computer in $computers) { 
    $pcinfo = Get-ADComputer $computer.Name -Properties lastlogontimestamp | ` 
              Select-Object @{n="Computer";e={$_.Name}}, ` 
             @{Name="Lastlogon";Expression[DateTime]::FromFileTime($_.lastLogonTimestamp)}}

    $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
    $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
    $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

    # Create hashtable for properties
    $properties = @{'Computer'=$pcinfo.Computer;
                    'LastLogon'=$pcinfo.Lastlogon;
                    'User'=$username.value
                   } #end $properties
    write-output (New-Object -Typename PSObject -Property $properties)
}

जब आप इसका उपयोग करते हैं तो फ़ॉर्मेटिंग की जाँच करें। इसमें से कुछ को मुझे स्क्रिप्ट विंडो में फिट करने के लिए एक एस्केप (`) चरित्र में जोड़ना पड़ा।

धन्यवाद, टिम।


0

यह टिम का कोड है और मैंने इसे कंप्यूटर के आईपी पते को प्रदर्शित करने के लिए अपडेट किया था & amp; ऑपरेटिंग सिस्टम, और डेटा निर्यात करें।

 $computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*'} -Properties OperatingSystem

 foreach($computer in $computers) { 
     $pcinfo = Get-ADComputer $computer.Name -Properties ipv4Address, OperatingSystem ,lastlogontimestamp | 
               Select-Object @{n="Computer";e={$_.Name}}, 
              @{Name="Lastlogon";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},ipv4Address,OperatingSystem

     $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
     $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
     $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

     # Create hashtable for properties
     $properties = @{'Computer'=$pcinfo.Computer;
                     'LastLogon'=$pcinfo.Lastlogon;
                     'ipv4Address'=$pcinfo.ipv4Address;
                    'OperatingSystem'=$pcinfo.OperatingSystem
                     'User'=$username.value
                    } #end $properties
     write-output (New-Object -Typename PSObject -Property $properties) |
     export-csv .\Computers.csv -append -notypeinformation -encoding "unicode"         
            }

Powershell Get-ADComputer गुण अभिव्यक्ति


कृपया अपना कोड जांचें। क्या इसमें बैक-टिक होना चाहिए? संकेत: ब्लॉकक्वाओट के बजाय कोड नमूना प्रारूपण का उपयोग करें।
— G-Man

कोड ब्लॉक करने के लिए अद्यतन किया गया
— Frank
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.