Get the last user login on computer

0

When I list an array to get only the last one user I have a problem when an array is empty. I must get a name of last user login on the computer with other properties.

I tried don't list an array, but then I get in CSV an object who doesn't have any value for me. I want to try to replace the NULL value, but... I don't have an idea of how. I get a null array of $username.lastwritetime[0] $username.name[0] because I don't have access to all computers (few exceptions) and I can't change it.

Get-ADComputer -Filter 'name -like $department' -Server $domain | ForEach-Object {
  if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $_.Name -Quiet) {
    #psexec -s \\$_.IPv4Address powershell.exe "winrm quickconfig -force -q"
    #$lastlogondate = Get-ADComputer -Identity $_.name -Properties LastLogonDate 
    #Invoke-Command -ComputerName $_.Name -ScriptBlock {wmic bios get serialnumber}
    $computer = echo $_.name 
    #Write-Host $computer
    $username = Get-ChildItem "\\$computer\c$\Users" | Sort-Object lastwritetime -Descending | Select-Object name, lastwritetime
    $bios = Get-WmiObject win32_bios -ComputerName $computer

    $systembios = $bios.serialnumber

    $outputObj = New-Object -type psobject
    $outputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $systembios 
    $outputObj | Add-Member -MemberType NoteProperty -Name LastUser -Value $username.name[0] 
    $outputObj | Add-Member -MemberType NoteProperty -Name LastLoginUser -Value $username.lastwritetime[0] 
    $outputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer 
    #$outputObj | Add-Member -MemberType NoteProperty -Name LastLogonDate -Value $lastlogondate.lastLogonDate -force
    $outputObj | Export-Csv -LiteralPath $pathcsv -Append -NoTypeInformation 

  }
}

I get CSV without 2 column or useless 2 columns when I don't have access to one of the PC, but when I have access to all PC what this script use I get nice CSV.

Error:

Get-ChildItem : Cannot find path '\\ThisComputerWhereIdonthaveAccess\c$\Users' because it does not exist.
At C:\Scripts\SerialNumber.ps1:12 char:17
+     $username = Get-ChildItem "\\$computer\c$\Users" | Sort-Object la ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\PL04DIT02\c$\Users:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scripts\SerialNumber.ps1:13 char:13
+     $bios = Get-WmiObject win32_bios -ComputerName $computer
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Cannot index into a null array.
At C:\Scripts\SerialNumber.ps1:19 char:5
+     $outputObj | Add-Member -MemberType NoteProperty -Name LastUser - ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Scripts\SerialNumber.ps1:20 char:5
+     $outputObj | Add-Member -MemberType NoteProperty -Name LastLoginU ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
powershell
cmd
asked on Stack Overflow Jul 29, 2019 by Plazma

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0