Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Office 365 Functions v3.00
- Change Log v3.00 Build 13022017
- Adding Set-BulkUserLicense function
- Change Log v2.00 Build 25112016
- Added Create-Log function
- Removed CreateLog function
- Removed Set-ADUserAttributes
- Removed Update-MsolFederationMetadata
- Removed Set-O365UserLicensing
- Removed usage help text
- Change Log v1.03b Build 06102015
- Updated some information for parameters
- Change Log v1.03 Build 05102015
- Updated GetCredentialManagerCredentials to a more appropriate form
- Updated UpdateADAttributes to Set-ADUserAttributes
- Change Log v1.02 Build 15052015
- Updated Set-O365UserLicensing function to v1.02
- Change Log v1.01 Build 06052015
- Added Set-O365UserLicensing_v1.01.ps1 function
- Removed CreateLog function
- Removed WriteToHost function
- Removed ApplyLicensing function
- Removed ApplyProPlusLicensing function
- Removed UpdateExchangeRegionalSettings function
- #>
- # Import Required Modules
- Import-Module ActiveDirectory
- Import-Module MSOnline
- # Functions Start
- # Get-CredManCreds function
- function Get-CredManCreds ($Target){
- $ErrorActionPreference = "silentlycontinue"
- $sig = @"
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct NativeCredential{
- public UInt32 Flags;
- public CRED_TYPE Type;
- public IntPtr TargetName;
- public IntPtr Comment;
- public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
- public UInt32 CredentialBlobSize;
- public IntPtr CredentialBlob;
- public UInt32 Persist;
- public UInt32 AttributeCount;
- public IntPtr Attributes;
- public IntPtr TargetAlias;
- public IntPtr UserName;
- internal static NativeCredential GetNativeCredential(Credential cred){
- NativeCredential ncred = new NativeCredential();
- ncred.AttributeCount = 0;
- ncred.Attributes = IntPtr.Zero;
- ncred.Comment = IntPtr.Zero;
- ncred.TargetAlias = IntPtr.Zero;
- ncred.Type = CRED_TYPE.GENERIC;
- ncred.Persist = (UInt32)1;
- ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
- ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName);
- ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
- ncred.UserName = Marshal.StringToCoTaskMemUni(System.Environment.UserName);
- return ncred;
- }
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct Credential{
- public UInt32 Flags;
- public CRED_TYPE Type;
- public string TargetName;
- public string Comment;
- public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
- public UInt32 CredentialBlobSize;
- public string CredentialBlob;
- public UInt32 Persist;
- public UInt32 AttributeCount;
- public IntPtr Attributes;
- public string TargetAlias;
- public string UserName;
- }
- public enum CRED_TYPE : uint {
- GENERIC = 1,
- DOMAIN_PASSWORD = 2,
- DOMAIN_CERTIFICATE = 3,
- DOMAIN_VISIBLE_PASSWORD = 4,
- GENERIC_CERTIFICATE = 5,
- DOMAIN_EXTENDED = 6,
- MAXIMUM = 7, // Maximum supported cred type
- MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes
- }
- public class CriticalCredentialHandle : Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid{
- public CriticalCredentialHandle(IntPtr preexistingHandle){
- SetHandle(preexistingHandle);
- }
- public Credential GetCredential(){
- if (!IsInvalid){
- NativeCredential ncred = (NativeCredential)Marshal.PtrToStructure(handle, typeof(NativeCredential));
- Credential cred = new Credential();
- cred.CredentialBlobSize = ncred.CredentialBlobSize;
- cred.CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob, (int)ncred.CredentialBlobSize / 2);
- cred.UserName = Marshal.PtrToStringUni(ncred.UserName);
- cred.TargetName = Marshal.PtrToStringUni(ncred.TargetName`);
- cred.TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
- cred.Type = ncred.Type;
- cred.Flags = ncred.Flags;
- cred.Persist = ncred.Persist;
- return cred;
- } else {
- throw new InvalidOperationException("Invalid CriticalHandle!");
- }
- }
- override protected bool ReleaseHandle(){
- if (!IsInvalid){
- CredFree(handle);
- SetHandleAsInvalid();
- return true;
- }
- return false;
- }
- }
- [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 = $Target
- $nCredPtr= New-Object IntPtr
- $success = [ADVAPI32.Util]::CredRead($targetName,1,0,[ref] $nCredPtr)
- if($success){
- $critCred = New-Object ADVAPI32.Util+CriticalCredentialHandle $nCredPtr
- $cred = $critCred.GetCredential()
- $UserName = $cred.UserName;
- $Password = $cred.CredentialBlob;
- $Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
- $objCreds = New-Object Management.Automation.PSCredential $UserName, $Password
- return $objCreds
- }
- }
- # Create-Log function
- Function Create-Log ($FilePath, $Message){
- (Get-Date -Format u) + " $Message" | Out-File -FilePath $FilePath -Append
- }
- # Set-BulkUserLicense function
- Function Set-BulkUserLicense ($Department, $Title, $LicensePlan, $LicensePlanOptions, $UsageLocation){
- # Check mandatory parameters
- if (!$LicensePlan){
- "Mandatory parameter -LicensePlan not detected. Please re-run the cmdlet."
- break
- }
- if (!$UsageLocation){
- "Mandatory parameter -UsageLocation not detected. Please re-run the cmdlet."
- break
- }
- # Check Department/Title mandatory parameters
- if ($Department -or $Title){
- if ($Department){
- "-Department parameter detected."
- }
- if ($Title){
- "-Title parameter detected."
- }
- } else {
- "Mandatory parameter -Department or -Title not detected. Please re-run the cmdlet."
- break
- }
- # Get user list for license check
- if ($Department){
- "Building user list from -Department = $Department"
- $Users = Get-MsolUser -All -Department $Department -Synchronized
- }
- if ($Title){
- "Building user list from -Title = $Title"
- $Users = Get-MsolUser -All -Title $Title -Synchronized
- }
- # Check for disabled plans
- if ($LicensePlanOptions){
- "Creating License Plan Options for $LicensePlan"
- $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $LicensePlan -DisabledPlans $LicensePlanOptions
- }
- # Build list of users without the license
- "Processing user set for unlicensed users"
- $NoLicensePlanUsers = $Users | ? {$_.Licenses.AccountSkuId -notcontains $LicensePlan}
- # Start licensing unlicensed users
- if (!$NoLicensePlanUsers){
- "Found no users without $LicensePlan"
- } else {
- foreach ($NoLicensePlanUser in $NoLicensePlanUsers){
- if (($NoLicensePlanUser).UsageLocation -ne 'NZ'){
- # Set-MsolUser -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -UsageLocation 'NZ'
- "Updating UsageLocation for " + ($NoLicensePlanUser).UserPrincipalName
- }
- if ($LicenseOptions){
- # Set-MsolUserLicense -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -AddLicenses $LicensePlan -LicenseOptions $LicenseOptions
- "Licensing w/ License Plan Options for " + ($NoLicensePlanUser).UserPrincipalName
- } else {
- # Set-MsolUserLicense -UserPrincipalName ($NoLicensePlanUser).UserPrincipalName -AddLicenses $LicensePlan
- "Licensing for " + ($NoLicensePlanUser).UserPrincipalName
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement