- Mastering PowerCLI
- Sajal Debnath
- 229字
- 2021-07-09 21:47:49
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" } }

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.
- PWA入門與實踐
- 摩登創客:與智能手機和平板電腦共舞
- 從0到1:HTML+CSS快速上手
- Hands-On RESTful Web Services with Go
- PostgreSQL Replication(Second Edition)
- MongoDB權威指南(第3版)
- Drupal 8 Configuration Management
- 21天學通C++(第5版)
- C++反匯編與逆向分析技術揭秘(第2版)
- Visual Basic程序設計習題與上機實踐
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- Natural Language Processing with Python Quick Start Guide
- Modernizing Legacy Applications in PHP
- Visual Basic語言程序設計基礎(第3版)
- Java Web應用開發