Advertisement
Guest User

Untitled

a guest
May 18th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. <#
  2. Office 365 Functions v3.00
  3.  
  4. Change Log v3.00 Build 13022017
  5. Adding Set-BulkUserLicense function
  6.  
  7. Change Log v2.00 Build 25112016
  8. Added Create-Log function
  9. Removed CreateLog function
  10. Removed Set-ADUserAttributes
  11. Removed Update-MsolFederationMetadata
  12. Removed Set-O365UserLicensing
  13. Removed usage help text
  14.  
  15. Change Log v1.03b Build 06102015
  16. Updated some information for parameters
  17.  
  18. Change Log v1.03 Build 05102015
  19. Updated GetCredentialManagerCredentials to a more appropriate form
  20. Updated UpdateADAttributes to Set-ADUserAttributes
  21.  
  22. Change Log v1.02 Build 15052015
  23. Updated Set-O365UserLicensing function to v1.02
  24.  
  25. Change Log v1.01 Build 06052015
  26. Added Set-O365UserLicensing_v1.01.ps1 function
  27. Removed CreateLog function
  28. Removed WriteToHost function
  29. Removed ApplyLicensing function
  30. Removed ApplyProPlusLicensing function
  31. Removed UpdateExchangeRegionalSettings function
  32. #>
  33.  
  34. # Import Required Modules
  35. Import-Module ActiveDirectory
  36. Import-Module MSOnline
  37.  
  38.  
  39. # Functions Start
  40.  
  41. # Get-CredManCreds function
  42. function Get-CredManCreds ($Target){
  43. $ErrorActionPreference = "silentlycontinue"
  44. $sig = @"
  45. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  46. public struct NativeCredential{
  47. public UInt32 Flags;
  48. public CRED_TYPE Type;
  49. public IntPtr TargetName;
  50. public IntPtr Comment;
  51. public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
  52. public UInt32 CredentialBlobSize;
  53. public IntPtr CredentialBlob;
  54. public UInt32 Persist;
  55. public UInt32 AttributeCount;
  56. public IntPtr Attributes;
  57. public IntPtr TargetAlias;
  58. public IntPtr UserName;
  59. internal static NativeCredential GetNativeCredential(Credential cred){
  60. NativeCredential ncred = new NativeCredential();
  61. ncred.AttributeCount = 0;
  62. ncred.Attributes = IntPtr.Zero;
  63. ncred.Comment = IntPtr.Zero;
  64. ncred.TargetAlias = IntPtr.Zero;
  65. ncred.Type = CRED_TYPE.GENERIC;
  66. ncred.Persist = (UInt32)1;
  67. ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
  68. ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName);
  69. ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
  70. ncred.UserName = Marshal.StringToCoTaskMemUni(System.Environment.UserName);
  71. return ncred;
  72. }
  73. }
  74. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  75. public struct Credential{
  76. public UInt32 Flags;
  77. public CRED_TYPE Type;
  78. public string TargetName;
  79. public string Comment;
  80. public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
  81. public UInt32 CredentialBlobSize;
  82. public string CredentialBlob;
  83. public UInt32 Persist;
  84. public UInt32 AttributeCount;
  85. public IntPtr Attributes;
  86. public string TargetAlias;
  87. public string UserName;
  88. }
  89. public enum CRED_TYPE : uint {
  90. GENERIC = 1,
  91. DOMAIN_PASSWORD = 2,
  92. DOMAIN_CERTIFICATE = 3,
  93. DOMAIN_VISIBLE_PASSWORD = 4,
  94. GENERIC_CERTIFICATE = 5,
  95. DOMAIN_EXTENDED = 6,
  96. MAXIMUM = 7, // Maximum supported cred type
  97. MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes
  98. }
  99. public class CriticalCredentialHandle : Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid{
  100. public CriticalCredentialHandle(IntPtr preexistingHandle){
  101. SetHandle(preexistingHandle);
  102. }
  103. public Credential GetCredential(){
  104. if (!IsInvalid){
  105. NativeCredential ncred = (NativeCredential)Marshal.PtrToStructure(handle, typeof(NativeCredential));
  106. Credential cred = new Credential();
  107. cred.CredentialBlobSize = ncred.CredentialBlobSize;
  108. cred.CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob, (int)ncred.CredentialBlobSize / 2);
  109. cred.UserName = Marshal.PtrToStringUni(ncred.UserName);
  110. cred.TargetName = Marshal.PtrToStringUni(ncred.TargetName`);
  111. cred.TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
  112. cred.Type = ncred.Type;
  113. cred.Flags = ncred.Flags;
  114. cred.Persist = ncred.Persist;
  115. return cred;
  116. } else {
  117. throw new InvalidOperationException("Invalid CriticalHandle!");
  118. }
  119. }
  120. override protected bool ReleaseHandle(){
  121. if (!IsInvalid){
  122. CredFree(handle);
  123. SetHandleAsInvalid();
  124. return true;
  125. }
  126. return false;
  127. }
  128. }
  129. [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
  130. public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
  131. [DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
  132. public static extern bool CredFree([In] IntPtr cred);
  133. "@
  134. Add-Type -MemberDefinition $sig -Namespace "ADVAPI32" -Name 'Util'
  135. $targetName = $Target
  136. $nCredPtr= New-Object IntPtr
  137. $success = [ADVAPI32.Util]::CredRead($targetName,1,0,[ref] $nCredPtr)
  138. if($success){
  139. $critCred = New-Object ADVAPI32.Util+CriticalCredentialHandle $nCredPtr
  140. $cred = $critCred.GetCredential()
  141. $UserName = $cred.UserName;
  142. $Password = $cred.CredentialBlob;
  143. $Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
  144. $objCreds = New-Object Management.Automation.PSCredential $UserName, $Password
  145. return $objCreds
  146. }
  147. }
  148.  
  149. # Create-Log function
  150. Function Create-Log ($FilePath, $Message){
  151. (Get-Date -Format u) + " $Message" | Out-File -FilePath $FilePath -Append
  152. }
  153.  
  154. # Set-BulkUserLicense function
  155. Function Set-BulkUserLicense ($Department, $Title, $LicensePlan, $LicensePlanOptions, $UsageLocation){
  156.  
  157. # Check mandatory parameters
  158. if (!$LicensePlan){
  159. "Mandatory parameter -LicensePlan not detected. Please re-run the cmdlet."
  160. break
  161. }
  162. if (!$UsageLocation){
  163. "Mandatory parameter -UsageLocation not detected. Please re-run the cmdlet."
  164. break
  165. }
  166.  
  167. # Check Department/Title mandatory parameters
  168. if ($Department -or $Title){
  169. if ($Department){
  170. "-Department parameter detected."
  171. }
  172. if ($Title){
  173. "-Title parameter detected."
  174. }
  175. } else {
  176. "Mandatory parameter -Department or -Title not detected. Please re-run the cmdlet."
  177. break
  178. }
  179.  
  180. # Get user list for license check
  181. if ($Department){
  182. "Building user list from -Department = $Department"
  183. $Users = Get-MsolUser -All -Department $Department -Synchronized
  184. }
  185. if ($Title){
  186. "Building user list from -Title = $Title"
  187. $Users = Get-MsolUser -All -Title $Title -Synchronized
  188. }
  189.  
  190. # Check for disabled plans
  191. if ($LicensePlanOptions){
  192. "Creating License Plan Options for $LicensePlan"
  193. $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $LicensePlan -DisabledPlans $LicensePlanOptions
  194. }
  195.  
  196. # Build list of users without the license
  197. "Processing user set for unlicensed users"
  198. $NoLicensePlanUsers = $Users | ? {$_.Licenses.AccountSkuId -notcontains $LicensePlan}
  199.  
  200. # Start licensing unlicensed users
  201. if (!$NoLicensePlanUsers){
  202. "Found no users without $LicensePlan"
  203. } else {
  204. foreach ($NoLicensePlanUser in $NoLicensePlanUsers){
  205. if (($NoLicensePlanUser).UsageLocation -ne 'NZ'){
  206. # Set-MsolUser -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -UsageLocation 'NZ'
  207. "Updating UsageLocation for " + ($NoLicensePlanUser).UserPrincipalName
  208. }
  209. if ($LicenseOptions){
  210. # Set-MsolUserLicense -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -AddLicenses $LicensePlan -LicenseOptions $LicenseOptions
  211. "Licensing w/ License Plan Options for " + ($NoLicensePlanUser).UserPrincipalName
  212. } else {
  213. # Set-MsolUserLicense -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -AddLicenses $LicensePlan
  214. "Licensing for " + ($NoLicensePlanUser).UserPrincipalName
  215. }
  216. }
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement