-
Notifications
You must be signed in to change notification settings - Fork 4
/
TKOnPremCommands.psm1
327 lines (287 loc) · 14 KB
/
TKOnPremCommands.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Some on-prem goodies.
# Originally published at https://github.com/ToddKlindt/PowerShell
# Also check out https://www.toddklindt.com
function Get-TKSPServiceAccount {
<#
.Synopsis
Gets all the accounts used for services by on-prem SharePoint Server.
.DESCRIPTION
Gets all the accounts used for services by on-prem SharePoint Server. Must be run on a SharePoint server by a local admin in an admin PowerShell window. Most of the functional code came from https://info.summit7.us/blog/retrieve-all-sharepoint-service-accounts-with-powershell. I prettied it up some, but all the props go to Michael Wilke.
.EXAMPLE
Get-TKSPServiceAccount
.EXAMPLE
Get-TKSPServiceAccount | Sort-Object ServiceName
Gets all of the service accounts and sorts them by service name.
.EXAMPLE
Get-TKSPServiceAccount | Export-Csv -Path .\output.csv -NoTypeInformation
Gets all of the service accounts in the farm. It outputs the results into a CSV file called output.csv in the current directory.
.EXAMPLE
Get-TKSPServiceAccount | ConvertTo-Csv -NoTypeInformation | Tee-Object -File .\output.csv | ConvertFrom-Csv
Gets all of the service accounts in the farm. It outputs the results into a CSV file called output.csv in the current directory and displays it on the screen at the same time.
#>
##Requires -RunAsAdministrator
[CmdletBinding()]
[OutputType('TKSPSeviceAccount')]
param (
)
begin {
}
process {
# Add the SharePoint snapin if it's not already here
try {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
catch {
Write-Error "Could not add Microsoft.SharePoint.PowerShell snapin"
return
}
#Get all accounts registered as managed accounts
Write-Verbose "Getting Managed Accounts"
$temp = Get-SPManagedAccount
foreach ($item in $temp) {
write-verbose $item.Username
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Managed Account'
UserName = $item.Username
}
$TempItem
}
#Get Application Pool Accounts
Write-Verbose "Getting SharePoint Application Pool Accounts"
# First get web application app pool accounts
$temp = Get-SPWebApplication -IncludeCentralAdministration | Select-Object -expand applicationpool | Select-Object name , username
# Then add the Service app app pool accounts
$temp += $(Get-SPServiceApplicationPool | Select-Object name, @{Name = "Username"; Expression = { $_.ProcessAccountName } })
foreach ($item in $temp) {
Write-Verbose "$($item.name) - $($item.Username)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.name
UserName = $item.Username
}
$TempItem
}
#Get all accounts running service applications
Write-Verbose "Getting SharePoint Service Application Accounts"
$temp = Get-SPServiceApplication | Select-Object DisplayName, applicationpool -expand applicationpool -EA 0 | Select-Object -Unique
foreach ($item in $temp) {
Write-Verbose "$($item.DisplayName) - $($item.ProcessAccountName)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.DisplayName
UserName = $item.ProcessAccountName
}
$TempItem
}
#Get User Profile sync account
Write-Verbose "Getting SharePoint User Profile Sync Account"
$caWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager( $(Get-SPServiceContext $caWebApp.Sites[0].Url))
$temp = $configManager | Select-Object -expand connectionmanager | Select-Object AccountUserName
foreach ($item in $temp) {
Write-Verbose $item.AccountUsername
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'User Profile Sync Account'
UserName = $item.AccountUsername
}
$TempItem
}
$temp = Get-SPServiceInstance | Select-Object -expand service | ForEach-Object { if ( $_.ProcessIdentity -and $_.ProcessIdentity.GetType() -eq "String") { Select-Object -InputObject $_ -Property TypeName, @{Name = "UserName"; Expression = { $_.ProcessIdentity } } } elseif ($_.TypeName -eq "SharePoint Server Search") { Select-Object -InputObject $_ -Property TypeName, @{Name = "UserName"; Expression = { $_.ProcessIdentity } } } elseif ( $_.ProcessIdentity ) { Select-Object -InputObject $_ -Property TypeName, @{Name = "UserName"; Expression = { $_.ProcessIdentity.UserName } } } }
foreach ($item in $temp) {
Write-Verbose "$($item.TypeName) - $($item.UserName)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.TypeName
UserName = $item.Username
}
$TempItem
}
#Get Services accounts
Write-Verbose "Getting Accounts Running SharePoint Services"
$temp = Get-WmiObject -Query "select * from win32_service where name LIKE 'SP%v4'" | Select-Object name, startname -Unique
foreach ($item in $temp) {
Write-Verbose "$($item.name) - $($item.Startname)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.name
UserName = $item.Startname
}
$TempItem
}
$temp = Get-WmiObject -Query "select * from win32_service where name LIKE '%15'" | Select-Object name, startname -Unique
foreach ($item in $temp) {
Write-Verbose "$($item.name) - $($item.Startname)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.name
UserName = $item.Startname
}
$TempItem
}
$temp = Get-WmiObject -Query "select * from win32_service where name LIKE 'FIM%'" | Select-Object name, startname
foreach ($item in $temp) {
Write-Verbose "$($item.name) - $($item.Startname)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = $item.name
UserName = $item.Startname
}
$TempItem
}
#Get Object Cache accounts
Write-Verbose "Getting SharePoint Object Cache Accounts"
$temp = Get-SPWebApplication | ForEach-Object { $_.Properties["portalsuperuseraccount"] }
if (-not [string]::IsNullOrWhiteSpace($temp)) {
foreach ($item in $temp) {
Write-Verbose "portalsuperuseraccount - $($item)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'portalsuperuseraccount'
UserName = $item
}
}
}
$temp = Get-SPWebApplication | ForEach-Object { $_.Properties["portalsuperreaderaccount"] }
if (-not [string]::IsNullOrWhiteSpace($temp)) {
foreach ($item in $temp) {
Write-Verbose "portalsuperreaderaccount - $($item)"
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'portalsuperreaderaccount'
UserName = $item
}
$TempItem
}
}
#Get default Search crawler account
Write-Verbose "Getting SharePoint Search Crawler Account(s)"
$temp = New-Object Microsoft.Office.Server.Search.Administration.content $(Get-SPEnterpriseSearchServiceApplication) | Select-Object DefaultGatheringAccount
foreach ($item in $temp) {
Write-Verbose $item.DefaultGatheringAccount
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Default SharePoint Search Crawler Account'
UserName = $item.DefaultGatheringAccount
}
$TempItem
}
#Get all search crawler accounts from crawl rules
$rules = Get-SPEnterpriseSearchCrawlRule -SearchApplication (Get-SPEnterpriseSearchServiceApplication)
foreach ($rule in $rules) {
Write-Verbose $item.AccountName
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'SharePoint Search Crawler Account'
UserName = $item.AccountName
}
$TempItem
}
#Get Unattended Accounts
Write-Verbose "Getting Unattended Service Application ID Account(s)"
$UnattendedAccounts = @()
try {
if (Get-SPVisioServiceApplication) {
$svcapp = Get-SPServiceApplication | Where-Object { $_.TypeName -like "*Visio*" }
$Visio = ($svcapp | Get-SPVisioExternalData).UnattendedServiceAccountApplicationID
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Viso Unattended ID Account'
UserName = $Visio
}
$TempItem
$UnattendedAccounts += $Visio
}
}
catch {
# no action needed
}
try {
if (Get-SPExcelServiceApplication) {
$Excel = (Get-SPExcelServiceApplication).UnattendedAccountApplicationID
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Excel Unattended ID Account'
UserName = $Excel
}
$TempItem
$UnattendedAccounts += $Excel
}
}
catch {
# no action needed
}
try {
[void](Get-Command -name Get-SPPerformancePointServiceApplication -ErrorAction Stop)
if (Get-SPPerformancePointServiceApplication) {
$PerformancePoint = (Get-SPPerformancePointSecureDataValues -ServiceApplication $svcApp.Id).DataSourceUnattendedServiceAccount
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Performance Point Unattended ID Account'
UserName = $PerformancePoint
}
$TempItem
$UnattendedAccounts += $PerformancePoint
}
}
catch {
# no action needed
}
try {
if (Get-PowerPivotServiceApplication) {
$PowerPivot = (Get-PowerPivotServiceApplication).UnattendedAccount
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Power Pivot Unattended ID Account'
UserName = $PowerPivot
}
$TempItem
$UnattendedAccounts += $PowerPivot
}
}
catch {
# no action needed
}
$serviceCntx = Get-SPServiceContext -Site (Get-SPWebApplication -includecentraladministration | Where-Object { $_.IsAdministrationWebApplication } | Select-Object -ExpandProperty Url)
$sssProvider = New-Object Microsoft.Office.SecureStoreService.Server.SecureStoreProvider
$sssProvider.Context = $serviceCntx
$marshal = [System.Runtime.InteropServices.Marshal]
try {
$applications = $sssProvider.GetTargetApplications()
foreach ($application in $applications | Where-Object { $UnattendedAccounts -contains $_.Name }) {
$sssCreds = $sssProvider.GetCredentials($application.Name)
foreach ($sssCred in $sssCreds | Where-Object { $_.CredentialType -eq "WindowsUserName" -or $_.CredentialType -eq "UserName" }) {
# Pretty sure this doesn't work. Need to create some Secure Store creds and test
$ptr = $marshal::SecureStringToBSTR($sssCred.Credential)
$str = $marshal::PtrToStringBSTR($ptr)
$str + " (" + $application.Name + ")"
if (-not [string]::IsNullOrWhiteSpace($str)) {
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Secure Store Account'
UserName = $str
}
$TempItem
}
}
}
}
catch {
# no action needed
}
#Get All Farm administrators
Write-Verbose "Getting Farm Administrators Group"
$FarmAdministrators = Get-SPWebApplication -IncludeCentralAdministration | Where-Object IsAdministrationWebApplication | Select-Object -Expand Sites | Where-Object ServerRelativeUrl -eq "/" | Get-SPWeb | Select-Object -Expand SiteGroups | Where-Object Name -eq "Farm Administrators" | Select-Object -expand Users -Unique
foreach ($FarmAdmin in $FarmAdministrators) {
$TempItem = [PSCustomObject]@{
PSTypeName = 'TKSPServiceAccount'
ServiceName = 'Farm Administrator'
UserName = $FarmAdmin.UserLogin
}
$TempItem
}
}
end {
}
}