powershell 术语"Set-WinVMIP"未被识别为cmdlet的名称

pzfprimi  于 2022-11-29  发布在  Shell
关注(0)|答案(1)|浏览(199)

我尝试使用PowerCLI为ESXi中的虚拟机分配公共IP地址,但收到以下错误:
PS /home/usr/xxx> Set-WinVMIP <VM-name> 1:1:1:1 255.255.255.0 0.0.0.0
Set-WinVMIP: The term 'Set-WinVMIP' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
有谁能帮我解决我的问题吗?
我试着查看其他Stackoverflow的答案,但我找不到解决我的问题的方法。

qjp7pelc

qjp7pelc1#

发生该错误的原因是“Set-WinVMIP”不是标准PowerCLI cmdlet,而且从来都不是。您没有做错任何事情。您可以编写一个脚本来更改VM的IP,如果愿意,可以将其命名为Set-WinVMIP。下面是一个可以根据需要进行修改的方法示例。
将其复制到名为Set-WinVMIP.ps1的文件中。注意:需要先安装VMware Tools。

$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Administrator", "")

Function Read-MyHost {
    [cmdletbinding()]
    Param(
    [Parameter(Position=0,Mandatory,HelpMessage="Enter the message prompt.")]
    [ValidateNotNullorEmpty()]
    [string]$Message,
    [Parameter(Position=1,Mandatory,HelpMessage="Enter key property name or names separated by commas.")]
    [System.Management.Automation.Host.FieldDescription []]$Key,
    [Parameter(HelpMessage = "Text to display as a title for the prompt.")]
    [string]$PromptTitle = "",
    [Parameter(HelpMessage = "Convert the result to an object.")]
    [switch]$AsObject
    )
    $response = $host.ui.Prompt($PromptTitle,$Message,$Key)
    if ($AsObject) {
        #create a custom object
        New-Object -TypeName PSObject -Property $response
    }
    else {
        #write the result to the pipeline
        $response
    }
    } #end function

$VMINFO = Read-MyHost "Enter the follwing information for the VM" -Key "VMName","IP","SubnetMask","Gateway","DNS1","DNS2" -AsObject

$Network = Invoke-VMScript -VM "Windows Server" -GuestCredential $GC -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter 'netconnectionid is not null').netconnectionid"
$NetworkName = $Network.ScriptOutput
$NetworkName = $NetworkName.Trim()
$netshIP = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $VMINFO.IP $VMINFO.SubnetMask $VMINFO.Gateway" 
$netshDNS = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $VMINFO.DNS1"
$netshDNS2 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $VMINFO.DNS2"

Invoke-VMScript -VM $VMINFO.VMname -GuestCredential $GC -ScriptType bat -ScriptText $netshIP
Invoke-VMScript -VM $VMINFO.VMname -GuestCredential $GC -ScriptType bat -ScriptText $netshDNS
Invoke-VMScript -VM $VMINFO.VMname -GuestCredential $GC -ScriptType bat -ScriptText $netshDNS2
 
Write-Host "Setting IP address completed." -ForegroundColor Green

相关问题