PowerCLI

 View Only
  • 1.  Passing command line arguments in PowerCli script

    Posted Dec 21, 2011 09:32 AM

    Hi All,

    I know that we can connect to any vmware server using Connect-VIServer cmdlet in our script by providing all credentials of server but I want to pass it through command line.

    I am able to connect the server by hardcoded login information in the script using Connect-VIServer cmdlet but I want that server will collect all necessary login information from command line and assign it which are required for Connect-VIServer to connect the server.

    For example, I am having test1.ps1 script and when I will execute the script with arguments,it should be taken by Connect-VIServer cmdlet to connect the server written in the script.

    [vSphere PowerCLI] C:\Scripts> C:\Scripts\test1.ps1 Server_IP_Address User_Name Password

    I appreciate the effort in advance :smileyhappy:

    Thanks!



  • 2.  RE: Passing command line arguments in PowerCli script

    Posted Dec 21, 2011 11:46 AM

    If you begin your PowerCLI script with:

    param($Server,$User,$Password)
    Connect-VIserver -Server $Server -User $User -Password $Password


    you can call it as in your example. The param() line defines the parameters you can specify on the command line. You can also call your script with:

    C:\Scripts\test1.ps1 -Server Server_IP_Address -User User_Name -Password Password

    Regards, Robert



  • 3.  RE: Passing command line arguments in PowerCli script

    Posted Dec 22, 2011 06:58 AM

    Thanks a lot for reply.

    Now I have written the script and want to add the following in the script attached in the post–

    • There should check for the command-line arguments and those are required should be checked that there is something.
    • I also want to check and error handling if the first argument is not a valid address or hostname for a VMware server or if the login information is incorrect? If any of them are not correct, error should be written in log file and exit from the script.
    • Check for vSphere PCLI is installed or not. If not, it should throw an error.
    • Proper Error handling at appropriate places in script and exit status.

      Please help me to implement the above. I appreciate the effort in advance.

      Thanks!!



    • 4.  RE: Passing command line arguments in PowerCli script

      Posted Dec 22, 2011 08:00 AM

      Several of these requirements are built into PowerShell.

      For example

      #requires -pssnapin VMware.VimAutomation.Core -version 5.0

      param( [parameter(Mandatory = $true)] [ValidateScript({     "vc1","vc2" -contains $_.Split('.')[0] })] $server, [parameter(Mandatory = $true)] $User, [parameter(Mandatory = $true)] $Password) Write-Host $Server, $User $Password

      • will make sure PowerCLI 5.0 is present
      • will make sure there are values for Server, User and Password
      • will make sure that the Servername contains either vc1 or vc2 for the hostname

      There are several more parameter validation possibilities. See about_Functions_Advanced_Parameters