gpt4 book ai didi

powershell - Powershell集变量未正确通过

转载 作者:行者123 更新时间:2023-12-02 23:59:12 25 4
gpt4 key购买 nike

我在脚本中添加了菜单部分,该菜单部分使用函数来设置变量以连接到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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com