PowerCLI

 View Only
  • 1.  Last login details from multiple VMs

    Posted Sep 28, 2021 12:03 PM

    Hi All,

    I wanted to decommission some of the Win7 VMs, these VMs are being used by the end user via RDP (remote desktop connection) directly. Now I am not having any reporting tool in place so wanted to check whether the VMs has been used in past or not and who did logged in last (Just one user ID entry). Is there any script / code I can achieve this please.



  • 2.  RE: Last login details from multiple VMs

    Posted Sep 28, 2021 12:32 PM

    Not with PowerCLI afaik.
    These logins are to the Guest OS, there are no logs on the VCSA about these logins.



  • 3.  RE: Last login details from multiple VMs

    Posted Sep 29, 2021 08:56 AM

    Hi Luc.

    I did found a code from which I can get the last write time of user profile folder from C drive and this is what I need to know who used the VMs last time. But I cant get the details for multiple VMs. if I put the multiple VMs its give the error (screen shot attached).  but when I put only single name in the txt file it ran successfully. Also In the output I am not getting the VMs name (Screen shot attached). Is there any way you can help with this pls.

    $LastLogin = $login
    $computer = Get-Content -path "F:\Last_Login\VM.txt"
    $Login = Get-ChildItem "\\$computer\c$\Users" | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -first 1
    $LastLogin | Export-CSV "F:\\Last_Login\Lastlogins.csv" -NoTypeInformation -Encoding UTF8

     

    Output - VMs name not there in Output file..

    system_noida_0-1632905503336.png

    Error when I put multiple Name in VM.txt file

    system_noida_3-1632905728642.png

     

     

     

     



  • 4.  RE: Last login details from multiple VMs
    Best Answer

    Posted Sep 29, 2021 09:42 AM

    That is limited to Windows Guest OS and only if the Administrative share is open, but if it works for you good.

    You have to loop through the VM names, provided the VM name corresponds with the NetBIOS name of the Guest OS.
    Something like this

    Get-Content -path "F:\Last_Login\VM.txt" -PipelineVariable vm |
    ForEach-Object -Process {
        Get-ChildItem "\\$($vm)\c`$\Users" | 
        Sort-Object LastWriteTime -Descending | 
        Select-Object @{N='VM';E={$vm}},Name, LastWriteTime -First 1
    } | Export-CSV "F:\\Last_Login\Lastlogins.csv" -NoTypeInformation -Encoding UTF8


  • 5.  RE: Last login details from multiple VMs

    Posted Sep 29, 2021 10:54 AM

    Hi Luc,

    I did tried with this code. its ran and generated the output, but last i did see an error. not sure if anything need to be done to fix this error.

    Error:..

    system_noida_0-1632912616575.png

     Output file....

    system_noida_2-1632912804247.png

     

     



  • 6.  RE: Last login details from multiple VMs

    Posted Sep 29, 2021 12:43 PM

    Looks like you might have a blank/empty line at the end of the input file.



  • 7.  RE: Last login details from multiple VMs

    Posted Sep 30, 2021 04:18 AM

    HI Luc,  Yes it was having a blank line, Thank you very much for your help on this mate. You are super !!!!