Freigeben über


Adding a new managed account in SharePoint 2010 using PowerShell

This is script to add a managed account programmatically using powershell. In this script we check if the account already exists if not then add it.

Below are the variable that will hold the needed data for the creation of the managed account.

  1. $AppPoolAC: The account that needs to be added to Managed Account.
  2. $AppPoolACCred: This the location of text file in which we have the password of the account stored as plain text.

     #Adding the app pool account as managed account to the farm

     LogInfo("Adding the app pool account as managed account to the farm.")

     Try

     {

           $AccountPassword = get-content $AppPoolACCred | convertto-securestring

           $AccountCred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AppPoolAC,$AccountPassword      

           If((Get-SPManagedAccount | Where {$_.UserName -eq $AppPoolAC}) -eq $null){

                Try

                {

                     $ManagedACStatus=New-SPManagedAccount -Credential $AccountCred -ErrorAction SilentlyContinue -ErrorVariable $Myerror

                     If($ManagedACStatus -eq $null)

                     {

                           LogError("The $AppPoolAC was not added as Managed Account, please check if it is a valid account.")

                     }

                }

                Catch [System.Exception]

                {

                     LogError("$AppPoolAC can not be added as Managed Account due to:"+$_.Exception.Message)

                     Exit

                }

           }

           Else{

                LogInfo("$AppPoolAC is already exiting in Managed Account.")

           }

     }

     Catch [System.SystemException]

     {

           LogError("$AppPoolAC can not be added as Managed Account due to:"+$_.Exception.Message)

           Exit

     }