0

I have a script that modifies network info of certain VMs ,however it run sequentially . Since there are hundred of VMs , I want to all VMs to execute the command simultaneously in order to save time . Also how do I add invoke-vmscript to the scriptblock with external variables. I did try few things but doesnt seem to working well. Script is run on a system using powercli 10 on linux and no additional modules are allowed to be installed

Below is the code , any help would be much appreciated

$test=get-vm -name testvm* |sort-object
 $fip='192.168.10.41'
$f=$fip.Split(".")|select -first 3
$s=[system.String]::join(".",$f)
$l=$fip.Split(".")|select -last 1
foreach  ($vm in $test) {
Write-Host "$vm  $s.$l"
do {start-sleep -s 2; write-host " $vm not ready yet" ;  $tstatus=(get-vm 
$vm).extensiondata.Guest.ToolsStatus;}while($tstatus -ne "toolsOk");
invoke-vmscript -ScriptText "sed -i '14,20 d' /etc/network/interfaces" - 
ScriptType bash -VM $vm -GuestUser admin -GuestPassword Welcome!}
$ipscript = "sed -i -e  's/address *.*.*.*/address $s.$l/g' 
/etc/network/interfaces"
write-host " Adding IP "

invoke-vmscript -ScriptText  $ipscript -ScriptType bash -VM $vm -GuestUser 
$GuestUserName admin -GuestPassword Welcome! 
Start-Sleep -s 2
Restart-VM -VM $vm  -RunAsync -Confirm:$false
$l=[int]$l+1
}
Mike Joe
  • 1
  • 1
  • Not exactly a duplicate as you need to know how to handle the session and the snapins. You need to "add-pssnapin VMware.VimAutomationCore to the top of the file calling the job and the file that runs the job, the same will be needed for the connect-viserver. – user3520245 May 25 '18 at 11:28
  • I have modified the question to reflect the exact issue ,so its not a duplicate – Mike Joe May 25 '18 at 15:52
  • Aaah...the fact that you are running on linux and thus presumably on to of Powershell Core does potentially add a unique wrinkle. – EBGreen May 29 '18 at 12:15
  • I would try sticking the script in another file, then call that file with run as job. At the top of both files you would need to declare the powercli snap in and pass around your log in cred – user3520245 Jun 12 '18 at 08:39

0 Answers0