powershell - Powershell集变量未正确通过

标签 powershell powercli

我在脚本中添加了菜单部分,该菜单部分使用函数来设置变量以连接到vcenter服务器。问题在于它没有传递变量。当我退出脚本时,执行get变量时变量是正确的。

#Main menu
function MainMenu {
    Clear-Host
    [int]$xMenuChoiceA = 0

    while ($xMenuChoiceA -lt 01 -or $xMenuChoiceA -gt 04) {
        Write-Host "============================================" -ForegroundColor Green
        Write-Host "|   Main Menu                              |" -ForegroundColor Green
        Write-Host "============================================" -ForegroundColor Green
        Write-Host "|  Migration Script                        |" -ForegroundColor Green
        Write-Host "============================================" -ForegroundColor Green
        Write-Host "|                                          |" -ForegroundColor Green
        Write-Host "| 1. Development                           |" -ForegroundColor Green
        Write-Host "| 2. Model                                 |" -ForegroundColor Green
        Write-Host "| 3. Production                            |" -ForegroundColor Green
        Write-Host "| 4. Quit                                  |" -ForegroundColor Green
        Write-Host "|                                          |" -ForegroundColor Green
        Write-Host "============================================" -ForegroundColor Green
        Write-Host ""
        Write-Host "Please enter an option 1 to 4..." -ForegroundColor Green -NoNewline

        [Int]$xMenuChoiceA = Read-Host
    }

    Switch($xMenuChoiceA) {
        1 { SetDev }
        2 { SetModel }
        3 { SetProd }
        4 { Are-YouSure }
        default { MainMenu }
    }
}

#Verifiyng exit of menu
function Are-YouSure {
    Write-Host "Are you sure you want to exit? (y/n)" -ForegroundColor Cyan -NoNewline
    $areyousure = Read-Host

    if ($areyousure -eq "y") { exit }
    if ($areyousure -eq "n") { MainMenu }
    else {
        Write-Host -ForegroundColor Red "Invalid Selection"
        Are-YouSure
    }
}

#clear vCenter variables
$SourceVC = $null
$DestVC = $null

#menu set vCenter function
function SetDev {
    Set-Variable -Name SourceVC -Value devvc.effingps.corp -Scope global
    Set-Variable -Name DestVC -Value ucsvcd.effingps.corp -Scope global
    break
}

function SetModel {
    Set-Variable -Name SourceVC -Value modelvc.effingps.corp -Scope global
    Set-Variable -Name DestVC -Value ucsvcm.effingps.corp -Scope global
    break
}

function SetProd {
    Set-Variable -Name SourceVC -Value prodvc.effingps.corp -Scope global
    Set-Variable -Name DestVC -Value ucsvcp.effingps.corp -Scope global
    break
}

MainMenu

#connects to the source vCenter for right-sizing
Connect-VIServer $SourceVC

当它失败时,看起来好像变量没有被传递,而是使用文字$ variable而不是函数中设置的变量。 Get-variable返回正确的变量(写入主机$ sourceVC等也是如此)。这是错误:

Connect-VIServer : Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At D:\scripts\vm-uscmigration.ps1:84 char:18

+ connect-viserver $SourceVC + ~~~~~~~~~ CategoryInfo : InvalidData: (:) [Connect-VIServer], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer



脚本在服务器的手动输入下确实可以正常工作,但是由于Operations将运行脚本,因此我想从公式中排除手动错误,因为已知源vCenter和目标vCenter。

谢谢!

最佳答案

我建议在其定义的范围内使用([ref]$Variable).Value='NewValue'更新变量。

function SetDev {
    ([ref]$SourceVC).Value='devvc.effingps.corp'
    ([ref]$DestVC).Value='ucsvcd.effingps.corp'
    break
}
function SetModel {
    ([ref]$SourceVC).Value='modelvc.effingps.corp'
    ([ref]$DestVC).Value='ucsvcm.effingps.corp'
    break
}
function SetProd {
    ([ref]$SourceVC).Value='prodvc.effingps.corp'
    ([ref]$DestVC).Value='ucsvcp.effingps.corp'
    break
}

关于powershell - Powershell集变量未正确通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499568/

相关文章:

Powershell:如何同时使用 Invoke-Command 和 dot sourcing?

powershell - 如何选择名称中包含括号的 NoteProperty

powershell - 在 PowerShell 中创建文件夹别名

c# - 使用PowerCLI for .NET在VMware vSphere Client上拦截错误消息

c# - 在 asp.net mvc 中执行 Powershell 和 Powercli 命令时如何设置超时

azure - 为什么我收到 "New-AzSqlDatabaseImport : Not found: Entity not found to invoke import"

powershell - 在 Powershell 的 GUI 窗口中创建进度条

powershell - ScriptText 中的 VMware PowerCLI 变量

c# - 从 C# 调用 VmWare PowerCLI