官术网_书友最值得收藏!

Switch parameters

Switch parameters are parameters without any need to assign any value to them. As the name suggests, they act as a switch. By default, their values are $false. Once these parameters are mentioned in the command line, their values become $true. We can use this switch parameter to perform our checks and run their respective script blocks. The following example is a function that connects to a vCenter server or vCloud Director server based on the input that we provide. As parameter values, we accept ServerName and UserName to connect to the server, Password for the connection, and another parameter that will act as a switch. If we mention –VCServer in the command line, then the Connect-VIServer cmdlets will be executed; if –VCDServer is mentioned, then the Connect-CIServer cmdlets will be executed. So, run the following code snippet:

Function Connect-Server{
[CmdletBinding()]
 Param(
    [Parameter(Mandatory=$true)]
            [string]$ServerName,
            [string]$UserName,
       [String]$Pass,
            [switch]$VCServer,
       [Switch]$VCDServer
            )

   If ($VCServer) {
        # Connect-VIServer –Server $VCServer –User -$Username –Password $Pass
        Write-Host "vCenter Server: $ServerName User: $UserName Password: $Pass"
        }
   If ($VCDServer) {
        # Connect-CIServer –Server $VCDServer –User -$Username –Password $Pass
        Write-Host "vCD Server: $ServerName User: $UserName Password: $Pass"
        }  
}
Switch parameters

Since I do not have a vCD environment ready, I have decided to just show the values to the console so that the way they work can be understood.

主站蜘蛛池模板: 常德市| 沈丘县| 漳浦县| 绿春县| 英超| 鹤岗市| 扬州市| 新源县| 广南县| 夏津县| 沙雅县| 沙坪坝区| 清新县| 襄垣县| 清河县| 交口县| 伊川县| 大竹县| 富锦市| 玉山县| 浙江省| 克拉玛依市| 广昌县| 民勤县| 金坛市| 顺义区| 永登县| 泗水县| 承德市| 崇州市| 武平县| 饶阳县| 昌邑市| 尼玛县| 北票市| 丰城市| 莒南县| 仪陇县| 临朐县| 竹溪县| 孟津县|