简体   繁体   中英

Get computers last logon with powershell

I have found the following script, but not able to get the expected result: computer, lastlogon. Using Windows 10. For some reason I am getting this error:

Get-ADComputer : Variable: 'NTName' found in expression: $NTName is not defined.
At line:10 char:19
+ ... astLogon = (Get-ADComputer -Filter {Name -eq $NTName} -Properties Las ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
 

And a random number of dates and times: 04/11/2017 07:16:44 04/26/2017 01:08:31 04/26/2017 22:33:51 05/01/2017 07:05:13 04/24/2017 09:01:12 04/25/2017 06:36:43 08/21/2020 21:27:23 01/11/2018 11:47:14 12/27/20 20 14:37:44 01/02/2021 11:24:41 01/01/2021 13:48:16 12/31/2020 14:12:24 01/01/2021 06:57:39 01/05/2021 14:14:14 01/05/2021 21:04:12 01/06/2021 04:37:42 10/13/2015 22:53:5 2 01/01/2015 17:58:55 11/04/2015 04:02:00 06/02/2015 18:46:48 03/23/2011 05:21:05 01/11/2013 23:59:16 01/10/2012 09:46:40 10/16/2015 22:11:38 10/16/2015 22:05:24 04/24/20 18 12:12:35 07/26/2017 13:50:14 04/24/2012 14:22:04 11/17/2014 17:44:11 05/18/2016 13:52:34 03/15/2015 08:52:39 07/21/2016 08:46:35 11/19/2015 06:37:05 02/26/2015 11:50:5 0 02/23/2015 20:03:49 01/22/2015 10:49:49 12/08/2015 14:52:41 02/17/2017 11:05:13 09/08/2015 13:11:49 05/24/2015 13:17:40 05/12/2015 00:51:28 11/05/2015 13:44:19 10/28/20 15 23:28:23 07/23/2015 18:28:34 11/17/2015 17:29:39 10/24/2018 23:43:50 02/15/2016 10:15:05 04/07/2015 10:0 8:14 02/07/2019 06:07:01 07/26/2016 16:19:09 08/25/2015 12:08:1 9 10/25/2018 05:26:48 03/07/2015 03:30:19 06/10/2015 12:00:27 02/20/2015 10:15:37 08/04/2015 08:48:43 04/14/2015 05:58:17 08/26/2015 16:10:23 02/20/2017 21:02:25 03/14/20 18 14:15:23 08/25/2015 16:45:42 07/09/2015 05:12:25 03/02/2015 13:18:07 04/14/2015 05:37:20 04/22/2015 03:42:14 09/14/2015 13:51:48

# Specify CSV file of computer names.
$File = ".\Computers.csv"

$Computers = Import-Csv -Path $File

ForEach ($Computer In $Computers)
{
    $NTName = $Computer.Name
    # Retrieve last logon date of the computer (accurate with 14 days).
    $LastLogon = (Get-ADComputer -Filter {Name -eq $NTName} -Properties LastLogonDate).LastLogonDate
    "$NTName, $LastLogon" 

Give this a shot and see if it works.

Please note: ensure that there is a column in the CSV with the first block actually being "Name" . That's where $Computers1.Name references for the computer names in that column.

#I would recommend putting the whole file path to your csv here and not just '.\'
#This is the location of your CSV
$File = ".\Computers.csv"

#Here you're importing the CSV into your console and storing it into the $Computers1 variable.
$Computers1 = Import-Csv -Path $File

#Here youre using the $Computers variable  to store the computer names being referenced in the "Name" column in your CSV, hence the $Computers1.Name.
#$Computers1.Name like mentioned above, this is just referencing the column beginning with "Name" (assuming this is where the computer names are)
$Computers = $Computers1.Name

#Here youre assigning the variable $Computer to each Computer name stored in $Computers and telling it to do the following FOR EACH Computer in Computers.
ForEach ($Computer In $Computers)
{



    #Here you're just querying AD against the list of computers with it referencing $Computer as each computer(like mentioned above), and storying the result in $LastLogon.
    $LastLogon = Get-ADComputer -Identity:$Computer -Property LastLogonDate | Select-Object -ExpandProperty LastLogonDate
    # Retrieve last logon date of the computer (accurate with 14 days).

    
    #This is just outputting to your console screen your computer name($Computer) and, the result for your Last Logon query ($LastLogon).
    "$Computer - $LastLogon"

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM