Search code examples
powershellautomationvmwarepowercli

PowerCLI Prompt for Server name for "Connect-VI Server"


I am attempting to use a PowerCLI script to create Virtual Machines based off of a CSV file. The current code that I am working with has the 'Connect-VIServer' hard-coded as a variable in the script "$vcenter_srv = 'vcenter.seba.local'":

$ScriptRoot = Split-Path $MyInvocation.MyCommand.Path
$csvfile = "$ScriptRoot\vms2deploy.csv"
$vcenter_srv = 'vcenter.seba.local'
$timeout = 1800
$loop_control = 0

$vmsnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
$Error.Clear()
if ($vmsnapin -eq $null)    
    {
    Add-PSSnapin VMware.VimAutomation.Core
    if ($error.Count -eq 0)
        {
        write-host "PowerCLI VimAutomation.Core Snap-in was successfully enabled." -ForegroundColor Green
        }
    else
        {
        write-host "ERROR: Could not enable PowerCLI VimAutomation.Core Snap-in, exiting script" -ForegroundColor Red
        Exit
        }
    }
else
    {
    Write-Host "PowerCLI VimAutomation.Core Snap-in is already enabled" -ForegroundColor Green
    }

if ($env:Processor_Architecture -eq "x86") {

    #Connect to vCenter
    Connect-VIServer -Server $vcenter_srv

I am trying to determine if there is a way to cause the PowerCLI script to prompt for the server name with the 'Connect-VIServer' command when it occurs in the script "Connect-VIServer -Server $vcenter_srv" and then continue with the remaining values pulled from the CSV file.

Thank you for any information you may provide,


Solution

  • You can prompt for an aswer by doing a Read-Host and storing it in a variable which the Connect-VIServer cmdlet can use as below:

    $vc = read-Host "vCenter Server?"
    Connect-VIServer -Server $vc