开发者

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

开发者_JS百科
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?


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

PS C:\Users\ben> $path = "Win32_ComputerSystem.Name='OLDNAME'"
PS C:\Users\ben> 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:\Users\ben> (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:\Users\ben> ([wmi]"Win32_ComputerSystem.Name='OLDNAME'").Rename("NEWNAME")

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜