Friday, 18 September 2015

get crm service users and app pool identitiy and add them to sharepoint

#$SPSiteURL = 'http://lbh.ntlm/sites/rnbtest'
#$GroupName = 'CRM Docs Members '

[CmdletBinding()]
param ([Parameter(Mandatory=$true)] $SPSiteURL,[Parameter(Mandatory=$true)] $GroupName)
$ErrorActionPreference = "Stop"
 

$CRMAPPUsers = Get-WmiObject win32_service | Where-Object{ ($_.Name -like "MSCRMUnzipService") -or ($_.Name -like "MSCRMSandboxService") -or ($_.Name -like 'MSCRMAsyncService$maintenance') -or ($_.Name -like "MSCRMAsyncService") } |%{$_.StartName}|Get-Unique

$CRMAppUser = Get-WmiObject -Class IISApplicationPoolSetting -namespace "root\microsoftiisv2" | Where-Object {$_.Name -like 'W3SVC/APPPOOLS/CRMAppPool'}|%{$_.WAMUserName}
$Users = $CRMAsyncUser , $CRMAPPUsers
$Users = $Users|Get-Unique
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$GroupName ='RNBTest Members'
$web = Get-SPWeb -Identity $SPSiteURL
foreach ($User in $Users) {
$User
$web.EnsureUser($User)
$SPUser = get-spuser -web $sPSiteURL|where-object{$_.userlogin -like "*$User*"} |%{$_.Userlogin}

Set-SPUser -UserAlias $SPUser -Web $SPSiteURL -Group $GroupName
#stsadm -o adduser -url $SPSiteURL -userlogin $User -useremail "email@dummy.com" -group $group -username 'dummyusername'
}

No comments:

Post a Comment