Hello,
Thank you for all the provided details. Below are the two scripts to fulfil the requirements.
Script for new user creation
The script should be executed in a business rule triggering After creating a user. In the script:
- $csvFilePath – Specifies the path to the CSV file.
- $userNamePropertyName – Specifies the LDAP name of the property whose value will be added to the User column of the CSV file.
- $telephonePropertyName – Specifies the LDAP name of the property that will be updated with the value of the phone number taken from the CSV file.
$csvFilePath = "\\Server\Share\PhoneList.csv" # TODO: modify me
$userNamePropertyName = "samaccountName" # TODO: modify me
$telephonePropertyName = "telephoneNumber" # TODO: modify me
# Import CSV
$records = import-csv $csvFilePath
# Get user name
try
{
$username = $Context.TargetObject.Get($userNamePropertyName)
}
catch
{
$Context.LogMessage("User %fullname% has no name specified in property $userNamePropertyName.", "Warning")
return
}
$phoneNumerbFounded = $False
foreach ($record in $records)
{
if (![System.String]::IsNullOrEmpty($record.User))
{
continue
}
# Update the user
$Context.TargetObject.Put($telephonePropertyName, $record."Phone Number")
$Context.TargetObject.SetInfo()
$record.User = $username
$record."Assigned Date" = "%whenCreated:format[dd.MM.yyyy]%"
$phoneNumberFounded = $True
break
}
if (!$phoneNumberFounded)
{
$Context.LogMessage("No available phone number found", "Warning")
return
}
# Update the CSV file
$records | Export-Csv -Path $csvFilePath -Force -NoTypeInformation
Script for user deprovisioning
The script be executed as part of your deprovisioning process (e.g. in a custom command). In the script:
- $csvFilePath – Specifies the path to the CSV file.
- $telephonePropertyName – Specifies the LDAP name of the property that stores the phone number. Must be the same as in the first script.
$csvFilePath = "\\Server\Share\PhoneList.csv" # TODO: modify me
$telephonePropertyName = "telephoneNumber" # TODO: modify me
# Get user phone number
try
{
$phoneNumber = $Context.TargetObject.Get($telephonePropertyName)
}
catch
{
$Context.LogMessage("User %fullname% has no phone number specified in property $telephonePropertyName.", "Warning")
return
}
# Import CSV
$records = import-csv $csvFilePath
$phoneNumerbFounded = $False
foreach ($record in $records)
{
if ($record."Phone Number" -ne $phoneNumber)
{
continue
}
# Update the user
$Context.TargetObject.Put($telephonePropertyName, $NULL)
$Context.TargetObject.SetInfo()
# Update CSV file record
$record.User = ""
$record."Assigned Date" = ""
$phoneNumberFounded = $True
break
}
if (!$phoneNumberFounded)
{
$Context.LogMessage("No record matching phone number '$phoneNumber' found", "Warning")
return
}
# Update the CSV file
$records | Export-Csv -Path $csvFilePath -Force -NoTypeInformation