VMware Cloud Community
newwenha
Contributor
Contributor

Help with script to see last modified time on VM based on vmx file?

My script is 95% functional but there is one problem I am not sure how to work around. Currently, my script gets a list of powered off VMs in a vCenter and then determines the last modified date based on the vmx file in the datastore and then exports it into a .csv file. It runs fine but when it encounters multiple VMs with the same name, it leaves them blank because it cannot differentiate which one to look at. For example, a VM called VM1 has 2 instances. The script will see both instances but does not know where to look at for the corresponding vmx file. How can I achieve this?

add-pssnapin VMware.VimAutomation.Core

# ---------- Only modify the fields in this area -------------

$vCenter = 'qlab-copsmgr'                  #name of the vCenter

$dataCenter = 'Fly-away Kit'               #name of the DataCenter

$outputFile = 'VMLastUsed.csv'             #desired output file name

# ---------- No modification is needed in the below code. Do not edit -------------

$columnName = "Name,DataStore,Date Last Used" | Out-File .\$OutputFile -Encoding ascii

Connect-VIServer $vCenter -WarningAction SilentlyContinue

$vmList = Get-VM | where { $_.PowerState -eq “PoweredOff”} | select Name

$vmList = $vmList -replace 'Name : ', '' -replace '@{Name=', '' -replace '}', ''

ForEach ($VM in $vmList)

{

    # Get configuration and path to vmx file

    $VMconfig = Get-VM $VM | Get-View | select config

    $VMXpath = $VMconfig.config.files.VMpathName

    # Remove and/or replace unwanted strings

    $VMXpath = $VMXpath -replace '\[','' -replace '\] ','\' -replace '@{Filename=','/' -replace '}','' -replace '/','\'

    # List the vmx file in the datastore

    $VMXinfo = ls vmstores:\$VCenter@443\$DataCenter\$VMXpath | Where {$_.LastWriteTime} | select -first 1 | select FolderPath, LastWriteTime

    # Remove and/or replace unwanted strings

    $VMXinfo = $VMXinfo -replace 'DatastoreFullPath=', '' -replace '@{', '' -replace '}', '' -replace ';', ',' -replace 'LastWriteTime=', ''

    # Output vmx information to .csv file

    $output = $VM + ', ' + $VMXinfo

    $output

    echo $output >> $OutputFile

}

# Red text errors indicates duplicate VMs with identical names. vmx file is unable to be differentiated

26 Replies
Sreejesh_D
Virtuoso
Virtuoso

please try with this code.

# ---------- Only modify the fields in this area ------------- 

$vCenter = 'qlab-copsmgr'   #name of the vCenter 

$dataCenter = 'Fly-away Kit'   #name of the DataCenter 

$outputFile = 'VMLastUsed.csv'   #desired output file name 

 

# ---------- No modification is needed in the below code. Do not edit ------------- 

$columnName = "Name,DataStore,Date Last Used" | Out-File .\$OutputFile -Encoding ascii 

Connect-VIServer $vCenter -WarningAction SilentlyContinue 


get-vm | where { $_.PowerState -eq “PoweredOff”} | %{


$VMconfig = $_ | get-view | select config

$VMXpath = $VMconfig.config.files.VMpathName


   # Remove and/or replace unwanted strings 

   $VMXpath = $VMXpath -replace '\[','' -replace '\] ','\' -replace '@{Filename=','/' -replace '}','' -replace '/','\' 

 

   # List the vmx file in the datastore 

   $VMXinfo = ls vmstores:\$VCenter@443\$DataCenter\$VMXpath | Where {$_.LastWriteTime} | select -first 1 | select FolderPath, LastWriteTime 

 

   # Remove and/or replace unwanted strings 

   $VMXinfo = $VMXinfo -replace 'DatastoreFullPath=', '' -replace '@{', '' -replace '}', '' -replace ';', ',' -replace 'LastWriteTime=', '' 

 

   # Output vmx information to .csv file 

   $output = $VM + ', ' + $VMXinfo 

   $output

}

0 Kudos
newwenha
Contributor
Contributor

Can you please attach it? I'm having trouble copy and pasting it.

0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

please find the script attached

0 Kudos
newwenha
Contributor
Contributor

I'm getting an error when I run it, what about on your end?

0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

please post the error. i havent executed the full script. I've just modified the following part. this will make sure that one VM details is extracted at a time.

get-vm | where { $_.PowerState -eq “PoweredOff”} | %{

$VMconfig = $_ | get-view | select config

$VMXpath = $VMconfig.config.files.VMpathName

0 Kudos
newwenha
Contributor
Contributor

At C:\Users\nha\Desktop\test-script.ps1:10 char:48

+ get-vm | where { $_.PowerState -eq "PoweredOff"} | %''

+                                                ~

Unexpected token '' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken

0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

please post the script. looks like there is a " instead of {

get-vm | where { $_.PowerState -eq “PoweredOff”} | %{

0 Kudos
newwenha
Contributor
Contributor

The script is exactly what you uploaded

0 Kudos
LucD
Leadership
Leadership

Give the attached script a try


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

newwenha
Contributor
Contributor

I tried to run the script and it connects to vCenter but nothing happens after that. There is no csv generated nor is anything appearing on the console window. I'm not too sure why.

0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

the outputfile should be defined with the variable $outputFile

like following.

$outputFile = "C:\test.csv"

0 Kudos
LucD
Leadership
Leadership

The output is stored in a file named VMLastUsed.csv.

That should be located in the folder where your PS prompt points to.


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

0 Kudos
newwenha
Contributor
Contributor

I'm running the .ps1 file as is and there is no output.

0 Kudos
LucD
Leadership
Leadership

The output is saved in a CSV file.

What does your PowerShell prompt show?

The file is in that folder.


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

0 Kudos
newwenha
Contributor
Contributor

It shows my desktop (the .ps1 is on my desktop too). But there is no file being created. Anyhow, I have removed some specifications and have emailed you via your website. Please let me know if you are able to respond.

0 Kudos
LucD
Leadership
Leadership

But how do you start the .ps1 file? By double-clicking the shortcut on your desktop?


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

0 Kudos
newwenha
Contributor
Contributor

Right click -> Edit (which opens up in Powershell IDE) -> Run (green button)

0 Kudos
LucD
Leadership
Leadership

Ok, in the IDE, at the bottom (normally), there is a dark blue area, the console.

The prompt that is shown, should be something like: PS C:\Users\Luc

That folder (C:\Users\Luc) is where the output file should be.


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

0 Kudos
newwenha
Contributor
Contributor

It appears I wasn't waiting long enough for the script to be finished. I see the results, but it is missing the name of the VM. The two columns only show the datastore and the time.

0 Kudos