Advertisement
Guest User

Untitled

a guest
Sep 7th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. <#
  2. Set-VpnConnectionUsernamePassword - by Paul Stancer.
  3. Huge thanks to Jeff Winn for the DotRas project (https://dotras.codeplex.com/) which showed me the way,
  4. and did all the really hard work.
  5. #>
  6. $code=@'
  7. using System;
  8. using System.Runtime.InteropServices;
  9.  
  10. public class VPNCredentialsHelper
  11. {
  12. private const int SUCCESS = 0;
  13. private const int ERROR_ACCESS_DENIED = 5;
  14.  
  15. private const int UNLEN = 256;// Defines the maximum length of a username.
  16. private const int PWLEN = 256;// Defines the maximum length of a password.
  17. private const int DNLEN = 15;// Defines the maximum length of a domain name.
  18.  
  19. [Flags]
  20. private enum RASCM
  21. {
  22. None = 0x0,
  23. UserName = 0x1,
  24. Password = 0x2,
  25. Domain = 0x4
  26. }
  27.  
  28. [DllImport("rasapi32.dll", CharSet = CharSet.Unicode)]
  29. private static extern int RasGetErrorString(
  30. int uErrorValue,
  31. [In, Out] string lpszErrorString,
  32. int cBufSize);
  33.  
  34. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 4)]
  35. private struct RASCREDENTIALS
  36. {
  37. public int size;
  38. public RASCM options;
  39. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UNLEN + 1)]
  40. public string userName;
  41. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PWLEN + 1)]
  42. public string password;
  43. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DNLEN + 1)]
  44. public string domain;
  45. }
  46.  
  47. [DllImport("rasapi32.dll", CharSet = CharSet.Unicode)]
  48. private static extern int RasSetCredentials(
  49. string lpszPhonebook,
  50. string lpszEntryName,
  51. IntPtr lpCredentials,
  52. [MarshalAs(UnmanagedType.Bool)] bool fClearCredentials);
  53.  
  54. public static bool SetCredentials(string entryName, string domain, string username, string password)
  55. {
  56. var credentials = new RASCREDENTIALS() { userName = username, password = password, domain = domain ?? string.Empty, options = RASCM.Domain | RASCM.UserName | RASCM.Password };
  57.  
  58. int size = Marshal.SizeOf(typeof(RASCREDENTIALS));
  59.  
  60. IntPtr pCredentials = IntPtr.Zero;
  61. try
  62. {
  63. credentials.size = size;
  64.  
  65. pCredentials = Marshal.AllocHGlobal(size);
  66. Marshal.StructureToPtr(credentials, pCredentials, true);
  67.  
  68. int ret = RasSetCredentials(null, entryName, pCredentials, false);
  69.  
  70. switch (ret)
  71. {
  72. case SUCCESS:
  73. return true;
  74. case ERROR_ACCESS_DENIED:
  75. throw new UnauthorizedAccessException();
  76. default:
  77. throw ProcessRASException(ret);
  78. }
  79. }
  80. finally
  81. {
  82. if (pCredentials != IntPtr.Zero)
  83. {
  84. Marshal.FreeHGlobal(pCredentials);
  85. }
  86. }
  87. }
  88.  
  89. private static Exception ProcessRASException(int errorCode)
  90. {
  91. try
  92. {
  93. string buffer = new string('\x00', 512);
  94.  
  95. int ret = RasGetErrorString(errorCode, buffer, buffer.Length);
  96. if (ret == SUCCESS)
  97. return new RASException(errorCode, buffer.Substring(0, buffer.IndexOf('\x00')));
  98. }
  99. catch (EntryPointNotFoundException)
  100. {
  101. }
  102.  
  103. return new RASException(errorCode, "RAS Error code: " + errorCode.ToString());
  104. }
  105.  
  106. public class RASException: Exception
  107. {
  108. public RASException(int errCode, string message):base(message)
  109. {
  110. RASErrorCode = errCode;
  111. }
  112.  
  113. public int RASErrorCode { get; private set; }
  114. }
  115. }
  116. '@
  117.  
  118. function Set-VpnConnectionUsernamePassword {
  119.  
  120. param
  121. (
  122. ##[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,HelpMessage='What connection name would you set the credentials?')]
  123. [ValidateLength(3,255)]
  124. [string]$connectionname,
  125. ##[Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  126. [ValidateLength(0,15)]
  127. [string]$domain,
  128. ##[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  129. [ValidateLength(0,255)]
  130. [string]$username,
  131. ##[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  132. [ValidateLength(0,255)]
  133. [string]$password
  134. )
  135.  
  136. Add-Type -TypeDefinition $code -IgnoreWarnings
  137.  
  138. Try
  139. {
  140. [VPNCredentialsHelper]::SetCredentials($connectionname, $domain, $username, $password)
  141. }
  142. Catch [System.UnauthorizedAccessException]
  143. {
  144. write-host "You do not have permissions to change the credentials" -ForegroundColor Red
  145. }
  146. Catch
  147. {
  148. write-host $_.Exception.Message -ForegroundColor Red
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement