Automation

 View Only
  • 1.  setting upgradepolicy via powershell to certain machines

    Posted Feb 19, 2018 05:48 PM

    I appreciate this sort of question has been asked before but in my trials i cannot find how to achieve what i want. I also appreciate i'm a little out of my depth as I've not ever played around with the powercli module and this is my first attempt

    I want to change the policy of any VM that is currently set to  "upgradeatpowercycle", to "manual". I'd prefer not to just do a blank across the hosts but rather a cluster (or specific machines) at a time, so through looking through documentation/forums/ trial and error  etc  I thought i had it with this:

    $cluster = get-cluster 'CL-DEV-NP'

    $servers = $cluster | Get-VM

    #param([string]$servers)

    $spec= new-object vmware.vim.virtualmachineconfigspec

    $spec.tools=new-object vmware.vim.toolsconfiginfo

    $spec.tools.toolsupgradepolicy ="manual"

    $vms= get-vm $servers

    Foreach($vm in $vms){

    $view=$vm | get-view -ViewType virtualmachine -SearchRoot $cluster.Id  -Filter @{'Config.Tools.ToolsUpgradePolicy' = 'upgradeAtPowerCycle' }

    $view.reconfigvm($spec)

    }

    this produces this error:

    get-view : The input object cannot be bound to any parameters for the command either

    because the command does not take pipeline input or the input and its properties do

    not match any of the parameters that take pipeline input.

    At line:56 char:11

    + $view=$vm|get-view -ViewType virtualmachine -SearchRoot $cluster.Id   ...

    +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (ueb-dev-01:PSObject) [Get-View], Para

       meterBindingException

        + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets

       .Commands.DotNetInterop.GetVIView

    I'm guessing I need to do the filtering beforehand and make it in a format that will be accepted but i cannot seem to get the result of  :

    get-view -ViewType virtualmachine -SearchRoot $cluster.Id  -Filter @{'Config.Tools.ToolsUpgradePolicy' = 'upgradeAtPowerCycle' }

    into a format i can use.

    I've seen ways to do it by feeding the vm's into the script from txt files but i'd rather do it via a search first. Could somebody help point me in the right direction to achieving what i'm trying to do

    Kind Regards



  • 2.  RE: setting upgradepolicy via powershell to certain machines
    Best Answer

    Posted Feb 19, 2018 06:03 PM

    Try like this

    $cluster = Get-Cluster 'CL-DEV-NP'

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $spec.Tools = New-Object VMware.Vim.ToolsConfigInfo

    $spec.Tools.ToolsUpgradePolicy = "manual"

    $filter = @{'Config.Tools.ToolsUpgradePolicy' = 'upgradeAtPowerCycle'}

    Get-View -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef -Filter $filter | %{

        $_.ReconfigVM($spec)

    }



  • 3.  RE: setting upgradepolicy via powershell to certain machines

    Posted Feb 22, 2018 09:00 PM

    Thank you for the response and solution!

    I was able to manipulate your answer a little to get what it needed to do ...and then went off on a tangent and made it do even more!! so thank you very much



  • 4.  RE: setting upgradepolicy via powershell to certain machines

    Posted Feb 21, 2018 08:53 AM

    Nighthawkz

    another way (across Get-VM)

    $spec = New-Object -Type VMware.Vim.VirtualMachineConfigSpec 

    $spec.Tools = New-Object -Type VMware.Vim.ToolsConfigInfo 

    $spec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" 
    Then, set the policy only for Windows VM

    Get-VM | Where {$_.guest.guestFamily -eq ‘windowsGuest’} | ForEach-Object {$_.ExtensionData.ReconfigVM_Task($spec)}