Author Posts

July 1, 2016 at 4:38 pm

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

July 1, 2016 at 5:31 pm

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

July 4, 2016 at 10:27 am

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.