-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDump-SPManagedAccounts.ps1
More file actions
84 lines (66 loc) · 3.51 KB
/
Copy pathDump-SPManagedAccounts.ps1
File metadata and controls
84 lines (66 loc) · 3.51 KB
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
<#
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
We grant you a nonexclusive, royalty-free right to use and modify the sample code and to reproduce and distribute the object
code form of the Sample Code, provided that you agree:
(i) to not use our name, logo, or trademarks to market your software product in which the sample code is embedded;
(ii) to include a valid copyright notice on your software product in which the sample code is embedded; and
(iii) to indemnify, hold harmless, and defend us and our suppliers from and against any claims or lawsuits, including
attorneys' fees, that arise or result from the use or distribution of the sample code.
Please note: None of the conditions outlined in the disclaimer above will supercede the terms and conditions contained within
the Premier Customer Services Description.
SUMMARY:
This script dumps all username and passwords for SPManagedAccounts configured on the current SharePoint farm
Reference: tbd
Version History:
1.0 - initial version
#>
# SharePoint PSSnapin laden (SP2016/2019)
#Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
# --------------------------------------------------
# Helper functions
# --------------------------------------------------
# BindingFlags for accessing private/internal instance fields of an object
function Get-BindingFlags {
return [System.Reflection.BindingFlags]::CreateInstance `
-bor [System.Reflection.BindingFlags]::GetField `
-bor [System.Reflection.BindingFlags]::Instance `
-bor [System.Reflection.BindingFlags]::NonPublic
}
# Read a private/internal field of an object via Reflection
function Get-NonPublicFieldValue {
param(
[object] $Object,
[string] $FieldName
)
$flags = Get-BindingFlags
return $Object.GetType().GetField($FieldName, $flags).GetValue($Object)
}
# SecureString to plain text (via marshalling and, zero out immediately after)
function ConvertTo-PlainText {
param([System.Security.SecureString] $SecureString)
$ptr = [System.IntPtr]::Zero
try {
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecureString)
return [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
}
finally {
# Securely zero and release unmanaged memory
[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($ptr)
}
}
# ------------------------------------------------------
# List all Managed Accounts and extract their passwords
# ------------------------------------------------------
Get-SPManagedAccount | Select-Object UserName, @{
Name = "Password"
Expression = {
# 1) Retrieve private field m_Password (SPEncryptedString)
$spEncryptedString = Get-NonPublicFieldValue -Object $_ -FieldName "m_Password"
# 2) Access public property SecureStringValue (System.Security.SecureString)
$secureString = $spEncryptedString.SecureStringValue
# 3) Convert SecureString to plain text
ConvertTo-PlainText -SecureString $secureString
}
}