PowerShell script to update user info in Vault

PowerShell script to update user info in Vault

MOHITSINGI
Explorer Explorer
180 Views
0 Replies
Message 1 of 1

PowerShell script to update user info in Vault

MOHITSINGI
Explorer
Explorer

Hi community,
iam trying to update user info from PowerShell script and iam facing some issue wile i use like permission denied and licensing type error while i use none or client as my license type 

 

 

#region Connect to Vault and Update a Single Windows User's Display Name

# Load Vault Web Services DLL
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Autodesk Vault 2024 SDK\bin\x64\Autodesk.Connectivity.WebServices.dll") | Out-Null #add you path if your dll is present in some other location

# Set Vault connection parameters
$serverIdentities = New-Object Autodesk.Connectivity.WebServices.ServerIdentities
$serverIdentities.DataServer = "" #server ip or name
$serverIdentities.FileServer = "" #server ip or name

$vaultName = "" #vault name
$userName = "" #username to login with
$securePassword = Read-Host 'Enter Vault Password' -AsSecureString
$passwordPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($passwordPtr)

# Licensing agent
$licenseType = [Autodesk.Connectivity.WebServices.LicensingAgent]::none #facing issue here if i use none i get permission denide error and if i use clent or server i get licenseing erroe

# Create credentials and connect
$userPasswordCredentials = New-Object Autodesk.Connectivity.WebServicesTools.UserPasswordCredentials(
$serverIdentities,
$vaultName,
$userName,
$password,
$licenseType
)

try {
$serviceManager = New-Object Autodesk.Connectivity.WebServicesTools.WebServiceManager($userPasswordCredentials)
Write-Host "Connected to Vault: $vaultName"

# Username to update (current Vault username)
$targetUsername = Read-Host "Enter the current Vault username of the user you want to update"

 

# Fetch all users
$users = $serviceManager.AdminService.GetAllUsers()

# Find target user
$user = $users | Where-Object { $_.Name -eq $targetUsername }

if ($user -eq $null) {
Write-Host "User '$targetUsername' not found."
} elseif ($user.Auth -ne [Autodesk.Connectivity.WebServices.AuthTyp]::Windows) {
Write-Host "User '$targetUsername' is not Windows-authenticated. Skipping."
} elseif (-not $user.Email -or -not $user.Email.Contains("@")) {
Write-Host "User '$targetUsername' does not have a valid email. Skipping."
} else {
# Extract initials from email
$initials = ($user.Email.Split("@")[0]).Substring(0, [Math]::Min(4, $user.Email.Split("@")[0].Length)).ToUpper()

# Fetch complete user info (roles, vaults, attributes)
$userInfo = $serviceManager.AdminService.GetUserInfoByUserId($user.Id)

$userId = $user.Id
$userName = $user.Name # Keep original username (you cannot change this in Vault)
$email = $user.Email
$isActive = $user.IsAct
$roleIds = $userInfo.Roles | ForEach-Object { $_.Id }
$vaultIds = $userInfo.Vaults | ForEach-Object { $_.Id }
$attributes = $userInfo.Attributes

# Update FullName attribute to initials
foreach ($attr in $attributes) {
if ($attr.Name -eq "FullName") {
$attr.Val = $initials
}
}

Write-Host "Updating display name (FullName) of user '$userName' to '$initials'..."

# Update the user info
$serviceManager.AdminService.UpdateUserInfo(
$userId,
$userName,
$email,
$isActive,
$roleIds,
$vaultIds,
$attributes
)

Write-Host "Update complete for user '$userName'."
}

} catch {
Write-Error "Error occurred: $_"
} finally {
if ($serviceManager -ne $null) {
$serviceManager.Dispose()
}
}

#endregion

and do review the code Onces if iam doing it in the right way 

0 Likes
181 Views
0 Replies
Replies (0)