簡體   English   中英

Invoke-VMscript無法正常工作

[英]Invoke-VMscript doesnt work as expected

這是我的腳本:

$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

產生以下錯誤:

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

字符串$ScriptText應包含引號之間的命令。

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

Invoke-VMScript通過提供的憑據登錄並運行腳本。

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

目前,我出現以下錯誤:

% : 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

您可以使用HereString作為腳本文本,如下所示:

$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

但是,我認為一種更簡單的方法是使用WMI,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM