WMI error in scheduled script

Oct 11, 2010 at 7:08 AM

I've written an script that reads a file of VM names; each VM is suspended, exported and brought back up.

This script works perfectly when the script is pasted in a powershell console with al powershell modules loaded. However when I want to schedule the script, I get a WMI error with every hyper-v cmdlet (line number may vary).

Get-WmiObject : Invalid class
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\hyperv\VM.ps1:70 char:26
+             Get-WmiObject
<<<<  -computername $Server -NameSpace $HyperVNames
pace -Query $WQL | Add-Member -MemberType ALIASPROPERTY -Name "VMElementName" -
Value "ElementName" -PassThru
    + CategoryInfo          :
InvalidOperation: (:) [Get-WmiObject], Managemen
   tException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
   ommands.GetWmiObjectCommand


The task is run as administrator and the commandline in the scheduled task is:
powershell -ImportSystemModules -command "& 'C:\scripts\VMBackup\testBackup.ps1' "

Thanks for any advice!

Pieter-Jan

Oct 25, 2010 at 10:02 PM

Hi Pieter,

Did you resolve this issue?  I would love to know what did the trick and what was the cause.  Hopefully you were able to resolve this without waiting for help from someone in this thread...

      - t -

Jan 4, 2011 at 11:15 PM

I'm getting some similar behavior.  I can successfully run individual commands such as get-vm and get-vhd in the console without error.  The odd behavior comes in with the ISE and any ps1 files.  I can run my scripts once but after that I get the exact same invalid class errors as the OP. 

Simple Example:

$vms = get-vm -server servername | %{$_.elementname}
foreach ($vm in $vms)
    {
    $vhdinfo  = get-vhd -server servername -path c:\clusterstorage\volume1\$vm
    $vhdsize = [math]::round($vhdinfo.Size/1GB,2)
    $path = $vhdinfo.name
    Write-Host $vm $vhdsize $path
    }

Running this script via ISE (F5) or as vhd-info.ps1 will work exactly once, after that I get invalid class errors.  But if I turn it into a single line I can run it over and over from the console:

$vms = get-vm -server servername | %{$_.elementname};foreach ($vm in $vms) {$vhdinfo  = get-vhd -server servername -path c:\clusterstorage\volume1\$vm;$vhdsize = $vhdinfo.Size/1GB;$path = $vhdinfo.name;Write-Host $vm $vhdsize $path}

OP did you have any luck figuring it out? 

Thanks!

Lance

Jan 31, 2011 at 8:00 AM

@Lance

I have the same behaviour. It works just once, then all these errors. I still haven't found the problem and I guess I'll never will. 

I'll try your solution and write everthing in a single line.

Cheers

Pieter-Jan

Coordinator
Feb 4, 2011 at 11:20 AM

I'm catching up on some of stuff I've left for a while here, this one's on the list ... no promises, but I'll see what I find.

Feb 14, 2011 at 2:07 PM

Hello,

 

I'm having similar problems.   Did you find anything?

Btw, I was looking at the variable $HyperVNamespace in the function Get-VM- I didnt' see in the script where this was being generated.

Thanks,

Carlos

Coordinator
Feb 14, 2011 at 5:45 PM

I don't have an answer yet.

$HyperVNameSpace is defined in constants.ps1

Feb 14, 2011 at 5:51 PM

Well that would explain things. Before posting this, I was looking for some type of "constants" file, as  I would have expected such file to contain the definitions. However I did not find it; Neither have i found the constants.ps1 file that you justed noted.  I wonder if something went wrong with my installation.  I don't see that file in Zip download.

Mar 4, 2011 at 11:06 AM

I had a similar problem with Get-WMI error and found out the problem was with my code importing the Hyper-V module more than once. I found the solution for my particular priblem by accident on the issues Thread "Import-Module HyperV multiple times causes errors"  .

 

The workaround was to replace my normal Import-Module command with this statement "if (!(Get-Module -Name HyperV)) { Import-Module HyperV }"

 

 

Hope this helps. :-)

 

Coordinator
Mar 10, 2011 at 5:37 PM
cardosct24 wrote:

Well that would explain things. Before posting this, I was looking for some type of "constants" file, as  I would have expected such file to contain the definitions. However I did not find it; Neither have i found the constants.ps1 file that you justed noted.  I wonder if something went wrong with my installation.  I don't see that file in Zip download.


Sorry . Constants.PS1 is a the file in the source tree where it is defined. It is merged into EN-US.PS1 which is the file the module loads.

If you are having trouble scheduling scripts, have a look at http://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/ 

May 18, 2011 at 8:33 AM

Hi jamesone and others,

By my side, issue is resolved when lauching PS running as Administrator.

I am surprized because my Administrator account is disabled AND belongs to Users group only! I use another admin account (this way shall lets hackers side way off the road because they looking for the usual "Administrator" account)

But I would schedule scripts using my daily admin account (and other I would let on).

Anybody for an idea about how to permit other account than usual administrator to run PS script?

... And tell me if my English syntax is bad :-)

May 18, 2011 at 8:40 AM
Edited May 18, 2011 at 8:52 AM

(post canceled)