PowerShell for Windows Admins

Sep 6 2011   11:11AM GMT

Invoke-WmiMethod–Type mismatch error

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Sometimes when we try to use Invoke-WmiMethod with an argument list we get an error

PS> Invoke-WmiMethod -Class Win32_Share -Name Create -ArgumentList "c:\test", "Test57", 0
Invoke-WmiMethod : Type mismatch
At line:1 char:17
+ Invoke-WmiMethod <<<<  -Class Win32_Share -Name Create -ArgumentList "c:\test", "Test57", 0
    + CategoryInfo          : InvalidOperation: (:) [Invoke-WmiMethod], ManagementException
    + FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.InvokeWmiMethod

 

Going back to basics this technique works – its how we did things in PowerShell and it still works great.

$s = [wmiclass]"Win32_Share"
$s.Create("c:\test", "Test57", 0)

As we have seen – this fails

Invoke-WmiMethod -Class Win32_Share -Name Create -ArgumentList "c:\test", "Test57", 0

According to the documentation the full list of the parameters for the method is:

Path
Name
Type
MaximumAllowed
Description
Password
Access

so we are really doing this

$path = "c:\test"
$name = "Test57"
$type = 0
$password = ""
$description = ""
$max = 100
$access = $null
$s = [wmiclass]"Win32_Share"
$s.Create($path, $name, $type, $max, $description, $password, $access)

when we did this

$s.Create("c:\test", "Test57", 0)

we were just ignoring the last four parameters.

BUT

if we look at the parameter list using
$s.psbase.GetMethodParameters("Create")

it shows the parameters in this order
Access
Description
MaximumAllowed
Name
Password
Path
Type

so this works
Invoke-WmiMethod -Class Win32_Share -Name Create -ArgumentList $access, $description, $max, $name, $password, $path, $type

If you get the Type mismatch error then its a good time to check the parameter order.

PS – I haven’t verified that the Invoke-WmiMethod expects the parameters in alphabetical order

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: