Automation

 View Only
  • 1.  How to get the last name of the user using the virtual machine?

    Posted Dec 21, 2015 09:26 AM

    I have over 1500 VM in 14 hosts. I need for each virtual machine, find the name of the last user to use this virtual machine (except for an empty record and administrators).

    my script

    $myCol = @()

    $ESXHosts = Get-VMHost | select -Exp Name

    foreach ($ESXHost in $ESXHosts){

         $VMs = Get-VM

              foreach ($VM in $VMs){

                   $VIe =$vm | Get-VIEvent | Where-Object {$_.userName -ne ""`

                     -and $_.userName -ne "User" -and $_.userName -ne "vpxuser"`

                     -and $_.userName -ne "admin1" -and $_.userName -ne "admin2"`

                     -and $_.userName -ne "admin3" -and $_.userName -ne "admin4"`

                     -and $_.userName -ne "admin5"} | select -Exp UserName

    $Details = "" | Select ESXHost,VMName,UserName

                   

                $Details.ESXHost = $ESXHost

                $Details.VMName = $VM.Name

                $Details.UserName = $VIe[0] | Out-String

           

                $myCol += $Details           

                }

    $myCol | Export-Csv -Delimiter "`t" -Path ...

    But this run very slow... script worked two days

    how to speed up the process?



  • 2.  RE: How to get the last name of the user using the virtual machine?

    Posted Dec 21, 2015 10:02 AM

    Your script is looking at any event that was triggered for each VM.

    Is that what you actually define as "last used" ?

    If you have more specific events to report on, you could use my Get-ViEventPlus function (see Get the vMotion/svMotion history).

    For example if you would be looking at the last power on for each VM over the last 7 days, you could do something like.

    Note to look over the complete timespan you keep events, you can just leave out the Start parameter.

    Get-VIEventPlus -EventType VmPoweredOnEvent -Start (Get-Date).AddDays(-7) |

    Sort-Object -Property CreatedTime,Vm.Name,UserName -Descending -Unique |

    Group-Object -Property {$_.Vm.Name} | %{

        $_.Group | Sort-Object -Property CreatedTime -Descending |

        Select -First 1 CreatedTime,@{N='VM';E={$_.Vm.Name}},UserName

    }



  • 3.  RE: How to get the last name of the user using the virtual machine?

    Posted Dec 21, 2015 01:01 PM

    Thx, I see how much time is executed

    Full version

    function Get-VIEventPlus { ... }

    $name="1. Ann1"

    $name1="2. Ann2"

    $name2="3. Ann3"

    $name3="4. Ann4"

    $myCol = @()

    $ESXHosts = Get-VMHost | select -Exp Name

    foreach ($ESXHost in $ESXHosts){

        $RPs = Get-ResourcePool -Location $ESXHost | ?{$_.Name -ne "Resources"}

        $VMs = Get-VM -Location $ESXHost

        foreach ($VM in $VMs){

            $Ann = $vm | Get-Annotation -CustomAttribute $name, $name1, $name2 ,$name3

            $VIe =  Get-VIEventPlus -Entity $VM -EventType VmPoweredOnEvent | Where-Object {$_.userName -ne ""`

                -and $_.userName -ne "User" -and $_.userName -ne "vpxuser"`

                -and $_.userName -ne "admin1" -and $_.userName -ne "admin2"`

                -and $_.userName -ne "admin3" -and $_.userName -ne "admin4"`

                -and $_.userName -ne "admin5"} |

            Sort-Object -Property CreatedTime,Vm.Name,UserName -Descending -Unique |

            Group-Object -Property {$_.Vm.Name} | %{

                $_.Group | Sort-Object -Property CreatedTime -Descending  |

                Select -First 1 UserName}

    $Details = "" | Select ESXHost,ResourcePool,VMName,$name,$name1,$name2,$name3,UserName

                  

                $Details.ESXHost = $ESXHost

                $Details.ResourcePool = $VM.ResourcePool.Name

                #$Details.VMName = $Ann.AnnotatedEntity[0] | select -Exp Name

                $Details.VMName = $VM.Name

                $Details.$name = $Ann.value[0]

                $Details.$name1 = $Ann.value[1]

                $Details.$name2 = $Ann.value[2]

                $Details.$name3 = $Ann.value[3]

                $Details.UserName = $VIe.userName | Out-String

          

                $myCol += $Details          

        }

    }

    $myCol | Export-Csv

    P.s. How to insert the text of the script with the release of color?



  • 4.  RE: How to get the last name of the user using the virtual machine?

    Posted Dec 21, 2015 03:37 PM


  • 5.  RE: How to get the last name of the user using the virtual machine?
    Best Answer

    Posted Dec 22, 2015 08:27 AM

    Resolved.
    The script works only 4 hours.

    In the first script, I forgot to specify the "-Location $ESXHost",

      

      

    cls

    $watch = [System.Diagnostics.Stopwatch]::StartNew()
    $watch.Start() #Start time
    $ErrorActionPreference = "SilentlyContinue"

    $name="Ann 1"
    $name1="Ann 2"
    $name2="Ann 3"
    $name3="Ann 4"

    $myCol = @()
    $ESXHosts = Get-VMHost | select -Exp Name
    foreach ($ESXHost in $ESXHosts){
       
    $RPs = Get-ResourcePool -Location $ESXHost | ?{$_.Name -ne "Resources"}
       
    $VMs = Get-VM -Location $ESXHost # - in the first script I forgot to specify
            foreach ($VM in $VMs){   
               
    $Ann = $vm | Get-Annotation -CustomAttribute $name, $name1, $name2 ,$name3
               
    $VIe =$vm | Get-VIEvent | Where-Object {$_.userName -ne ""`
               
    -and $_.userName -ne "User" -and $_.userName -ne "vpxuser"`
               
    -and $_.userName -ne "admin1" -and $_.userName -ne "admin2"`
               
    -and $_.userName -ne "admin3" -and $_.userName -ne "admin4"`
               
    -and $_.userName -ne "admin5"} | select -First 1 -Exp UserName
           
               
    $Details = "" | Select ESXHost,ResourcePool,VMName,$name,$name1,$name2,$name3,UserName
                   
               
    $Details.ESXHost = $ESXHost
               
    $Details.ResourcePool = $VM.ResourcePool.Name
               
    $Details.VMName = $VM.Name
               
    $Details.$name = $Ann.value[0]
               
    $Details.$name1 = $Ann.value[1]
               
    $Details.$name2 = $Ann.value[2]
               
    $Details.$name3 = $Ann.value[3]
               
    $Details.UserName = $VIe | Out-String

               
    $myCol += $Details           
            }
         
    Write-Host $ESXHost

    }

    $myCol | Export-Csv -Delimiter "`t" -Path C:\PowerCLI\Report\Annotation.csv -encoding "unicode" –NoTypeInformation
    $watch.Stop() #Stop time
    Write-Host $watch.Elapsed #Run time