Skip to main content

Start User Profile Synchronization Service through Powershell cmd


Function ConvertTo-UnsecureString

([System.Security.SecureString]$string)
   {
   $unmanagedString =

[System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUni

code($string)
    $unsecureString =

[System.Runtime.InteropServices.Marshal]::PtrToStringUni

($unmanagedString)
     

[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode

($unmanagedString)
   
        return $unsecureString
     }


$syncMachine = Get-SPServer "sp2010dev"
$profApp = Get-SPServiceApplication | where {$_.Name -eq "User Profile

Service Application 1"}
$account = Get-Credential "localdev\spfarm"
if ($syncMachine.Address -eq $env:ComputerName) {
    $syncSvc = Get-SPServiceInstance -Server $env:ComputerName | where

{$_.TypeName -eq "User Profile Synchronization Service"}
    $syncSvc.Status =

[Microsoft.SharePoint.Administration.SPObjectStatus]::Provisioning
    $syncSvc.IsProvisioned = $false
    $syncSvc.UserProfileApplicationGuid = $profApp.Id
    $syncSvc.Update()
    $profApp.SetSynchronizationMachine($syncMachine.Address,

$syncSvc.Id, $account.UserName, (ConvertTo-UnsecureString

$account.Password))
}

if ($syncSvc.Status -ne "Online") {
    Write-Host "Starting User Profile Synchronization Service..."
    Start-SPServiceInstance $syncSvc
}
do {Start-Sleep 2} while ((Get-SPServiceInstance -Server

$env:ComputerName | where {$_.TypeName -eq "User Profile

Synchronization Service"}).Status -ne "Online")

Comments