German

 View Only
Expand all | Collapse all

I need a script to Import Groups with User of csv-file in vCenter

  • 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 Jun 02, 2025 11:58 AM
    First I created a file like this (as you wish to read users and groups from csv file)
    users groups
    user1 groupA
    user2 groupA
    user3 groupB

    Then I ran the script, all done


     
    Connect-VIServer -Server vcenter.example.com
     
     
    $entries = Import-Csv -Path ".\Desktop\1.csv"
     
     
    $groupedData = $entries | Group-Object -Property groups
     
    foreach ($group in $groupedData) {
        $groupName = $group.Name
        Write-Host $groupName
     
        if (-not (Get-VIAccount -Group -Name $groupName -ErrorAction SilentlyContinue)) {
            New-VIAccount -Group -Name $groupName -Domain "vsphere.local"  # change domain if needed
            Write-Host "Created group: $groupName"
        }
     
        foreach ($entry in $group.Group) {
            $username = $entry.users
     
           if (-not (Get-VIAccount -User -Name $username -ErrorAction SilentlyContinue)) {
                New-VIAccount -User -Name $username -Domain "vsphere.local" -Password (Read-Host -Prompt "Password for $username" -AsSecureString)
                Write-Host "Created user: $username"
            }
     
            Add-VIAccountToGroup -User $username -Group $groupName -Domain "vsphere.local"
            Write-Host "Added $username to $groupName"
        }
    }