PowerShell script to create a SystemUser in Microsoft CRM 2011

I wanted to bulk create a bunch of users in CRM. PowerShell and the Microsoft CRM 2011 REST/OData endpoint make it easy. Here is a function to create a SystemUser via PowerShell

FUNCTION JBMURPHY-CRM-CreateSystemUser {
PARAM([string][ValidateSet("crmserver.company.com", "dev-crmserver.company.com")]$ServerName="crmserver.company.com",
[string][ValidateSet("CRMOrganizationName")]$OrganizationName="CRMOrganizationName",
[string]$BusinessUnitId="BusinessUnitGUID",
[string]$SystemUserDomain="CRMSystemUserDomain",
[string][parameter(Mandatory=$true)]$FirstName,
[string][parameter(Mandatory=$true)]$LastName,
[string][parameter(Mandatory=$true)]$UserName,[switch]$MyDebug
)
[string]$url="http://$ServerName/$($OrganizationName)/xrmservices/2011/OrganizationData.svc/SystemUserSet"

$SystemUserInfo = @{
DomainName="$($UserName)@$($SystemUserDomain)"
FirstName=$FirstName
LastName=$LastName
BusinessUnitId=@{LogicalName="businessunit";Id=$BusinessUnitId}
}
$assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$json=new-object System.Web.Script.Serialization.JavaScriptSerializer
$SystemUserInfoData=$json.Serialize($SystemUserInfo)

$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $url, $false)
$http_request.setRequestHeader("Accept", "application/json")
$http_request.setRequestHeader("Content-Type", "application/json; charset=utf-8")
$results=$http_request.send($SystemUserInfoData)
if ($MyDebug){
$http_request.statusText
$http_request
}
$SystemUserId=$($json.DeserializeObject($http_request.responseText)).d.SystemUserId
return $SystemUserId
}

,

5 Responses to PowerShell script to create a SystemUser in Microsoft CRM 2011

  1. John K July 8, 2014 at 5:24 pm #

    Are you passing the business unit id GUID as a string with hyphens? How are you getting the BU GUID? does that come off the BU link properties? I presume the BU logical name is “My Business Unit”?

  2. John K July 8, 2014 at 5:26 pm #

    Is a “SystemUser” just a CRM User?

  3. jbmurphy July 8, 2014 at 5:34 pm #

    yes

  4. jbmurphy July 8, 2014 at 5:41 pm #

    Yes hyphens. I looked at the existing users in the REWST view:
    http://your.servername.com/OrgName/xrmservices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId

  5. Eddie August 18, 2015 at 2:51 pm #

    Hey jbmurphy, i am very interest in getting this working in my organization.. first, is it possble to management roles through your script? second, are all the variables pass in the command line; example
    CRM-CreateSystemUser “”john”,”thompson”. is businessunitid and systemuserdomain defined within the script, or command line? Thanks!