Get Cached Credentials in PowerShell from Windows 7 Credential Manager
我可以使用一些我忽略的 API 来执行此操作,还是我必须弄清楚将此处提到的 C# .NET API 调用移植到 PowerShell 中以在脚本中实现这一点?
我相信您可以使用 Add-Type cmdlet 轻松移植它:
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 |
$sig = @“
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal static NativeCredential GetNativeCredential(Credential cred) [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public enum CRED_TYPE : uint public class CriticalCredentialHandle : Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid public Credential GetCredential() } } override protected bool ReleaseHandle() [DllImport(“ Advapi32.dll“, EntryPoint =”CredReadW“, CharSet = CharSet.Unicode, SetLastError = true)]public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr); [DllImport(“ Advapi32.dll“, EntryPoint =”CredFree“, SetLastError = true)]public static extern bool CredFree([In] IntPtr cred); “ @Add–Type –MemberDefinition $sig -Namespace“ADVAPI32” -Name ‘Util’ $targetName =“computer” $success = [ADVAPI32.Util]::CredRead($targetName,1,0,[ref] $nCredPtr) if($success){ |
改编自此处:http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/e91769eb-dbce-4e77-8b61-d3e55690b511/
基于:http://blogs.msdn.com/b/peerchan/archive/2005/11/01/487834.aspx
- 我想我实际上已经很尴尬地通过了,但我认为一定有更简单的方法。我有点侮辱没有更简单的方法来为 Windows 领域的脚本提取安全凭据,但我想我应该停止感到惊讶。当我有更多时间测试它时,我会更新这篇文章。谢谢。
这已经完成了:
http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde/view/Discussions
它提供了一些与 cred-man 相关的方法,并且已经记录在案。
- 该文件的使用许可是什么?
来源:https://www.codenong.com/7162604/