Automation

 View Only
  • 1.  I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 04, 2024 04:36 AM

    How can I use a PCLI script to import the groups with the users from a csv file?

    I have already tried using the Get-Group command, but I get an error message "Command not found in cmdlet..."

    vCenter version 7.5



  • 2.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 04, 2024 01:22 PM

    What kind of users,  and import into what?



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 3.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 05, 2024 01:47 AM

    Hi Luc,

    The groups and members should be imported into the vCenter 5.7. It's not about AD groups.

    The csv is in the format: Group;Description;Users




  • 4.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 05, 2024 10:40 AM

    Do you mean a local user account on the VCSA?
    Like this one Create a Local User Account in vCenter Server



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 5.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 05, 2024 12:44 PM

    Assuming that these users are defined in the SSO domain, and considering that your CSV looks something like this

    Group;Description;Users
    TestGroup;Test Group;Administrator@vsphere.local,user@vsphere.local
    TestGroup2;Second Test Group;user@vsphere.local

    you could do something like this

    #Requires -Modules 'VMware.vSphere.SsoAdmin'
    
    $vcsaName = 'vcsa.local.lab'
    $user = 'administrator@vsphere.local'
    $pswd = 'VMware1!'
    
    $ssoSrv = Connect-SsoAdminServer -Server $vcsaName -User $user -Password $pswd -SkipCertificateCheck
    
    Import-Csv -Path .\groups.csv -Delimiter ';' -PipelineVariable row |
    ForEach-Object -Process {
      $group = New-SsoGroup -Name $row.Group -Description $row.Description
      $row.Users.Split(',') |
      ForEach-Object -Process {
        $account,$domain = $_.Split('@')
        $user = Get-SsoPersonUser -Name $account -Domain $domain
        Add-UserToSsoGroup -TargetGroup $group -User $user
      }
    }
    
    Disconnect-SsoAdminServer -Server $ssoSrv
    

    This script requires the VMware.vSphere.SsoAdmin module written by Dimitar Milov



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 6.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 06, 2024 03:43 AM

    Hi Luc,

    Thanks for the script.
    I got an error message:

    Add-UserToSsoGroup: Y:\Areas\...\import_groups.ps1:57:50 Line | 57 | Add-UserToSsoGroup -TargetGroup $group -User $user
    | Cannot bind argument to parameter 'User' because it is null.

    I'm a bit confused by the variable $user! It is defined at the beginning with the admin account, but appears again at the bottom of the script and is overwritten again with $user = Get-SsoPersonUser -Name $account -Domain $domain
    The script still ran through and created the groups, but the users were only partially added to the groups. I was able to clean that up manually, though.

    Thanks for your help.

    Br the1960




  • 7.  RE: I need a script to Import Groups with User of csv-file in vCenter

    Posted Nov 06, 2024 04:18 AM

    That seems to indicate the one of the users you specified in the Users column in the CSV does not exist.

    The $user variable at the beginning is used to connect.
    The $user variable in the foreach loop is used to retrieve the user(s) you specified in the CSV.
    If you want you can rename one of these variables.



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------