简体   繁体   中英

Invoke-VMscript doesnt work as expected

Here is my script:

$ScriptText = $Adapter = Get-NetAdapter | 
    Select-Object InterfaceDescription,Name | %{
        if ($Adapter.InterfaceDescription -match "vmxnet3") {
             Get-NetAdapter -Name $Adapter.Name | Rename-NetAdapter -NewName "LAN"
        }
    }

Invoke-VMScript –VM "DC01” -guestuser “administrator” -guestpassword “password” -ScriptText $ScriptText

The following error is produced:

Invoke-VMScript : 26-8-2015 11:34:40    Invoke-VMScript        
Value cannot be found for the mandatory parameter ScriptText
At line:7 char:1
+ Invoke-VMScript –VM "DC01” -guestuser “administrator” -guestpassword “password”  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

The string $ScriptText should contain the commands between the quotes.

$ScriptText = "Get-NetAdapter | Select InterfaceDescription,Name | % {if($_.InterfaceDescription -match 'vmxnet3') {Get-NetAdapter -Name $_.Name | Rename-NetAdapter -NewName ""LAN""}}"

The Invoke-VMScript logs in via the supplied credentials and runs the script.

Invoke-VMScript –VM "DC01” -guestuser “administrator” -guestpassword “password” -ScriptText $ScriptText

At this moment I have the following error:

% : The term '.InterfaceDescription' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:56
+ & {Get-NetAdapter | Select InterfaceDescription,Name | % {if(.InterfaceDescripti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.InterfaceDescription:String) [ForEach-Object], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.ForEachObjectCommand

You Can Use HereString for the Script Text like this:

$ScriptText = @'
$Adapter = Get-NetAdapter | 
       Select-Object InterfaceDescription,Name | 
       %{if ($Adapter.InterfaceDescription -match "vmxnet3")               
         {Get-NetAdapter -Name $Adapter.Name | Rename-NetAdapter -NewName "LAN"}
        }
'@

$GuestCredential = Get-Credential
Invoke-VMScript –VM "DC01” -GuestCredential $GuestCredential -ScriptText $ScriptText

However, an easier Approach i think is to use WMI,

$Adapter = gwmi win32_networkadapter -ComputerName DC01 | ? {$_.Description -match "vmxnet3"}
$Adapter.NetConnectionID = "LAN"
$Adapter.Put()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM