none
Importing a multiple users Trough Powershell Scripts RRS feed

  • Question

  • Hi All

    Am completely new to this technology am having a source code for “Adding a single user to a SG Group” similarly I need to Add a multiple users through PowerShell scripts.

    Set-Variable -Name GROUPNAME -Value "<My Group>" -Option Constant
    Set-Variable -Name USERNAME -Value "<My User>" -Option Constant
    Set-Variable -Name URI -Value "http://localhost:5725/resourcemanagementservice" -Option Constant
    #--------------------------------------------------------------------------------------------------------------------
    Function GetSingleResource
    {
    Param($Filter)
    End
    {
    $exportResource = export-fimconfig -uri $URI `
    –onlyBaseResources `
    -customconfig ("$Filter")`
    -ErrorVariable Err `
    -ErrorAction SilentlyContinue
    If($Err){Throw $Err}
    If($exportResource -eq $null) {Throw "Resource not found: $Filter"}
    If(@($exportResource).Count -ne 1) {Throw "More than one resource found: $Filter"}
    $exportResource
    }
    }
    #--------------------------------------------------------------------------------------------------------------------
    If(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation}
    Clear-Host
    $ProgressPreference = "SilentlyContinue"
    #--------------------------------------------------------------------------------------------------------------------
    $ProgressPreference = "Continue"
    Write-Progress -Activity "Retrieving group object" `
    -Status "Please wait..." `
    -CurrentOperation "Processing step 1 of 3"
    $ProgressPreference = "SilentlyContinue"
    $groupObject = GetSingleResource -Filter "/Group[DisplayName='$GROUPNAME']"

    $ProgressPreference = "Continue"
    Write-Progress -Activity "Retrieving user object" `
    -Status "Please wait..." `
    -CurrentOperation "Processing step 2 of 3"
    $ProgressPreference = "SilentlyContinue"
    $userObject = GetSingleResource -Filter "/Person[DisplayName='$USERNAME']"

    $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
    $importChange.Operation = 0
    $importChange.AttributeName = "ExplicitMember"
    $importChange.AttributeValue = $userObject.ResourceManagementObject.ObjectIdentifier
    $importChange.FullyResolved = 0
    $importChange.Locale = "Invariant"

    $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
    $importObject.ObjectType = "Group"
    $importObject.TargetObjectIdentifier = $groupObject.ResourceManagementObject.ObjectIdentifier
    $importObject.SourceObjectIdentifier = $groupObject.ResourceManagementObject.ObjectIdentifier
    $importObject.State = 1
    $ImportObject.Changes = (,$ImportChange)

    $ProgressPreference = "Continue"
    Write-Progress -Activity "Adding user to group" `
    -Status "Please wait..." `
    -CurrentOperation "Processing step 3 of 3"
    $ProgressPreference = "SilentlyContinue"

    $importObject | Import-FIMConfig -Uri $URI -ErrorVariable Err -ErrorAction SilentlyContinue | Out-Null
    If($Err){Throw $Err}
    #--------------------------------------------------------------------------------------------------------------------
    Write-Host "Command completed successfully"
    #--------------------------------------------------------------------------------------------------------------------
    Trap
    {
    Write-Host $_.Exception.Message -foregroundcolor white -backgroundcolor darkred
    Write-Host $_.Exception.GetType().FullName -foregroundcolor white -backgroundcolor darkred
    Write-Host "`n"
    Exit 1
    }
    #--------------------------------------------------------------------------------------------------------------------

    Regards:

    Rajesh.k


    Tuesday, June 17, 2014 4:11 AM

All replies