I am wanting to add the ability for my concept script to iterate the same loop of commands over a list of servers, rather that over just one server.
Is it safe to have a foreach loop inside of a foreach loop. Or is there another way to do this?
# Create an empty hash table for discovered virtual machines
my %virtual_machines = ();
my @getallvms = $ssh1->capture('vim-cmd vmsvc/getallvms');
# Remove first line from ESX\ESXi output
shift @getallvms;
# Collect data from ESX\ESXi output
foreach my $server (@servers) {
foreach my $vm (@getallvms) {
# Match ID, NAME and VMX file name
$vm =~ m/^(?<ID> \d+)\s+(?<Name> \S+)\s+\[.+?\]\s+.+?\/(?<VMX> .+?\.vmx)/xm;
my $id = "$+{ID}";
my $name = "$+{Name}";
# Find the absolute path to the VMX file for each virtual machine
my $vmx_location = $ssh1->capture("find -name $+{VMX}");
}
}
That is a piece of my script and the basic concept I want to achieve as per the first response.
Yes, it’s safe. However, you will probably want to use different loop variables other than the default
$_.