Hello all, I am currently trying to get ARM to run a script that move the user to a specific OU and to add it to specific group when using an open template to terminate a user. I created the Open template using the ARM Template generator. the PS script I am trying to run is this:
param(
[string] $UserName,
[string] $targetDate,
)
try {
# Asegurarse de que el script se ejecute incluso sin salida en la consola
$ProgressPreference = "SilentlyContinue"
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$loginUrl = $baseUrl + "/Session/loginWithToken?token=$authZToken"
$result = Invoke-RestMethod -uri $loginUrl -Method Get -SessionVariable websession
Write-Host "Logon Result: $result"
if (!$result.Success) {
exit 5
}
$cookies = $websession.Cookies.GetCookies($loginUrl)
$session.Cookies.Add($cookies)
#------------------------------------------------
# Configuración inicial
$ADUser = $accountDn
$EmailGroup = "Mail group NAME"
$ExcludeGroup = "MAIL GROUP TO EXCLUDE"
$TargetOU = "OU=TargetOU,OU=Site,DC=fqm,DC=COM"
# Función para agregar un usuario a un grupo
function Add-UserToGroup {
param(
[string]$UserName,
[string]$GroupName
)
$User = Get-ADUser $UserName
if ($User.Email -ne $null) {
Add-ADGroupMember -Identity $GroupName -Members $User
}
}
# Función para remover un usuario de todos los grupos excepto uno
function Remove-UserFromGroups {
param(
[string]$UserName,
[string]$ExcludeGroupName
)
$User = Get-ADUser $UserName
$User | Get-ADPrincipalGroupMembership | Where-Object {$_.Name -ne $ExcludeGroupName} | ForEach-Object {
Remove-ADGroupMember -Identity $_ -Members $User -Confirm:$false
}
}
# Función para limpiar el campo de manager de un usuario
function Clear-UserManager {
param(
[string]$UserName
)
Set-ADUser $UserName -Manager $null
}
# Función para mover un usuario a una OU específica
function Move-UserToOU {
param(
[string]$UserName,
[string]$TargetOU
)
Move-ADObject -Identity $UserName -TargetPath $TargetOU
}
# Lógica principal
Add-UserToGroup -UserName $ADUser -GroupName $EmailGroup
Remove-UserFromGroups -UserName $ADUser -ExcludeGroupName $ExcludeGroup
Clear-UserManager -UserName $ADUser
Move-UserToOU -UserName $ADUser -TargetOU $TargetOU
I am not an expert on PowerShell either on coding so will appreciate if you can provide me help about how to make it works on ARM. I have been looking for tutorials but there is not enough information about it. Any help will be fully appreciated.
this is the code of the Open template:
[
{
"Version": 1,
"TemplateType": "OpenTemplate",
"Id": "68421248-a1d7-4a02-972e-533412254cd5",
"DisplayName": "User Termination",
"Description": "Terminate all user access and disabled the account. ",
"IsManualInteractionRequired": "False",
"ScriptToExecute": "User Terminationv2",
"Form": {
"Type": "Container",
"Label": "User Termination",
"Templates": [
{
"Key": "SAMAccountName",
"Value": {
"Type": "AccountSearchTextField",
"Label": "User ID",
"Description": "Network user ID used to logon on FQML network",
"LookupTableId": "",
"IsRequired": true,
"AttributesToLoad": [
""
]
}
},
{
"Key": "targetDate",
"Value": {
"Type": "DatePicker",
"Label": "Date to be deactivated",
"Description": "Please specify when the account needs to be terminated. ",
"ScriptParameterFormat": "M"
}
}
]
}
}
]