VMware Cloud Community
sudeep0007
Contributor
Contributor

need a powershell scripts

Hi Guyz,

do you guyz have any powershell script for ESXI which create VMs with static ip and add them into domain.

Any help will be appreciated Smiley Happy

TIA

Tags (1)
Reply
0 Kudos
10 Replies
daphnissov
Immortal
Immortal

Have you done any Internet searching yourself? How about any attempts at beginning a script yourself? Generally speaking, going to a forum and asking people to hand things over to you isn't appropriate. Read the first post in my signature.

Reply
0 Kudos
sudeep0007
Contributor
Contributor

$fileName = 'whatever.csv'

foreach($vmLine in (Import-Csv -Path $fileName -UseCulture)){

    # Create VM

    $vm = New-VM -Name $vmLine.Server -VMHost $vmLine.EsxHost -Datastore $vmLine.Datastore -Template $vmLine.Template

    Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $vmLine.Network -StartConnected -Confirm:$false

    # Wait till VM is running

    Start-VM -VM $vm

    while($vm.Guest.State -ne 'running'){

        $vm = Get-VM -Name $vm.Name

        sleep 5

    }

    # Configure guest OS network

    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $($vmLine.IPAddress) $($vmLine.NetMask) $($vmLine.Gateway) 1"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

    $netsh = "c:\windows\system32\netsh.exe interface ip set dns ""Local Area Connection"" static $($vmLine.FirstDNS) $($vmLine.SecondDns)"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

}

i have tried this above script but it is showing below error

New-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and

then try the command again.

At line:13 char:24

+     $vm = New-VM -Name

Reply
0 Kudos
LucD
Leadership
Leadership

What is in the CSV you are using?

Is there a column Server in there?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sudeep0007
Contributor
Contributor

NameEsxHostDatastoreTemplateIPAddressGatewayFirstDNSSecondDNS

this is my csv file

Reply
0 Kudos
LucD
Leadership
Leadership

I suspect you used Name while the script uses Server.

Change that column name.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sudeep0007
Contributor
Contributor

Thanks Man now i am able to create the VMs but after that it is showing below error

Set-NetworkAdapter : Missing an argument for parameter 'StartConnected'. Specify a parameter of type 'System.Boolean' and try again.

At line:9 char:82

+ ... vmLine.Network -StartConnected -Confirm:$false

+                    ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-NetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : MissingArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

Reply
0 Kudos
LucD
Leadership
Leadership

You need to provide a Boolean value to that parameter.

Get-NetworkAdapter -VM $vm |

Set-NetworkAdapter -NetworkName $vmLine.Network -StartConnected:$true -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sudeep0007
Contributor
Contributor

now it is showing this below error Smiley Sad

Set-NetworkAdapter : Cannot validate argument on parameter 'NetworkName'. The argument is null. Provide a valid value for the argument, and then try running

the command again.

At line:1 char:66

+     Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $vmLine.Network ...

+                                                                  ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Set-NetworkAdapter], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

Reply
0 Kudos
sudeep0007
Contributor
Contributor

fixed the above issue now facing the below issue

Invoke-VMScript : 17-05-2020 22:03:24    Invoke-VMScript        Failed to authenticate with the guest operating system using the supplied credentials.   

At line:3 char:5

+     Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], InvalidGuestLogin

    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_GetGuestAuthentication_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

Reply
0 Kudos
LucD
Leadership
Leadership

You have to provide credentials to logon/run in the Guest OS

Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh -GuestCredential (Get-Credential)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos