PowerCLI

 View Only
Expand all | Collapse all

Script to Automate VM Tools and Hardware Upgrade..

  • 1.  Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 19, 2011 01:26 PM

    Hi community,

    I'm now looking to create a script to semi automate the VM Tools and Virtual HW upgrade.. I've searched and the script which I got runs on all the VMs in a vcenter. Which will not be a good option for me as we need approval from BUs..

    Here is what I've planned...and the reason is we work on a list of change records that have approved VMs for this upgrade..

    1. Script imports a list of VMs from a location (using Import-csv)

    2. Gets an output with VMs Name, Cluster Name, Nic Count, IP Addresses, WINS IPs (I'm not sure how to pull out WINS IPs..I can do rest of them)

    3. Script updates the VM Tools version and reboots

    4. After reboot script waits for about 5 mins and shuts down the VM and Upgrades the virtual hardware.

    5. Start the VM and wait till new hardware changes are made to the VM and finally reboot again.

    Can this be done simultaneously on say 10 VMs at a time? Also can this be done interactively using "Write-Host" so we can see which VM script is working on currently?

    Also after completing all this step 2 will run again and output is written to a different file so we can check if any nics or ip addresses are gone..(we ran into network issues while upgrading hardware last time)

    Thanks for your help in advance!!

    VMSavvy :smileyhappy:



  • 2.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 19, 2011 02:39 PM

    Hi,

    i have a task in update manager to accomplish this task, nad you can do in all the vms that you need. Yo avoid network issues i recommend you that you upgrade vmware tools before virtual hardware. Maybe in your script in some cases tools updates takes more than five minutes and for this reason you had network issues (because tools are not properly upgraded)

    can you copy here your script to see it?

    Regards



  • 3.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 20, 2011 11:39 AM

    Hi Moritz,

    I haven't used scripting to update VMtools or hardware. We do it manually and is getting more painful as on one go we are getting to do this on 200 VMs.. Thats the reason I'm looking to automate..

    I don't think we can do hardware upgrade without updating VM tools.. if I can recall it right..

    VMSavvy



  • 4.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 19, 2011 03:11 PM

    You can use the RunAsync parameter on the cmdlets, that way you can run multiple tasks in parallel.

    You will have to keep track of these async tasks.

    One way of doing this is with a hash table as I have shown in my About Async tasks, the Get-Task cmdlet and a hash table post.

    You can query the status of the running tasks, which will allow you to display something on screen.

    Do you already have part of this script ?



  • 5.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 19, 2011 04:48 PM

    I currently have this..(attached)

    I got this from some blog site.. can't recall the name..

    As per my understanding it runs through all the VMs in a given vcenter.. which is a big NO NO for me..

    Luc, can you help me to get this worked as per my requirements that I gave in the question? That would be awesome..

    Thanks for responding real quick..

    VMSavvy :smileyhappy:



  • 6.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 12:55 PM

    Luc, can you please help me out with this?

    Thank you so much. Appreciate your help!!

    VMSavvy :smileyhappy:



  • 7.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 01:30 PM

    Try the attached script, it reads a CSV file with names of the VMs you want to upgrade.

    The CSV file should look like this

    "vmName"

    "vm1"

    "vm2"

    Best run it first with a short list of non-critical VMs, just to make sure it does what you want it to do :smileywink:



  • 8.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 02:26 PM

    Will sure try this. I don't understand the following lines..

    $esx = Get-VMHost xyz*
    $deviceNames = Get-ScsiLun -VmHost $esx | where {$_.CanonicalName -match "naa"}

    what these lines will do?

    Also you said the CSV file should look like

    "vmName"

    "vm1"

    "vm2"

    so the first row should read "vmName"? Sorry for asking these silly questions.. :smileyhappy:

    VMSavvy



  • 9.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 04:21 PM

    Something went wrong with the copy/paste from the script.

    I updated the attached script above.

    The first line in a CSV file defines the column names.

    That is also the name with which you can get at these values after the Import-Csv, in this case the $_.vmName variable in the script.

    There are no silly questions :smileyhappy:



  • 10.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 05:12 PM

    I guessed it should be a Ctrl C/Ctrl V error :smileywink:

    As I wanted to run this on multiple VMs at a time do I have to add -RunAsync for all the functions.. it is now in Power-On function as below..

    Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null

    VMSavvy..



  • 11.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 21, 2011 06:53 PM

    You can add the RunAsync parameter and the cmdlets will run in the background.

    The only problem is that the script now has some sleep commands, they will be useless with multiple background jobs.

    But you still have to make sure that the task you started with 1 cmdlet (Update-Tools for example) is completed before you start the next one in the sequence.



  • 12.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 22, 2011 01:28 PM

    Hi Luc,

    As it is getting a list of VMs in a folder through Get-Folder $folder and I only want to do with the list of vms in the csv file I've made few changes where Get-Folder and $folder are given.. I think I spoiled it..Please see if there are any errors in there..

    Also not sure why I'm getting these errors. Pls help!!

    Creating a CSV File with VM info
    Cannot process argument transformation on parameter 'Datastore'. Strings as pipeline input are not supported.
    At :line:96 char:34
    +     foreach ($vm in $vmNames | Get-VM <<<<  )

    Cannot process argument transformation on parameter 'Datastore'. Strings as pipeline input are not supported.
    At :line:96 char:34
    +     foreach ($vm in $vmNames | Get-VM <<<<  )

    Cannot process argument transformation on parameter 'Datastore'. Strings as pipeline input are not supported.
    At :line:96 char:34
    +     foreach ($vm in $vmNames | Get-VM <<<<  )

    script is attached.

    Regards,

    VMSavvy.



  • 13.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jul 22, 2011 01:54 PM

    PowerShell doesn't know how to interpret that statement.

    You can fix that by using brackets

    foreach ($vm in ($vmNames | Get-VM)){

       ...

    }

    Btw, I couldn't find that line in the attached script.

    Is that the version that produced these errors ?



  • 14.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jan 10, 2024 06:02 AM

    Hi Luc,

    This is my first post and I'd gone through several of your answers to queries from many VMware as well as scripting enthusiasts and the answers to their queries had helped myself too. I'd like to thank you for all the help.

    I'm working on an automation for vCenter where, it should interact with us during execution at various stages based on customizable requirements. For example, when we execute the script, it should prompt the eecuter to choose between the options, basically tasks like vCenter upgrades, Host level upgrade, Tools & H/W Compatibility, either forced or on demand, std. to distributed switch, port group creation, VM, DataStore creation, removal, etc. OfCourse, it should all first check the existing essentials and then, to prompt us if there's an upgrade or to tell that we're current and send out a report.

    I understand it's a very humongous task to have a PowerCLI script readied for this. Not sure if it already exists. If so, could you please point out to me to that forum. However, I'm looking at catering everything when it comes to vCenter covering A-Z part of it from Reporting to Action and compare the changes to have a better manageability. 

    Any help from you would be much appreciated... Thanks in advance!



  • 15.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jan 10, 2024 07:27 AM

    I'm afraid you can't find such an extended, ready-to-use script in this community.
    You might find most of the tasks you described in one form or another in here.

    To tackle such a task I would split it up into multiple subtasks.
    And then find solutions/scripts for each such subtask.



  • 16.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jan 11, 2024 03:12 AM

    Sure Luc. Thanks!



  • 17.  RE: Script to Automate VM Tools and Hardware Upgrade..

    Posted Jan 11, 2024 07:29 AM

    For Parallel Processing, we often use PowerShell 7 with -InParallel feature.

    Something likes below

    $VMs | Foreach-Object -Parallel {

          ##processing

    } -ThrottleLimit $No_of_Proces_Limit