## create crm connction function, call this function with arguments OrganizationURL, UserName, Password
function Invoke-CRMConn {
## pass arguments OrganizationURL, UserName, Password
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)] [string]$OrganizationURL,
[Parameter(Position=1, Mandatory=$true)] [string]$UserName,
[Parameter(Position=2, Mandatory=$true)] [string]$Password
)
## get directory path
$current_directory = $(Get-Item ($MyInvocation.ScriptName)).DirectoryName
## load crm & xrm dll
$xrm_dll = $current_directory + "/microsoft.xrm.sdk.dll"
$crm_dll = $current_directory + "/microsoft.crm.sdk.proxy.dll"
[void][System.Reflection.Assembly]::LoadFile($xrm_dll)
[void][System.Reflection.Assembly]::LoadFile($crm_dll)
[void][System.Reflection.Assembly]::LoadWithPartialName("system.servicemodel")
## create credentials
$clientCredentials = new-object System.ServiceModel.Description.ClientCredentials
$clientCredentials.UserName.UserName = $UserName
$clientCredentials.UserName.Password = $Password
## create organization service proxy
$service = new-object Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy($OrganizationURL, $null, $clientCredentials, $null)
## use default crm time out or set 5 minutes of timeout.
#$service.Timeout = new-object System.Timespan(0, 5, 0)
## return service
return $service
}
## test the service using who i am request
$service1 = Invoke-CRMConn -OrganizationURL "https://dns.com/XRMServices/2011/Organization.svc" -UserName 'test@dns.com' -Password ABC123!@#'
$request = new-object Microsoft.Crm.Sdk.Messages.WhoAmIRequest
$service1.Execute($request)
<#
UserId : 123ddfa0-9534-f234-8780-78900568c2fc0
BusinessUnitId : 456a043cf-0c34-e234-878c-78900568c6c1c
OrganizationId : 7895b3c2-0c34f-e234-878c-7890568c6c1c
ResponseName : WhoAmI
Results : {[UserId, 123ddfa0-952d-f234-8780-7890568c6c1c], [BusinessUnitId, 456a043cf-0caf-e234-8780-7890568c6c1c],
[OrganizationId, 7895b3c2-0caf-e234-8780-7890568c6c1c]}
ExtensionData : System.Runtime.Serialization.ExtensionDataObject
#>
function Invoke-CRMConn {
## pass arguments OrganizationURL, UserName, Password
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)] [string]$OrganizationURL,
[Parameter(Position=1, Mandatory=$true)] [string]$UserName,
[Parameter(Position=2, Mandatory=$true)] [string]$Password
)
## get directory path
$current_directory = $(Get-Item ($MyInvocation.ScriptName)).DirectoryName
## load crm & xrm dll
$xrm_dll = $current_directory + "/microsoft.xrm.sdk.dll"
$crm_dll = $current_directory + "/microsoft.crm.sdk.proxy.dll"
[void][System.Reflection.Assembly]::LoadFile($xrm_dll)
[void][System.Reflection.Assembly]::LoadFile($crm_dll)
[void][System.Reflection.Assembly]::LoadWithPartialName("system.servicemodel")
## create credentials
$clientCredentials = new-object System.ServiceModel.Description.ClientCredentials
$clientCredentials.UserName.UserName = $UserName
$clientCredentials.UserName.Password = $Password
## create organization service proxy
$service = new-object Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy($OrganizationURL, $null, $clientCredentials, $null)
## use default crm time out or set 5 minutes of timeout.
#$service.Timeout = new-object System.Timespan(0, 5, 0)
## return service
return $service
}
## test the service using who i am request
$service1 = Invoke-CRMConn -OrganizationURL "https://dns.com/XRMServices/2011/Organization.svc" -UserName 'test@dns.com' -Password ABC123!@#'
$request = new-object Microsoft.Crm.Sdk.Messages.WhoAmIRequest
$service1.Execute($request)
<#
UserId : 123ddfa0-9534-f234-8780-78900568c2fc0
BusinessUnitId : 456a043cf-0c34-e234-878c-78900568c6c1c
OrganizationId : 7895b3c2-0c34f-e234-878c-7890568c6c1c
ResponseName : WhoAmI
Results : {[UserId, 123ddfa0-952d-f234-8780-7890568c6c1c], [BusinessUnitId, 456a043cf-0caf-e234-8780-7890568c6c1c],
[OrganizationId, 7895b3c2-0caf-e234-8780-7890568c6c1c]}
ExtensionData : System.Runtime.Serialization.ExtensionDataObject
#>
1 comment:
Check out Microsoft.Xrm.Data.PowerShell https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell
When you connect with that you get a $conn variable that is a serviceproxy object too. You also get a bunch of pre-made cmdlets.
Post a Comment