Invoke-WmiMethod–Type mismatch error
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