Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get-licensed-users-license-details.ps1
$ErrorActionPreference = "Continue"
$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 = "LicenceManagment"
$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
}
####
If(@(Get-Module | ? { $_.Name -eq "MSOnline"}).Count -eq 0)
{
Import-Module MSOnline;
}
If(@(Get-Module | ? { $_.Name -eq "ActiveDirectory"}).Count -eq 0)
{
Import-Module ActiveDirectory;
}
$LoggingFile="C:\Office365-Scripts\LicenceLog.txt"
$POWER_BI_STANDARD="C:\Office365-Scripts\POWER_BI_STANDARD.txt"
$STANDARDWOFFPACK_FACULTY="C:\Office365-Scripts\STANDARDWOFFPACK_FACULTY.txt"
$OFFICESUBSCRIPTION_FACULTY="C:\Office365-Scripts\OFFICESUBSCRIPTION_FACULTY.txt"
$OFFICESUBSCRIPTION_STUDENT="C:\Office365-Scripts\OFFICESUBSCRIPTION_STUDENT.txt"
$STANDARDWOFFPACK_IW_STUDENT="C:\Office365-Scripts\STANDARDWOFFPACK_IW_STUDENT.txt"
$CRMSTANDARD="C:\Office365-Scripts\CRMSTANDARD.txt"
$INTUNE_A="C:\Office365-Scripts\INTUNE_A.txt"
$STANDARDWOFFPACK_IW_FACULTY="C:\Office365-Scripts\STANDARDWOFFPACK_IW_FACULTY.txt"
$PROJECTONLINE_PLAN_1_FACULTY="C:\Office365-Scripts\PROJECTONLINE_PLAN_1_FACULTY.txt"
$STANDARDWOFFPACK_STUDENT="C:\Office365-Scripts\STANDARDWOFFPACK_STUDENT.txt"
Connect-MsolService -Credential $objCreds
$Users=Get-MsolUser -all | where {$_.isLicensed -eq "True"}
ForEach ($User in $Users)
{
If($User.UserPrincipalName -notlike "*<YourTenancyName>.onmicrosoft.com" -and $User.UserPrincipalName -ne "DirSync@blah.com")
{
$AdUser=Get-ADUser -Filter {UserPrincipalName -eq $User.UserPrincipalName} -Properties extensionAttribute5, sAMAccountName
$List=$NULL
$Licences=$User.Licenses
$SKUIDs=$Licences.AccountSkuId
ForEach ($SKU in $SKUIDs)
{
If ($SKU -eq "<YourTenancyName>:POWER_BI_STANDARD")
{
Add-ADGroupMember -Identity POWER_BI_STANDARD_Users -Members $AdUser.sAMAccountName
#Add-Content $POWER_BI_STANDARD $User.UserPrincipalName
}
If ($SKU -eq "<YourTenancyName>:STANDARDWOFFPACK_FACULTY")
{
Add-ADGroupMember -Identity STANDARDWOFFPACK_FACULTY_Users -Members $AdUser.sAMAccountName
#Add-Content $STANDARDWOFFPACK_FACULTY $User.UserPrincipalName
}
If ($SKU -eq "<YourTenancyName>:OFFICESUBSCRIPTION_FACULTY")
{
Add-ADGroupMember -Identity OFFICESUBSCRIPTION_FACULTY_Users -Members $AdUser.sAMAccountName
#Add-Content $OFFICESUBSCRIPTION_FACULTY $User.UserPrincipalName
}
If ($SKU -eq "<YourTenancyName>:OFFICESUBSCRIPTION_STUDENT")
{
Add-ADGroupMember -Identity OFFICESUBSCRIPTION_STUDENT_Users -Members $AdUser.sAMAccountName
#Add-Content $OFFICESUBSCRIPTION_STUDENT $User.UserPrincipalName
}
#If ($SKU -eq "<YourTenancyName>:STANDARDWOFFPACK_IW_STUDENT")
#{
##Add-Content $STANDARDWOFFPACK_IW_STUDENT $User.UserPrincipalName
#}
If ($SKU -eq "<YourTenancyName>:CRMSTANDARD")
{
Add-ADGroupMember -Identity CRMSTANDARD_Users -Members $AdUser.sAMAccountName
#Add-Content $CRMSTANDARD $User.UserPrincipalName
}
If ($SKU -eq "<YourTenancyName>:INTUNE_A")
{
Add-ADGroupMember -Identity INTUNE_A_Users -Members $AdUser.sAMAccountName
#Add-Content $INTUNE_A $User.UserPrincipalName
}
#If ($SKU -eq "<YourTenancyName>:STANDARDWOFFPACK_IW_FACULTY")
#{
##Add-Content $STANDARDWOFFPACK_IW_FACULTY $User.UserPrincipalName
#}
If ($SKU -eq "<YourTenancyName>:PROJECTONLINE_PLAN_1_FACULTY")
{
Add-ADGroupMember -Identity PROJECTONLINE_PLAN_1_FACULTY_Users -Members $AdUser.sAMAccountName
#Add-Content $PROJECTONLINE_PLAN_1_FACULTY $User.UserPrincipalName
}
If ($SKU -eq "<YourTenancyName>:STANDARDWOFFPACK_STUDENT")
{
Add-ADGroupMember -Identity STANDARDWOFFPACK_STUDENT_Users -Members $AdUser.sAMAccountName
#Add-Content $STANDARDWOFFPACK_STUDENT $User.UserPrincipalName
}
$List = $List + $SKU + "|"
}
Sort-object $List
Write-Host $List
$Content=$User.UserPrincipalName + "|" + $List
#Add-Content $LoggingFile $Content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.