[Solved] How can I use Invoke-WmiMethod to rename a computer

I am trying to call the Rename method on the Win32_ComputerSytem class using Invoke-WMI method. Using this syntax works fine

(gwmi win32_ComputerSystem).Rename("NEWNAME")

This also works fine for demo purposes

Invoke-WmiMethod -path win32_process -Name create -ArgumentList notepad

However, when i try the following, I get an error

11 >  Invoke-WmiMethod -path win32_computersystem -Name Rename -ArgumentList IwasRenamed
Invoke-WmiMethod : Invalid method Parameter(s) 
At line:1 char:17
+ Invoke-WmiMethod <<<<  -path win32_computersystem -Name Rename -ArgumentList IwasRenamed
    + CategoryInfo          : InvalidOperation: (:) [Invoke-WmiMethod], ManagementExcepti 
   on
    + FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands. 
   InvokeWmiMethod

What am I missing?

Solution #1:

You need to specify an instance of the class Win32_ComputerSystem using the Path parameter:

PS C:Usersen> $path = "Win32_ComputerSystem.Name="OLDNAME""
PS C:Usersen> Invoke-WmiMethod -Name Rename -Path $path -ArgumentList "NEWNAME"

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0

Which is functionally equivalent to the gwmi Rename syntax that you referred to. This syntax implicitly retrieves an instance of the class Win32_ComputerSystem to call the method on:

PS C:Usersen> (gwmi win32_computersystem).rename("NEWNAME")

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0

Here’s another cool syntax:

PS C:Usersen> ([wmi]"Win32_ComputerSystem.Name="OLDNAME"").Rename("NEWNAME")

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0
Respondent: xcud

Solution #2:

The Rename method takes three parameters. I’m guessing Invoke-WmiMethod uses reflection to call the method, so you have to specify all three parameters. Try this:

[String]$newName = "IWasRenamed"
[String]$password = $null
[String]$username = $null

Invoke-WmiMethod -Path Win32_ComputerSystem -Name Rename -ArgumentList $newName, $password, $username
Respondent: George Howarth

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.