VMware Cloud Community
Pcliba
Contributor
Contributor
Jump to solution

"Attempted operation cannot be performed in the current state" error when trying to invoke command with invoke-vmscript for a SRM protected VM

Hi community,

I have a short question. I have a powercli script which does sth. with a vm via Invoke-VMScript.

If the VM is protected by SRM, I get the following error message:

Invoke-VMScript  The attempted operation cannot be performed in the current state (Powered off).

    + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], InvalidPowerState

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

   .Commands.InvokeVmScript

I tried to catch this with query

(Get-VM VMname).ExtensionData.Config.ManagedBy

But whatever I do, I get the same error message again. Sometimes, the action is performed nevertheless I receive this message.

Has somebody an idea what to do in this case?

Thanks in advance

Sepp

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

With the Get-VM cmdlet you will have to use a Where-clause.

Something like this

$vm = Get-VM -Name vm1041033 | where{$_.PowerState -eq 'PoweredOn'}

When you use the Get-View cmdlet, you have the option to use the Filter parameter that accepts RegEx expressions.

The drawback is that Get-View returns a vSphere object, you will need an extra line, with the Get-VIObjectByVIView, to convert this to a .Net object.

$vmObj = Get-View -ViewType VirtualMachine -Filter @{'Name'="^vm1041033$";'Runtime.PowerState'='PoweredOn'}

$vm = Get-VIObjectByVIView -VIView $vmObj


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

What PowerState does the Get-VM VMName return?

Is it always PoweredOff?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Pcliba
Contributor
Contributor
Jump to solution

Hi Luc,

thanks for you reply.

I search this vm in my 2 separate environments (whith 2 vcenter server each).

I get 2 lines, one with PoweredOff and one with PoweredOn while connected to both (all) vcenters.

Name             PowerState Num CPUs MemoryGB
----             ---------- -------- --------
vm1041033        PoweredOn  2    8.000
vm1041033        PoweredOff 2    8.000

Is there a way to get only the poweredOn vm and to skip the placeholder?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the Get-VM cmdlet you will have to use a Where-clause.

Something like this

$vm = Get-VM -Name vm1041033 | where{$_.PowerState -eq 'PoweredOn'}

When you use the Get-View cmdlet, you have the option to use the Filter parameter that accepts RegEx expressions.

The drawback is that Get-View returns a vSphere object, you will need an extra line, with the Get-VIObjectByVIView, to convert this to a .Net object.

$vmObj = Get-View -ViewType VirtualMachine -Filter @{'Name'="^vm1041033$";'Runtime.PowerState'='PoweredOn'}

$vm = Get-VIObjectByVIView -VIView $vmObj


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Pcliba
Contributor
Contributor
Jump to solution

Thanks Luc! The Get-View tip seems to be the solution! No errors with these lines.

I already tried the Where-clause with the Get-VM cmdlet but received the same error.

Thanks again for your quick reply! Appreciate it.

0 Kudos