PowerCLI

 View Only
Expand all | Collapse all

Function not Functional

  • 1.  Function not Functional

    Posted Nov 13, 2023 03:28 PM

    I create the below function in order to connect to vCenter, unfortunately when I run it nothing happen

    [CmdletBinding()]
        param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Server,
       
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Username,
       
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [SecureString]$Password

        )
        try {
            # Import the VMware PowerCLI module
            Import-Module -Name VMware.PowerCLI -ErrorAction Stop

            # Connect to vCenter
            Connect-VIServer -Server $Server -User $Username -Password $Password

            # Output success message
            Write-Output "Connected to vCenter successfully"
        }
        catch {
            # Log the error
            Write-Error "Failed to connect to vCenter: $_"
        }
    }


  • 2.  RE: Function not Functional

    Posted Nov 13, 2023 03:31 PM

    How do you call that function?
    I assume you did include a function line at the start?



  • 3.  RE: Function not Functional

    Posted Nov 13, 2023 03:39 PM

    No, I didn't include that at the start



  • 4.  RE: Function not Functional

    Posted Nov 13, 2023 03:46 PM

    You have to give some more details.
    Did you store that code in a separate .ps1 file?
    Did you dot-source that .ps1 file to make the function "known" to the PS engine?
    Or is everything stored in a single .ps1 file?



  • 5.  RE: Function not Functional

    Posted Nov 13, 2023 03:51 PM

    All things are stored on a single file.

    The idea is that I would like to have a function, that give as the way to entrer vCenter detail then user name and login

    The same function should check the required modules



  • 6.  RE: Function not Functional

    Posted Nov 13, 2023 05:05 PM

    If it is all in the same .ps1 file, you should have a function statement.
    And then call that function

    Something like this

    function Get-WhatEver {
    
    # function statements
    
    }
    
    # call the function
    Get-WhatEver


  • 7.  RE: Function not Functional

    Posted Nov 13, 2023 05:47 PM

    Thanks got it  

    Just a question why I get the below message when I run the script? is there a way to hide it?

    cmdlet Connect-VCenter at command pipeline position 1
    Supply values for the following parameters:



  • 8.  RE: Function not Functional

    Posted Nov 13, 2023 06:36 PM

    You made all the parameters Mandatory, so you should provide these mandatory parameter when you call the function



  • 9.  RE: Function not Functional

    Posted Nov 13, 2023 06:53 PM
    function Connect-VCenter {
        [CmdletBinding()]
        param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Server,
        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Username,
        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [SecureString]$Password
    
        )
        try {
            # Import the VMware PowerCLI module
            Import-Module -Name VMware.PowerCLI -ErrorAction Stop
    
            # Connect to vCenter
            Connect-VIServer -Server $Server -User $Username -Password $Password
    
            # Output success message
            Write-Output "Connected to vCenter successfully"
        }
        catch {
            # Log the error
            Write-Error "Failed to connect to vCenter: $_"
        }
    }
    Connect-VCenter

    I got the below error message

    Failed to connect to vCenter: Method 'ConnectProcessRecordInternal' in type 'VMware.Sdk.Nsx.Policy.ConnectNsxServer' from assembly 'VMware.Sdk.Nsx.Policy.Cmdlets, Version=0.0.0.0, Culture=neutral,
    | PublicKeyToken=null' does not have an implementation.

     

     



  • 10.  RE: Function not Functional

    Posted Nov 13, 2023 07:01 PM

    It is hard to believe you got that far since you provided no parameters on your call to Connect-VCenter, and all the parameters are mandatory.



  • 11.  RE: Function not Functional

    Posted Nov 13, 2023 07:10 PM

    I manually typed in the entries in the console.
    I provided the IP address of the vcenter, followed by the username and password.



  • 12.  RE: Function not Functional

    Posted Nov 13, 2023 07:19 PM

    Did you dot-source the .ps1 file before?
    I'm not sure what exactly you are doing, but you should run the .ps1 file, not call the function from the prompt (unless you dot-sourced it before).



  • 13.  RE: Function not Functional

    Posted Nov 13, 2023 07:28 PM

    Did you dot-source the .ps1 file before? No

    My idea is that when I run the script I should enter Ip or vCenter FQDN followed by the login and user.

    So when I run the script without calling the function nothing happen, when I add the call function it gaves me the possibility to enter detail bu failed with error message.



  • 14.  RE: Function not Functional
    Best Answer

    Posted Nov 13, 2023 08:10 PM

    Try leaving out the ErrorAction on the Import-Module.

    Import-Module -Name VMware.PowerCLI