Getting Lastaccess on the server through Last write time

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of gautam panday gautam panday 5 months ago.

  • Author
    Posts
  • #45542
    Profile photo of gautam panday
    gautam panday
    Participant

    MY command is very basic and i want to get the last access information on the server through last write time as get-lastlogon on the server is not helping me to get the correct last access on the sever.

    #############################################################################################
    $listofdeviceids = get-WmiObject win32_logicaldisk | Select DeviceID,DriveType | where {$_.DriveType -notlike "5"}
    foreach ($DeviceId in $listofdeviceids)
    {
    Get-ChildItem -Recurse $DeviceId | ?{ $_.PSIscontainer} | select FullName,LastWriteTime | sort Lastwritetime,FullName | ft lastwritetime,FullName | out-file -filepath "C:\Test\Output\lastaccessfiledetails.txt" -Append
    }
    ###################################################################################################
    I am receiving below error post executing the above command .
    #######################################################################################################
    Get-ChildItem -Recurse $DeviceId | ?{ $_.PSIscontainer} | select FullName,LastW ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (@{DeviceID=Q:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Get-ChildItem : Cannot find drive. A drive with the name '@{DeviceID=V' does not exist.
    At line:4 char:2
    + Get-ChildItem -Recurse $DeviceId | ?{ $_.PSIscontainer} | select FullName,LastW ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (@{DeviceID=V:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Get-ChildItem : Cannot find drive. A drive with the name '@{DeviceID=Z' does not exist.
    At line:4 char:2
    + Get-ChildItem -Recurse $DeviceId | ?{ $_.PSIscontainer} | select FullName,LastW ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (@{DeviceID=Z:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    ####################################################################################################################
    Please suggest .. where i am doing wrong

  • #45547
    Profile photo of random commandline
    random commandline
    Participant

    Try this. Get-childitem accepts strings not objects. Sending output to a text file will truncate long folder/file paths. Use of format cmdlets should always be at the end of your pipeline.

    $drives = Get-WmiObject win32_logicaldisk -Filter "DriveType != 5"
    
    $results = foreach ($drive in $drives){
    Get-ChildItem -Recurse -Directory | Select-Object FullName,LastWriteTime |
    Sort-Object Lastwritetime,FullName}
    
    $results | export-csv "C:\Test\Output\lastaccessfiledetails.csv" -NoTypeInformation
    
  • #45645
    Profile photo of gautam panday
    gautam panday
    Participant

    To Random Cmdlines,
    Thanks for reply..

    I think it wont check each and every drives of the server. It will only check the current drive where this script is getting executed. Can you please suggest .. I require a last write time of the server after checking every drives.

You must be logged in to reply to this topic.