How to get the Application Group, SID & UserName in NetSqlAZMAN

462 views Asked by At
$AppName = "MyAppName"

#Open the store
$AzStore = New-Object -COMobject AzRoles.AzAuthorizationStore

#Access the App
$MyApp = $AzStore.OpenApplication($AppName)

Can anyone please help me in completing this.

1

There are 1 answers

0
Giorgi Chakhidze On

You might find this example useful:

# This internal function will download latest NetSqlAzMan.dll from NuGet (if necessary)
# and import it into current runspace.
# You can manually download installation package from netsqlazman.codeplex.com and
# Add-Type it directly instead of this function
function _DownloadAndImportLatestNetSqlAzManDll
{
    $DownloadUrl = 'https://api.nuget.org/packages/netsqlazman-x86.3.6.0.15.nupkg'
    $LocalDir    = Join-Path $env:TEMP 'NetSqlAzManx86'
    $LocalNupkg  = Join-Path $LocalDir 'netsqlazman-x86.3.6.0.15.nupkg'
    $DllPath     = Join-Path $LocalDir 'lib\net40\NetSqlAzMan.dll'

    if (-not (Test-Path $LocalDir)) {
        New-Item -Path $LocalDir -ItemType Directory -Force | Out-Null
    }

    if (-not (Test-Path $LocalNupkg)) {
        Invoke-WebRequest -Uri $DownloadUrl -Method Get -OutFile $LocalNupkg | Out-Null
    }

    if (-not (Test-Path $DllPath)) {
        Add-Type -AssemblyName System.IO.Compression.FileSystem
        [System.IO.Compression.ZipFile]::ExtractToDirectory($LocalNupkg, $LocalDir)
    }

    Add-Type -Path $DllPath
}

_DownloadAndImportLatestNetSqlAzManDll

# Initialization:
$ConnectionString = 'Server=MySQLServerHostName; Database=NetSqlAzManStorage; Integrated Security=True'
$AppStoreName     = 'MyAppStoreName'
$AppName          = 'MyAppName'
$AppGroupName     = 'MyAppGroupName'
$AzStorage        = New-Object NetSqlAzMan.SqlAzManStorage($ConnectionString)
$AzStore          = $AzStorage.GetStore($AppStoreName)
$AzApp            = $AzStore.GetApplication($AppName)

# Example usage:

$UserName = ''
$Members = $AzApp.GetApplicationGroup($AppGroupName).GetApplicationGroupAllMembers()

"Members of application group $AppGroupName are:"

foreach ($Member in $Members)
{
    $Member.GetMemberInfo([ref] $UserName) | Out-Null

    "User SID: $($Member.SID.StringValue)"
    "User Display Name: $UserName"
}

# See NetSqlAzMan API reference:  http://netsqlazman.codeplex.com/downloads/get/348377

# Cleanup:
$AzApp.Dispose()
$AzStore.Dispose()
$AzStorage.Dispose()