×

PowerCLI提示“Connect-VI Server”的服务器名称(PowerCLI Prompt for Server name for “Connect-VI Server”)

hqy hqy 发表于2023-01-05 13:20:57 浏览274 评论0

抢沙发发表评论

PowerCLI提示“Connect-VI Server”的服务器名称(PowerCLI Prompt for Server name for “Connect-VI Server”)

我正在尝试使用PowerCLI脚本基于CSV文件创建虚拟机。 我正在使用的当前代码将“Connect-VIServer”硬编码为脚本“$ 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

我试图确定是否有一种方法可以使PowerCLI脚本在脚本“Connect-VIServer -Server $ vcenter_srv”中出现时使用“Connect-VIServer”命令提示输入服务器名称,然后继续执行其余操作从CSV文件中提取的值。

感谢您提供的任何信息,


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,



您可以通过执行读取主机并将其存储在Connect-VIServer cmdlet可以使用的变量中来提示输入aswer,如下所示:

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

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


 您阅读本篇文章共花了: 

打赏

本文链接:https://kinber.cn/post/3403.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

群贤毕至

访客