Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vmware 多变量声明_Vmware_Powershell 3.0 - Fatal编程技术网

Vmware 多变量声明

Vmware 多变量声明,vmware,powershell-3.0,Vmware,Powershell 3.0,我有一个脚本,我可以将它用于多个任务,如 在vSphere中创建文件夹 创建资源池 创建虚拟机(VMware) 配置IP/DNS 将其添加到域 为此,我需要多个输入,如VM名称、vCenter服务器名称、用户ID、密码等。如何在外部声明输入变量(与主脚本分离),并使其在外观上具有通用性 我尝试使用下面的过程,但它给了我调用深度溢出错误 变量声明.ps1: VariableDeclaration = @{ [CmdletBinding()] Param ( [Parameter(M

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

  • 在vSphere中创建文件夹
  • 创建资源池
  • 创建虚拟机(VMware)
  • 配置IP/DNS
  • 将其添加到域
  • 为此,我需要多个输入,如VM名称、vCenter服务器名称、用户ID、密码等。如何在外部声明输入变量(与主脚本分离),并使其在外观上具有通用性

    我尝试使用下面的过程,但它给了我调用深度溢出错误

    变量声明.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)
      )
    }
    
    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 {
        ...
    }
    

    PowerShell参数定义不是这样工作的。使用特定功能/任务所需的参数为每个任务创建一个函数。要使函数可用,请将它们放在脚本和it中,或将它们放在和中

    点源PowerShell脚本示例:

    vm函数.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)
      )
    }
    
    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 ... ... PS C:\>。C:\path\to\vm-functions.ps1 PS C:\>新的VSphereFolder-FolderName。。。 ...
    有关PowerShell脚本/函数中参数的更多信息,请参阅和。

    您能帮我举一个小示例吗?非常感谢,如果我想包括错误/异常处理的Try Catch,请说明什么?如何以及在何处包含这些内容?请发布一个新问题,描述您希望处理哪些错误。包括您当前拥有的代码的一部分,并描述代码没有按预期工作的原因。如果您正在寻找PowerShell教程,请咨询您首选的搜索引擎。