Hello,
Here's an updated version of the script that sorts office names alphabetically before updating the Property Pattern:
$databaseHost = "host.company.com" #TODO: modify me
$databaseName = "MyDatabase" # TODO: modify me
$isOfficePropertyRequired = $True
# Use the credentials of the default Adaxes administrator
# to connect to the database.
$databaseUsername = $NULL
$databasePassword = $NULL
# Get the office name
$connectionString = "Data Source=$databaseHost; Initial Catalog=$databaseName;"
if ($databaseUsername -eq $NULL)
{
$connectionString = $connectionString +
"Integrated Security=SSPI;"
}
else
{
$connectionString = $connectionString +
"User ID=$databaseUsername;Password=$databasePassword;"
}
$connection = New-Object "System.Data.SqlClient.SqlConnection" $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = "SELECT ADOfficeName FROM Offices"
$reader = $command.ExecuteReader()
$officesName = @()
while ($reader.Read())
{
$officesName += $reader["ADOfficeName"]
}
$reader.Close()
$command.Dispose()
$connection.Close()
if($officesName.Length -eq 0)
{
return
}
# Sort office names in the array
[System.Array]::Sort($officesName)
# Modify the User Pattern
$propertyPatternsPath = $Context.GetWellKnownContainerPath("PropertyPatterns")
$propertyPatternsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" $propertyPatternsPath
$builtinPathObj = $propertyPatternsPathObj.CreateChildPath("CN=Builtin")
$userPatternPath = $builtinPathObj.CreateChildPath("CN=User Pattern")
$userPattern = $Context.BindToObject($userPatternPath)
foreach ($item in $userPattern.Items)
{
if($item.PropertyName -eq "physicalDeliveryOfficeName")
{
$userPattern.Items.Remove($item)
break
}
}
$item = $userPattern.Items.Create()
$item.PropertyName = "physicalDeliveryOfficeName"
$item.IsPropertyRequired = $isOfficePropertyRequired
$constraints = $item.GetConstraints()
$constraint = $constraints.Create("ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$constraint.Values = $officesName
$constraints.Add($constraint)
$item.SetConstraints($constraints)
$item.SetInfo()
$userPattern.Items.Add($item)
Here's also an updated version of the script that updates user's address after user creation. If a certain portion of the address is not specified in the database, the script just skips it, and no errors will be shown on the Web Interface:
$databaseHost = "host.company.com" #TODO: modify me
$databaseName = "MyDatabase" # TODO: modify me
# TODO: Specify country info @{"Country name" = @("two-letter country code", "Country code");
# "Country name" = @("two-letter country code", "Country code")}
$countryInfo = @{
"United States" = @("US", "840");
"Albania" = @("AL", "8");
"Argentina" = @("AR", "32");
"Australia" = @("AU", "36");
"Austria" = @("AT", "40");
"Belarus" = @("BY", "112");
"Belgium" = @("BE", "56");
"Brazil" = @("BR", "76");
"Bulgaria" = @("BG", "100");
"Canada" = @("CA", "124");
"Chile" = @("CL", "152");
"China" = @("CN", "156");
"Colombia" = @("CO", "170");
"Costa Rica" = @("CR", "188");
"Croatia" = @("HR", "191");
"Czech Republic" = @("CZ", "203");
"Denmark" = @("DK", "208");
"Estonia" = @("EE", "233");
"Finland" = @("FI", "246");
"France" = @("FR", "250");
"Germany" = @("DE", "276");
"Greece" = @("GR", "300");
"Hong Kong" = @("HK", "344");
"Hungary" = @("HU", "348");
"India" = @("IN", "356");
"Indonesia" = @("ID", "360");
"Ireland" = @("IE", "372");
"Italy" = @("IT", "380");
"Japan" = @("JP", "392");
"Kazakhstan" = @("KZ", "398");
"Democratic People's Republic of Korea" = @("KP", "408");
"Republic of Korea" = @("KR", "410");
"Latvia" = @("LV", "428");
"Lithuania" = @("LT", "440");
"Malaysia" = @("MY", "458");
"Mexico" = @("MX", "484");
"Montenegro" = @("ME", "499");
"Netherlands" = @("NL", "528");
"New Zealand" = @("NZ", "554");
"Norway" = @("NO", "578");
"Pakistan" = @("PK", "586");
"Panama" = @("PA", "591");
"Peru" = @("PE", "604");
"Philippines" = @("PH", "608");
"Poland" = @("PL", "616");
"Portugal" = @("PT", "620");
"Romania" = @("RO", "642");
"Russian Federation" = @("RU", "643");
"Saudi Arabia" = @("SA", "682");
"Serbia" = @("RS", "688");
"Singapore" = @("SG", "702");
"Slovakia" = @("SK", "703");
"South Africa" = @("ZA", "710");
"Spain" = @("ES", "724");
"Sweden" = @("SE", "752");
"Switzerland" = @("CH", "756");
"Taiwan, Province of China" = @("TW", "158");
"Thailand" = @("TH", "764");
"Turkey" = @("TR", "792");
"Ukraine" = @("UA", "804");
"United Arab Emirates" = @("AE", "784");
"United Kingdom" = @("GB", "826");
"VietNam" = @("VN", "704");
"Israel" = @("IL", "376")} # TODO: modify me
# Use the credentials of the default Adaxes administrator to connect to the database.
$databaseUsername = $NULL
$databasePassword = $NULL
$connectionString = "Data Source=$databaseHost; Initial Catalog=$databaseName;"
if ($databaseUsername -eq $NULL)
{
$connectionString = $connectionString + "Integrated Security=SSPI;"
}
else
{
$connectionString = $connectionString +
"User ID=$databaseUsername;Password=$databasePassword;"
}
$connection = New-Object "System.Data.SqlClient.SqlConnection" $connectionString
$connection.Open()
# Get the office address
$command = $connection.CreateCommand()
$command.CommandText = "SELECT OfficeID, DefaultLanguageId, CountryId FROM Offices WHERE ADOfficeName = @ADOfficeName"
$command.Parameters.Add("@ADOfficeName", "%physicalDeliveryOfficeName%") | Out-Null;
$reader = $command.ExecuteReader()
if ($reader.Read())
{
$officeID = $reader["OfficeID"]
$languageId = $reader["DefaultLanguageId"]
$countryId = $reader["CountryId"]
}
$reader.Close()
$command.Dispose()
$command = $connection.CreateCommand()
$command.CommandText = "SELECT DisplayAddress, City, StateProvince, PostalCode FROM OfficeLanguages WHERE OfficeId = @OfficeId AND LanguageId = @LanguageId"
$command.Parameters.Add("@OfficeId", $officeID) | Out-Null;
$command.Parameters.Add("@LanguageId", $languageId) | Out-Null;
$reader = $command.ExecuteReader()
if ($reader.Read())
{
$streetAddress = $reader["DisplayAddress"]
$city = $reader["City"]
$stateProvince = $reader["StateProvince"]
$postalCode = $reader["PostalCode"]
}
$reader.Close()
$command.Dispose()
$command = $connection.CreateCommand()
$command.CommandText = "SELECT Name FROM Countries WHERE CountryId = @CountryId"
$command.Parameters.Add("@CountryId", $countryId) | Out-Null;
$reader = $command.ExecuteReader()
if ($reader.Read())
{
$countryName = $reader["Name"]
}
$reader.Close()
$command.Dispose()
$connection.Close()
# Update user
if(-not([System.String]::IsNullOrEmpty($streetAddress)) -and -not(([System.Convert]::IsDBNull($streetAddress))))
{
$Context.TargetObject.Put("streetAddress", $streetAddress)
}
if(-not([System.String]::IsNullOrEmpty($city)) -and -not(([System.Convert]::IsDBNull($city))))
{
$Context.TargetObject.Put("l", $city)
}
if(-not([System.String]::IsNullOrEmpty($stateProvince)) -and -not(([System.Convert]::IsDBNull($stateProvince))))
{
$Context.TargetObject.Put("st", $stateProvince)
}
if(-not([System.String]::IsNullOrEmpty($postalCode)) -and -not(([System.Convert]::IsDBNull($postalCode))))
{
$Context.TargetObject.Put("postalCode", $postalCode)
}
if($countryInfo[$countryName] -ne $NULL)
{
$country = ($countryInfo[$countryName])[0]
$countryCode = ($countryInfo[$countryName])[1]
$Context.TargetObject.Put("co", $countryName)
$Context.TargetObject.Put("c", $country)
$Context.TargetObject.Put("countryCode", $countryCode)
}
$Context.TargetObject.SetInfo()