VMware Cloud Community
deensolea
Contributor
Contributor
Jump to solution

powercli script to get last boottime of guest OS

hi

can I get some help with a script to get last boottime of guest OS (Win7 and 10) for multiple VMs.

thanks

37 Replies
Jegadeesh-mani
Contributor
Contributor
Jump to solution

the script works perfect.. Thank you very much..!! also i am not getting OS information and why i am not sure.

i am looking for some more info like, Last user accessed those VM guests..

can you please help me with that...

0 Kudos
Jegadeesh-mani
Contributor
Contributor
Jump to solution

Thank you very much Marcel..!!

the script works perfect..  i am not getting OS information  in the output and why, i am not sure.

i am looking to add some more info like, Last user who accessed those VM guests..

can you please help me with that...

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution

Unfortunately, I'm a real noob when it comes to scripting. The fact that the script works is thanks to LucD. 😉

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The OS information might be missing because the VMware Tools are not installed inside the Guest OS.

Finding out which users connected to the Guest OS (logon) is something that vSphere doesn't keep track of.
You will have to query inside the Guest OS to find that information.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
technewbie1
Contributor
Contributor
Jump to solution

Hi LucD, is it possible to send an alert through mail, when the uptime date increase a thresold value like 100 days

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use a Where-clause and a Send-MailMessage.
Something like this for example

$stat = 'sys.osuptime.latest'

$now = Get-Date
$vms = Get-VM

$report = Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |
Select @{N='VM';E={$_.Entity.Name}},
    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},
    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}} |
Where{$_.UptimeDays -gt 100}}

if($report){
  $sMail = @{
   From = "from@domain"
   To = "to@domain"
   Subject = "VM with uptime over 100 days"
   Body = $report | Convert-ToHtml | Out-String
   BodyAsHtml = $true
   SMTPServer = "smtp.domain"
 }
 Send-MailMessage @smail
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution

Hi @LucD ,

can you help me again?

I would like to add to the existing script that the tags in the “Backup” category are also output.
But there doesn't seem to be any tags in the extension data, or am I looking wrong?

Here is the existing script:

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers

connect-viserver xxx

connect-viserver xxx

$stat = 'sys.osuptime.latest'

$now = Get-Date

$vms = Get-VM

Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},

    @{N='GuestOS';E={$_.Entity.ExtensionData.Guest.GuestFullName}},

    @{N='Notes';E={$_.Entity.ExtensionData.Summary.Config.Annotation}},

    @{N='VMware Tools State';E={$_.Entity.ExtensionData.Guest.ToolsStatus}},

    @{N='VMware Tools Version';E={$_.Entity.ExtensionData.Guest.ToolsVersion}},

    @{N='CreateDate';E={$_.Entity.CreateDate}},

    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},

    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}},
    
    @{N="DNS";E={$_.Entity.ExtensionData.Guest.IpStack.DnsConfig.IpAddress}} |

Export-Csv -Path .\VM_Uptime_Report.csv -NoTypeInformation -UseCulture
0 Kudos
technewbie1
Contributor
Contributor
Jump to solution

Hi sir, thanks for your reply. Instead of sending as a normal mail. I'm looking to send it as an alarm. whenever the uptime is more than 100 days. i want to send a mail. Hope you can help me

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should use the Get-TagAssignment cmdlet to retrieve Tags assigned to a VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case, add an Alarm Action to the Alarm.
See the New-AlarmAction cmdlet, and there are many samples available in this community


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution


@LucD wrote:

You should use the Get-TagAssignment cmdlet to retrieve Tags assigned to a VM.


I had already tried that, but unfortunately it doesn't work as expected. Unfortunately I'm a PowerShell newbie, can you tell me where the error is?

@{N='Tag';E={(Get-TagAssignment -Category 'Backup').Tag.name}},

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are we talking about a Tag or a Category that is named 'Backup'?
If it is the Category, you could do

@{N='Tag';E={(Get-TagAssignment -Entity $_.Entity -Category 'Backup').Tag.name}},

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution


@LucD wrote:

Are we talking about a Tag or a Category that is named 'Backup'?
If it is the Category, you could do

 

@{N='Tag';E={(Get-TagAssignment -Entity $_.Entity -Category 'Backup').Tag.name}},

 

 

 

Thank you very much, that's how it works now. 🙂
I was “close” to it. ^^
The category is "Backup" and the tags e.g. "Bkp2DiskNoTape"

 

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution

Hello @LucD ,

Maybe you, as a PowerShell professional, can help me again.
I would like to expand "the report" to include the folder path in which the VM is located.

Our paths are always laid out as follows:
"Datacenter\IT-Team\Application"

I would prefer that there be a column “IT team” and another column “Application” in the .csv.
Would it be possible?

Best regards

Marcel

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use a VIProperty I once wrote to retrieve the full blue folderpath.
To avoid having to call the VIProperty twice I used the script scope to store the $path variable.

 

New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
    param($vm)

    function Get-ParentName{
        param($object)

        if($object.Folder){
            $blue = Get-ParentName $object.Folder
            $name = $object.Folder.Name
        }
        elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
            $blue = Get-ParentName $object.Parent
            $name = $object.Parent.Name
        }
        elseif($object.ParentFolder){
            $blue = Get-ParentName $object.ParentFolder
            $name = $object.ParentFolder.Name
        }
        if("vm","Datacenters" -notcontains $name){
            $blue + "/" + $name
        }
        else{
            $blue
        }
    }

    (Get-ParentName $vm).Remove(0,1)
} -Force | Out-Null

#Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers

$stat = 'sys.osuptime.latest'
$now = Get-Date
$vms = Get-VM
Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |
Select @{N='VM';E={$_.Entity.Name}},
    @{N='GuestOS';E={$_.Entity.ExtensionData.Guest.GuestFullName}},
    @{N='Notes';E={$_.Entity.ExtensionData.Summary.Config.Annotation}},
    @{N='VMware Tools State';E={$_.Entity.ExtensionData.Guest.ToolsStatus}},
    @{N='VMware Tools Version';E={$_.Entity.ExtensionData.Guest.ToolsVersion}},
    @{N='CreateDate';E={$_.Entity.CreateDate}},
    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},
    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}},
    @{N="DNS";E={$_.Entity.ExtensionData.Guest.IpStack.DnsConfig.IpAddress}},
    @{N='Tag';E={(Get-TagAssignment -Entity $_.Entity -Category 'Backup').Tag.name}},
    @{N='IT Team';E={
        $script:path = $_.Entity.BlueFolderPath
        $script:path.Split('/')[-2]
    }},
    @{N='Application';E={$script:path.Split('/')[-1]}} |
Export-Csv -Path .\VM_Uptime_Report.csv -NoTypeInformation -UseCulture

 




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution

Hello @LucD ,

wow, what can I say, works perfectly!!!
Thanks alot!

I saw that there is a “Buy LucD a beer” function on your website. I think it's time I bought you one tonight. 😉

Thank you again and I hope you have a nice weekend!

Best regards

Marcel

 

0 Kudos
prabhakarnaidum
Contributor
Contributor
Jump to solution

This looks good, but due to presence of many powered-off VMs we are seeing few errors .. How can we make sure we run for only powered-on VMs

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing the line to

$vms = Get-VM | where{$_.PowerState -eq 'PoweredOn'}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos