VMware Cloud Community
vinceper
Contributor
Contributor
Jump to solution

Creating list of VMs to exclude from script through .csv file?

Hi,

I have a script that runs everyday off hours to verify that my VMs respect certains things, such as having the "VM Tools Upgrade Policy" option checked. The script exports a list of VMs that DO NOT have this option enabled, and then later rectifies the problem. Now, for certain specific VMs, I do not want to have that option enabled, and would like to exclude them from that check. I'm trying to add a list of those VMs that I want to exclude from my VMTools_UpgradePolicy check script through a .csv file that contains their names. Here is what I have :

##############################################
#VMTools Upgrade during power cycle  - Report
##############################################
function Report_VMTools_UpgradePolicy {
$list = Import-Csv "C:\VMware\PowerCLI\Scripts\EXCLUDE LIST\EXCLUDE_vmtools_upgradepolicy.csv"
foreach ($entry in $list) {$exclude = Get-VM -Name $entry.Name}
$vms = Get-Vm | where {$_.Name -ne $exclude -and (Get-View -Id $_.Id).Config.Tools.ToolsUpgradePolicy -eq "manual"} | Select Name
$vms | Export-Csv "C:\VMware\PowerCLI\Scripts\REPORTS\vmtools_upgradepolicy.csv" -NoTypeInformation
}

The content of the EXCLUDE_vmtools_upgradepolicy.csv is as follow :

"Name"
"VM1"
"VM2"

"VM3"

I'm trying to get all VMs in my environment that have the ToolsUpgradePolicy -eq "manual" (unchecked), AND that do not match any of the names from my exclude list (VM1, VM2, VM3). My script so far will do what I want, except that only the last entry from my exclude list will be taken into account. VM1 and VM2 will still be reported as VMs that need to be processed.

I'm not sure I am taking the right approach with this, I will keep looking for a solution for what I want but any help would be appreciated! I prefer getting my exclude list from a separate file such as that "EXCLUDE_vmtools_upgradepolicy.csv", because I want the other administrators to be able to just open that .csv file and add any VMs to that list without having to touch my scripts.

Thanks!

Vince

0 Kudos
1 Solution

Accepted Solutions
Grzesiekk
Expert
Expert
Jump to solution

Hi Vince,

it is acting like this i believe because of your statement :

foreach ($entry in $list) {$exclude = Get-VM -Name $entry.Name}   -> this one
$vms =  Get-Vm | where {$_.Name -ne $exclude -and (Get-View -Id  $_.Id).Config.Tools.ToolsUpgradePolicy -eq "manual"} | Select Name
$vms | Export-Csv "C:\VMware\PowerCLI\Scripts\REPORTS\vmtools_upgradepolicy.csv" -NoTypeInformation
}

$exclude is a variable that contains only 1 entry., so that's why you see only the last item in it. It will not be an array.

foreach ($entry in $list) {[string[]]$exclude+ = Get-VM -Name $entry.Name}

This will populate $exclude array with string entries that will hold vm names.

And when you will be comparing it. i think you should use

where { $exclude -notcontains $_.Name -and ....... the rest

Regards,

Greg

--- @blog https://grzegorzkulikowski.info

View solution in original post

0 Kudos
3 Replies
Grzesiekk
Expert
Expert
Jump to solution

Hi Vince,

it is acting like this i believe because of your statement :

foreach ($entry in $list) {$exclude = Get-VM -Name $entry.Name}   -> this one
$vms =  Get-Vm | where {$_.Name -ne $exclude -and (Get-View -Id  $_.Id).Config.Tools.ToolsUpgradePolicy -eq "manual"} | Select Name
$vms | Export-Csv "C:\VMware\PowerCLI\Scripts\REPORTS\vmtools_upgradepolicy.csv" -NoTypeInformation
}

$exclude is a variable that contains only 1 entry., so that's why you see only the last item in it. It will not be an array.

foreach ($entry in $list) {[string[]]$exclude+ = Get-VM -Name $entry.Name}

This will populate $exclude array with string entries that will hold vm names.

And when you will be comparing it. i think you should use

where { $exclude -notcontains $_.Name -and ....... the rest

Regards,

Greg

--- @blog https://grzegorzkulikowski.info
0 Kudos
vinceper
Contributor
Contributor
Jump to solution

Thanks very much Greg, this has worked as expected! That will be useful for my scripts. I just had to change a space in this line

{[string[]]$exclude+ = Get-VM -Name $entry.Name}

to

{[string[]]$exclude += Get-VM -Name $entry.Name}

Thanks again for your help.

Vince

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Exactly Smiley Wink  typo there. Im glad it helped Smiley Wink

--- @blog https://grzegorzkulikowski.info
0 Kudos