Guest User

Untitled

a guest
Oct 2nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #CredMan stuff
  2. $sig = @"
  3. [DllImport("Advapi32.dll", SetLastError=true, EntryPoint="CredWriteW", CharSet=CharSet.Unicode)]
  4. public static extern bool CredWrite([In] ref Credential userCredential, [In] UInt32 flags);
  5. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  6. public struct Credential
  7. {
  8. public UInt32 flags;
  9. public UInt32 type;
  10. public IntPtr targetName;
  11. public IntPtr comment;
  12. public System.Runtime.InteropServices.ComTypes.FILETIME lastWritten;
  13. public UInt32 credentialBlobSize;
  14. public IntPtr credentialBlob;
  15. public UInt32 persist;
  16. public UInt32 attributeCount;
  17. public IntPtr Attributes;
  18. public IntPtr targetAlias;
  19. public IntPtr userName;
  20. }
  21. "@
  22. Add-Type -MemberDefinition $sig -Namespace "ADVAPI32" -Name 'Util'
  23. $cred = New-Object ADVAPI32.Util+Credential
  24. $cred.flags = 0
  25. $cred.type = 1
  26.  
  27. #Get MSOL creds
  28. While (!$UserName) {$UserName = (Read-Host "`n MSOL username (user`@domain)").ToUpper()}
  29.  
  30. #Set the name of the CredMan credentials
  31. $TargetName = "LicenceManagment"
  32. $cred.targetName = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($TargetName)
  33. $cred.userName = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($UserName)
  34. $cred.attributeCount = 0
  35. $cred.persist = 2
  36. While (!$Password) {$Password = Read-Host -assecurestring "`n MSOL password"}
  37. $objCreds = New-Object Management.Automation.PSCredential $UserName, $Password
  38. $Password = $objCreds.GetNetworkCredential().Password
  39.  
  40. #Validating MSOL creds
  41. Write-Host "`n Validating MSOL credentials"
  42. Import-Module MSOnline
  43. Connect-MsolService -Credential $objCreds
  44. If ($?)
  45. {
  46. Write-Host "`n`tSuccess" -ForegroundColor Green
  47. }
  48. Else
  49. {
  50. Write-Host "`n`tFailed MSOL credential validation. Exiting...`n" -ForegroundColor Red
  51. Exit
  52. }
  53.  
  54. $cred.credentialBlobSize = [System.Text.Encoding]::Unicode.GetBytes($Password).length
  55. $cred.credentialBlob = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($Password)
  56.  
  57. #Store the MSOL creds in CredMan
  58. $CredWrite = [ADVAPI32.Util]::CredWrite([ref]$cred,0)
  59. If ($CredWrite)
  60. {
  61. Write-Host "`n`tAdded MSOL credentials to the local Credential Manager" -ForegroundColor Green
  62. }
  63. Else
  64. {
  65. Write-Host "`n`tFailed adding MSOL credentials to the local Credential Manager. Exiting...`n" -ForegroundColor Red
  66. Exit
  67. }
Add Comment
Please, Sign In to add comment