多个变量声明

时间:2017-01-25 09:15:28

标签: vmware powershell-v3.0

我有一个脚本,我将它用于多个任务,如

  1. 在vSphere中创建文件夹
  2. 创建ResourcePools
  3. 创建VM(VMware)
  4. 配置IP / DNS
  5. 将其添加到域名,...
  6. 为此,我需要多个输入,如VM名称,vCenter Server名称,用户ID,密码等。如何在外部声明输入变量(与主脚本分开),并使其外观通用。

    我尝试使用以下程序,但它给我调用深度溢出错误。

    VARIABLE_DECLARATION.ps1

    VariableDeclaration = @{
      [CmdletBinding()]
      Param (
        [Parameter(Mandatory=$True)]
        [Parameter(ValueFromPipeline=$true)]
        [int]$CustomerCID = '10000';
        [string]$CustomerName = "ABCcorp";
        [string]$vCenterName = "vCenter.ABCcorp.com";
        [string]$vCenterUserName = "administrator@vsphere.local";
        [string]$vCenterPassword = "ABCcorp123!";
        [string]$CustomerPODLocation = "VW1";
        [string]$DatacenterName = "ABCcorpDC";
        [string]$ClusterName = "ABCcorpcluster";
        [string]$InfraResourcePoolName = ($CustomerCID + "-" + $CustomerName + "-" + "Infrastructure");
        [string]$DesktopResourcePoolName = ($CustomerCID + "-" + $CustomerName + "-" + "Desktop");
        [string]$CustomerFolderName = ($CustomerCID + "-" + $CustomerName);
        [string]$ConnectionType = "xConnect";
        [int]$VLANID = '237';
        [string]$CustomerDVPortGroupName = ($CustomerPODLocation + "-" + $ConnectionType  + "-" + $CustomerCID + "-" + $CustomerName + "-" + $VLANID);
        [int]$NumberofPorts = '1024';
        [string]$AD1computername = ($CustomerCID + "-RADSVR01-v" + $CustomerName + ".vdi");
        [string]$AD2computername = ($CustomerCID + "-RADSVR02-v" + $CustomerName + ".vdi");
        [string]$sourcetemplate = "BaseWin2012R2";
        [string]$description = "ABCcorp Infra Systems";
        [string]$OSCustomizationspec = "ABCcorp";
        [string]$AD1IP = "10.0.0.10";
        [string]$AD2IP = "10.0.0.11";
        [string]$SNM = "255.255.255.0";
        [string]$DG = "10.0.0.1";
        [string]$DNS = "10.0.0.2";
        [string]$HostUsername = "root";
        [string]$HostPassword = (ConvertTo-SecureString -String "ABCcorp123" -AsPlainText -Force);
        [string]$HostCredential = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $HostUsername, $HostPassword)
      )
    }
    

1 个答案:

答案 0 :(得分:0)

PowerShell参数定义不起作用。使用该特定功能/任务所需的参数为每个任务创建一个功能。要使这些功能可用,请将它们放在脚本中dot-source,或将它们放在moduleload that module中。

点源PowerShell脚本的示例:

vm-functions.ps1

function New-VSphereFolder {
    Param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string]$FolderName,

        [Parameter(Mandatory=$false, ValueFromPipeline=$true)]
        [string]$vCenterName = "vCenter.ABCcorp.com",

        [Parameter(Mandatory=$false, ValueFromPipeline=$true)]
        [string]$Username = "administrator@vsphere.local",

        [Parameter(Mandatory=$false, ValueFromPipeline=$true)]
        [string]$Password = "ABCcorp123!",

        ...
    )

    ...
}

function New-ResourcePool {
    ...
}

脚本脚本脚本,使函数在当前上下文中可用,然后使用您需要的任何函数:

PS C:\> . C:\path\to\vm-functions.ps1
PS C:\> New-VSphereFolder -FolderName ...
...

有关PowerShell脚本/函数中参数的详细信息,请参阅herehere