csv轉powercli語法大量rename VM

ref:
https://communities.vmware.com/message/2252750

csv example

Old_Name001,VM Description,New_Name001
Old_Name002,VM Description,New_Name002
Old_Name003,VM Description,New_Name003
Old_Name004,VM Description,New_Name004

 

 

<?php

$file1 = file('example.csv');
#print_r($file1);

foreach($file1 as $line){
  $tmp = preg_split("/,/", $line);
  #print_r($tmp);
  $old_name = trim($tmp[0]);
  $new_name = trim($tmp[2]);

  echo "#rename VM $old_name -> $new_name\n";
  echo "\$VM = Get-VM -Name '$old_name'\n";
  echo "Set-VM -VM \$VM -Name '$new_name' -Confirm:\$false\r\n"; #配合Windows txt格式換行符號需調整為\r\n

}//foreach

?>

 

Output

#rename VM Old_Name001 -> New_Name001
$VM = Get-VM -Name 'Old_Name001'
Set-VM -VM $VM -Name 'New_Name001' -Confirm:$false
#rename VM Old_Name002 -> New_Name002
$VM = Get-VM -Name 'Old_Name002'
Set-VM -VM $VM -Name 'New_Name002' -Confirm:$false
#rename VM Old_Name003 -> New_Name003
$VM = Get-VM -Name 'Old_Name003'
Set-VM -VM $VM -Name 'New_Name003' -Confirm:$false
#rename VM Old_Name004 -> New_Name004
$VM = Get-VM -Name 'Old_Name004'
Set-VM -VM $VM -Name 'New_Name004' -Confirm:$false

 

Facebook Comments Box

Leave a Reply