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
please try with this code.
$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
}
Can you please attach it? I'm having trouble copy and pasting it.
I'm getting an error when I run it, what about on your end?
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
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
please post the script. looks like there is a " instead of {
get-vm | where { $_.PowerState -eq “PoweredOff”} | %{
The script is exactly what you uploaded
Give the attached script a try
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
the outputfile should be defined with the variable $outputFile
like following.
$outputFile = "C:\test.csv"
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
I'm running the .ps1 file as is and there is no output.
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
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.
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
Right click -> Edit (which opens up in Powershell IDE) -> Run (green button)
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
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.