PowerShell Example: Create a User
The following example shows how to create a CSM User account using PowerShell.
# Set server login variables $serverName = "your server" $apiKey = "your client id" $userName = "CSDAdmin" $password = "CSDAdmin" $baseUri = "http://${serverName}/CherwellAPI/" # Get an access token $tokenUri = $baseUri + "token" $authMode = "Internal" $tokenRequestBody = @{ "Accept" = "application/json"; "grant_type" = "password"; "client_id" = $apiKey; "username" = $userName; "password"= $password } $tokenResponse = Invoke-RestMethod -Method POST -Uri "${tokenUri}?auth_mode=${authMode}&api_key=${apiKey}" -Body $tokenRequestBody $requestHeader = @{ Authorization = "Bearer $($tokenResponse.access_token)" } # Get the business object summary for UserInfo. This will give us the busObId $summaryUri = $baseUri + "api/V1/getbusinessobjectsummary/busobname/UserInfo" $summaryResults = Invoke-RestMethod -Method GET -Uri $summaryUri -ContentType application/json -Header $requestHeader $busObId = $summaryResults[0].busobId # Get the security group information $securityGroupUri = $baseUri + "api/V2/getsecuritygroups" $securityGroupResponse = Invoke-RestMethod -Method GET -Uri $securityGroupUri -ContentType application/json -Header $requestHeader $adminGroup = $securityGroupResponse.securityGroups | Where-Object {$_.groupName -eq "Admin"} # Get the template for UserInfo. Use this to get field ids $templateUri = $baseUri + "api/V1/getbusinessobjecttemplate" $templateRequestBody = @{ busObId = $busObId includeAll = $true } | ConvertTo-Json $templateResponse = Invoke-RestMethod -Method POST -Uri $templateUri -ContentType application/json -Header $requestHeader -Body $templateRequestBody # Create the save request $userSaveUri = $baseUri + "api/V2/saveuser" $userSaveRequest = @{ AccountLocked = $false; BusObId = $busObId; DisplayName = "Test User"; LdapRequired = $false; LoginId = "Test"; NextPasswordResetDate = $null; Password = "P@ssword"; PasswordNeverExpires = $true; SecurityGroupId = $adminGroup.groupId; UserCannotChangePassword = $false; UserMustChangePasswordAtNextLogin = $false; UserInfoFields = @( @{ Dirty = $true; Name = "FullName"; Value = "Test User"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "FullName"}).fieldId }; @{ Dirty = $true; Name = "EmployeeID"; Value = "123456"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "EmployeeID"}).fieldId }; @{ Dirty = $true; Name = "Comments"; Value = "Created by API in PowerShell"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "Comments"}).fieldId }; @{ Dirty = $true; Name = "Department"; Value = "IT"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "Department"}).fieldId }; @{ Dirty = $true; Name = "Office"; Value = "Colorado Springs"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "Office"}).fieldId }; @{ Dirty = $true; Name = "Phone"; Value = "719-777-7777"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "Phone"}).fieldId }; @{ Dirty = $true; Name = "CellPhone"; Value = "719-777-7778"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "CellPhone"}).fieldId }; @{ Dirty = $true; Name = "FirstName"; Value = "Test"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "FirstName"}).fieldId }; @{ Dirty = $true; Name = "MiddleInitial"; Value = "C"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "MiddleInitial"}).fieldId }; @{ Dirty = $true; Name = "LastName"; Value = "User"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "LastName"}).fieldId }; @{ Dirty = $true; Name = "HomePhone"; Value = "719-777-7779"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "HomePhone"}).fieldId }; @{ Dirty = $true; Name = "Address"; Value = "1234 Cherwell Ave"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "Address"}).fieldId }; @{ Dirty = $true; Name = "City"; Value = "Colorado Springs"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "City"}).fieldId }; @{ Dirty = $true; Name = "ProvinceStateName"; Value = "CO"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "ProvinceStateName"}).fieldId }; @{ Dirty = $true; Name = "PostalCodeZip"; Value = "80132"; FieldId = ($templateResponse.fields | Where-Object {$_.name -eq "PostalCodeZip"}).fieldId } ) } | ConvertTo-Json # Create the user $userSaveResponse = Invoke-RestMethod -Method POST -Uri $userSaveUri -Header $requestHeader -ContentType application/json -Body $userSaveRequest