LicenseAssignmentDates.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

<#PSScriptInfo
 
    .VERSION 1.4
 
    .GUID ce2341dd-19e1-4f4a-9af5-d01e16f0dec7
 
    .AUTHOR Vikas Sukhija
 
    .COMPANYNAME techwizard.cloud
 
    .COPYRIGHT techwizard.cloud
 
    .TAGS
 
    .LICENSEURI https://techwizard.cloud/2021/05/23/office-365-license-assignment-dates/
 
    .PROJECTURI https://techwizard.cloud/2021/05/23/office-365-license-assignment-dates/
 
    .ICONURI
 
    .EXTERNALMODULEDEPENDENCIES AzureAD,ImportExcel
 
    .REQUIREDSCRIPTS
 
    .EXTERNALSCRIPTDEPENDENCIES
 
    .RELEASENOTES
    https://techwizard.cloud/2021/05/23/office-365-license-assignment-dates/
 
    .PRIVATEDATA
 
    Created with: ISE
    Created on: 5/11/2021
    Filename: LicenseAssignmentDates.ps1
    updated on: 6/14/2021 (fixed bug with export excel showing incorrect dates)
    updated on: 1/17/2022 (fixed bug as it was not creating report folder)
#>


#Requires -Module AzureAD,ImportExcel

<#
 
    .DESCRIPTION
    License Assignment Dates
 
#>
 
Param(
$run="run")
function New-FolderCreation
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true)]
    [string]$foldername
  )
    

  $logpath  = (Get-Location).path + "\" + "$foldername" 
  $testlogpath = Test-Path -Path $logpath
  if($testlogpath -eq $false)
  {
    #Start-ProgressBar -Title "Creating $foldername folder" -Timer 10
    $null = New-Item -Path (Get-Location).path -Name $foldername -Type directory
  }
}
function Write-Log
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [array]$Name,
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [string]$Ext,
    [Parameter(Mandatory = $true,ParameterSetName = 'Create')]
    [string]$folder,
    
    [Parameter(ParameterSetName = 'Create',Position = 0)][switch]$Create,
    
    [Parameter(Mandatory = $true,ParameterSetName = 'Message')]
    [String]$message,
    [Parameter(Mandatory = $true,ParameterSetName = 'Message')]
    [String]$path,
    [Parameter(Mandatory = $false,ParameterSetName = 'Message')]
    [ValidateSet('Information','Warning','Error')]
    [string]$Severity = 'Information',
    
    [Parameter(ParameterSetName = 'Message',Position = 0)][Switch]$MSG
  )
  switch ($PsCmdlet.ParameterSetName) {
    "Create"
    {
      $log = @()
      $date1 = Get-Date -Format d
      $date1 = $date1.ToString().Replace("/", "-")
      $time = Get-Date -Format t
    
      $time = $time.ToString().Replace(":", "-")
      $time = $time.ToString().Replace(" ", "")
      New-FolderCreation -foldername $folder
      foreach ($n in $Name)
      {$log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext"}
      return $log
    }
    "Message"
    {
      $date = Get-Date
      $concatmessage = "|$date" + "| |" + $message +"| |" + "$Severity|"
      switch($Severity){
        "Information"{Write-Host -Object $concatmessage -ForegroundColor Green}
        "Warning"{Write-Host -Object $concatmessage -ForegroundColor Yellow}
        "Error"{Write-Host -Object $concatmessage -ForegroundColor Red}
      }
      
      Add-Content -Path $path -Value $concatmessage
    }
  }
} #Function Write-Log
####################Load variables and log#######################################
$log = Write-Log -Name "LicenseAssignmentDates-Log" -folder "logs" -Ext "log"
New-FolderCreation -foldername report
$report1 = (Get-Location).path + "\report\LicenseAssignmentDates-Report.xlsx"
$csv = (Get-Location).path + "\report\tempcsv3.csv"
##################################################################################
try{
  Write-Log -Message "Start ................Script" -path $log
  Write-Log -Message "Connect to AZUREAD" -path $log
  Connect-AzureAD
  import-module ImportExcel
}
catch{
  $exception = $_.Exception.Message
  Write-Log -Message "exception $exception has occured Connecting to AzureAD"  -path $log -Severity Error
  Exit
}
#######################Get all Azure AD Users###################################################
Write-Log -Message "Get all AzureAD Users" -path $log
try{
  [System.Collections.ArrayList]$collection = @()
  $getallazureadusers = Get-AzureADUser -Filter "UserType eq 'Member'" -All $true
  Write-Log -Message "Fetched all AzureAD Users" -path $log
  $getallazureadusers | ForEach-Object{
    $upn = $_.UserPrincipalName
    $coll = "" | Select UserPrincipalName,DomesticCalling, DomesticCallingPrepaid, CommunicationCredits # for reporting add the names
    $coll.UserPrincipalName =  $upn
    $getazureaduserServiceid = $_.assignedplans
    $getazureaduserServiceid | where{$_.CapabilityStatus -eq "Enabled"}| foreach-object{
      $serviceplanid = $_.ServicePlanId
      $AssignedTimestamp = get-date $($_.AssignedTimestamp) -Format MM/dd/yy
      ############updated serviceplanid here#######################
      switch($serviceplanid){
        "5hhh152dc-90de-4776-93d2-bc476677fc06" {$coll.DomesticCalling = $AssignedTimestamp}
        "4edfff63-69d7-4f88-b984-5aec56533eca8" {$coll.DomesticCallingPrepaid = $AssignedTimestamp}
        "50nnn0f-f7e0-4b65-91d4-00d757788888c" {$coll.CommunicationCredits = $AssignedTimestamp}
        default{}
      }
      #####################################################################################
    }
      $collection.add($coll) | Out-Null
  }
  Write-Log -Message "Exporting the data to Report" -path $log
  $collection | Export-Csv $csv
  import-csv $csv | Export-Excel $Report1
}
catch{
  $exception = $_.Exception.Message
  Write-Log -Message "exception occured $exception" -path $log -Severity Error
  exit
}
Disconnect-AzureAD
Write-Log -Message "Script Finished" -path $log
#################################################################################################