Link to home
Start Free TrialLog in
Avatar of leosis
leosisFlag for Italy

asked on

need Help for exclude Vm Placeholder in foreachloop

Hi everyone


I have the following script that sets the Tags for the VMs contained in a CSV (very large) which contains many placeholder VMs that I want to exclude from the Foreachloop.. I tried to run the script below.. but unfortunately I find the tag also on the placeholder VMs

Any idea?

Thanks in advance


## Example CSV  #####

Vmname                       TAG      

------                            ------      

Server01                      DB Oracle

Placeholder02             DB SQL

######################


$TagDB = import-CSV "C:\temp\Tag-DB.csv"  -Encoding Default
ForEach ($item in $TagDB ) {
 $vm = $item.Vmname
if($vm.ExtensionData.Config.ManagedBy.extensionKey -notmatch 'com\.vmware\.vcDr')
{
 $tag = $item.TAG
 $vmname = $item.Vmname
  Get-VM -Name $vmname | New-TagAssignment -Tag $tag -Confirm:$false
}
else
{
 Write-Host  "$vm Is Placeholder" 
}

Open in new window


Avatar of PortletPaul
PortletPaul
Flag of Australia image

Can you work but excluding those names that contain "placeholder"?

$TagDB = import-CSV "C:\temp\Tag-DB.csv" -Encoding Default


ForEach ($item in $TagDB ) {
  $vm = $item.Vmname
  if($vm -notlike '*placeholder*') {
    if($vm.ExtensionData.Config.ManagedBy.extensionKey -notmatch 'com\.vmware\.vcDr') {
      $tag = $item.TAG
      $vmname = $item.Vmname
      Get-VM -Name $vmname | New-TagAssignment -Tag $tag -Confirm:$false
    } else {
      Write-Host "$vm Is Placeholder"
    }
  } else {
    Write-Host "$vm Is Excluded"
  }
}

Open in new window

Open in new window


Avatar of leosis

ASKER

hi Portletpaulthanks for your reply,unfortunately none of the vm has the name "Place holer" I used an example name unfortunately having a thousand and a thousand vm in the list extrapolated from the cmdb   with the related tags to assign, it's difficult for me to organize myself with excel... regards

Perhaps provide a more representative set of  examples?

But note, if there is nothing in common between placeholders that sets them apart from the others - then you may be barking up the wrong tree trying to code your way through the problem.

The script should work. Try if the condition

$vm.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*"

Open in new window

works better (though that shouldn't make a difference).


If not - you are talking about Site Recovery Manager VMs?

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

D'oh. I certainly need new glasses. oBdA is 150% correct.

Avatar of leosis

ASKER

Hi everyone


thanks for the replies, first of all I wanted to reply to PortletPaul:  my governance structure provided me with this excel, extrapolated from their repository called CMDB in which there are 2 columns, in the first colum i have  VMs of the datacenter (including the placeholders) , the second column i have  the TAGs with which they recognize the type of VM.. and they asked me to set the tags on all  VMs in the Vcenter.. but unfortunately I have difficulty working with the Vlookup function in excel by comparing it with RVtools, since there are over thousands of VM... so to make no mistake I would like to exclude them from the Foreachloop, which sets the TAGs..

So now I want to thank OBDA for their valuable support that is corret 200 % :-)