Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Invoke VMscript未按预期工作_Powershell - Fatal编程技术网

Powershell Invoke VMscript未按预期工作

Powershell Invoke VMscript未按预期工作,powershell,Powershell,这是我的剧本: $ScriptText = $Adapter = Get-NetAdapter | Select-Object InterfaceDescription,Name | %{ if ($Adapter.InterfaceDescription -match "vmxnet3") { Get-NetAdapter -Name $Adapter.Name | Rename-NetAdapter -NewName "LAN"

这是我的剧本:

$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
产生以下错误:

调用VMScript:26-8-2015 11:34:40调用VMScript 找不到必需参数ScriptText的值 第7行字符:1 +调用VMScript–VM“DC01”-guestuser“administrator”-guestpassword“password”。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:NotSpecified:(:)[Invoke VMScript],VimException +FullyQualifiedErrorId:Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.cmdlet.Commands.InvokeVmScript 字符串
$ScriptText
应包含引号之间的命令

$ScriptText = "Get-NetAdapter | Select InterfaceDescription,Name | % {if($_.InterfaceDescription -match 'vmxnet3') {Get-NetAdapter -Name $_.Name | Rename-NetAdapter -NewName ""LAN""}}"
调用VMScript
通过提供的凭据登录并运行脚本

Invoke-VMScript –VM "DC01” -guestuser “administrator” -guestpassword “password” -ScriptText $ScriptText
此时我有以下错误:

%:术语“.InterfaceDescription”未被识别为cmdlet的名称,
函数、脚本文件或可操作程序。检查名称的拼写,或
如果包含路径,请验证路径是否正确,然后重试。
第1行字符:56
+&{Get NetAdapter |选择InterfaceDescription,名称|%{如果(.InterfaceDescripti。。。
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:ObjectNotFound:(.InterfacedDescription:String)[ForEach对象],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()

$ScriptText
的值是什么?Richard,ScriptText是一种脚本块。代码将在虚拟机内运行。Invoke VMScript允许脚本在虚拟机内运行。是的,这很明显:但错误是它没有值。(我注意到,它必须是字符串而不是脚本块).Richard,Get-NetAdapter |选择接口描述,名称|%{if($\.InterfaceDescription-match'vmxnet3'){Get-NetAdapter-Name$\.Name |重命名NetAdapter-NewName“LAN”}正在Windows 2012R2服务器上工作,没有问题。它将nic重命名为LAN。字符串必须是以下文本行:Get-NetAdapter | Select InterfaceDescription,Name |%{if($.InterfaceDescription-match'vmxnet3'){Get NetAdapter-Name$|Name | Rename-NetAdapter-NewName“LAN”}感谢您的宝贵时间Marcel