Friday, May 17, 2013

How to get a list of all Managed service accounts on a SharePoint 2010


We can use the below Powershell cmd to get the all Managed service accounts on a SharePoint 2010

PS C:\> Get-SPManagedAccount





But we have some issues like above and PowerShell truncates table by default.
Here are the results of that command, showing a table with column names that span multiple lines and rows with data that is not useful:

If you try to solve this problem by looking at the help for Format-Table, you may come across the AutoSize parameter.  The AutoSize parameter allows you to tell PowerShell that you want the formatter to automatically size the table columns, showing as much data as possible.  Here is what happens if you add the AutoSize parameter to the same command you just ran:

PS C:\> Get-SPManagedAccount | Format-Table -Property * -AutoSize





This is on the right track for what you are after, but notice the warning text that is output at the top of the resulting table.  It indicates that 10 columns do not fit into the display and were removed.  If you pipe the results of this command to Out-File, PowerShell will simply pass what you see on the screen to the file you are writing to, which is not sufficient for your needs.


Solution: 

Using Parameters for Format-Table like below

PS C:\> Get-SPManagedAccount | Format-Table -Property UserName
,PasswordExpiration, AutomaticChange –AutoSize

Using Out-String and width if you have many parameters to pass

PS C:\Users\sg-tst-spsql> Get-SPManagedAccount | Format-Table -Property *
 -AutoSize | Out-String -Width 4096




The above command that might not look very useful either, but look what happens when you take this one step further and pass the results to Out-File.  Here is the command to do this:
3. Using Out File

Get-SPManagedAccount 
| Format-Table -Property * -AutoSize | Out-String -Width 4096 
| Out-File C:\ServiceAccount.txt





Opening the resulting ServiceAccount.txt file shows the following contents:


Note: In your notepad, Go to Format and Uncheck the Word Wrap if its checked to get the above result.

Reference:

No comments:

Post a Comment