-1

I am trying to use PowerCLI to assign public IP address to a VM in ESXi but I am getting the following error:

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.

Can anyone help me where I am going wrong?

I tried looking at other Stackoverflow answers, but I could not find a solution for my issue.

Harsha R
  • 11
  • 2

1 Answers1

0

That error happens because "Set-WinVMIP" is not a standard PowerCLI cmdlet and never was. You're not doing anything wrong. You can write a script to change the IP of a VM and call it Set-WinVMIP if you'd like. Here is an example of one way to do it that you can modify as you want.

Copy it to a file called Set-WinVMIP.ps1 Note: VMware Tools does need to be already installed.

$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
MyuFoxy
  • 38
  • 5