BIGCommonPS7.psm1

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function PS7Check {
    if ($PSVersionTable.PSVersion -notlike "7.*") {
        throw "You need to be running PowerShell 7 to use this function"
    }
}

function sleepforone {
    Start-Sleep -s 1
}

function sleepfortwo {
    Start-Sleep -s 2
}

function installChoco {
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}

# ExchangeOnlineManagement
function checkIfExchangeModuleExists {
    if (!(Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
        Write-Host "You don't have the Exchange Online Management module installed.`nDon't worry though, I'll install it for you"`
         -ForegroundColor Yellow
        Install-Module -Name ExchangeOnlineManagement
    } elseif (Get-Module -ListAvailable -Name ExchangeOnlineManagement) {
        Write-Host "Exchange Online Management module is installed!" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name ExchangeOnlineManagement
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-eom {
    param (
        $credential
    )
    checkIfExchangeModuleExists
    Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
    Connect-ExchangeOnline -ShowProgress $true -Credential $credential
}

# Az
function checkifAzModuleExists {
    PS7Check
    if (!(Get-Module -ListAvailable -Name Az)) {
        Write-Host "You don't have the Az module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name Az
    } elseif (Get-Module -ListAvailable -Name Az) {
        Write-Host "Az module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name Az
        #Import-Module -Name Az.Accounts
        Write-Host "If the authentication box doesn't appear, check behind the VSCode window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-az {
    param (
        $credential
    )
    PS7Check
    checkifAzModuleExists
    Connect-AzAccount -credential $credential
}

function checekifTeamsModuleExists {
    if (!(Get-Module -ListAvailable -Name MicrosoftTeams)) {
        Write-Host "You don't have the Microsoft Teams module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name MicrosoftTeams -Force
    } elseif (Get-Module -ListAvailable -Name MicrosoftTeams) {
        Write-Host "Microsoft Teams module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name MicrosoftTeams
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-msteams {
    param (
        $credential
    )
    checekifTeamsModuleExists
    Connect-MicrosoftTeams -credential $credential
}

function checkifPnPExists {
    if (!(Get-Module -ListAvailable -Name PnP.Powershell)) {
        Write-Host "You don't have the PnP module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name PnP.Powershell -Force
    } elseif (Get-Module -ListAvailable -Name PnP.Powershell) {
        Write-Host "PnP module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name PnP.Powershell
    }
}

function Show-2Menu
{
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2
    )
    Clear-Host
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: Press 'ctrl + c' to quit."
}

function Show-2Menu-NoClear
{
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2
    )
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: Press 'ctrl + c' to quit."
}

function Show-4Menu {
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2,
        $option3,
        $option4
    )
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: $($option3)."
    Write-Host "4: $($option4)."
    Write-Host "Press 'ctrl + c' to quit."
}

function beVerbose {
    param (
        $beVerbose,
        $Colour = "Yellow"
    )
    Write-Host $beVerbose -ForegroundColor $Colour
    sleepforone
}

function startLogging {
    param (
        [string]$scriptname
    )
        $logdirectory = "C:\BIGLogs"
        [string]$dateTime = Get-Date -Format s | foreach {$_ -replace ":", "-"}
        $logfile = "$($logdirectory)\$($scriptname)-$($dateTime).log"

        if (!(Test-Path $logdirectory)) {
            Write-Host "$($logdirectory) does not exist. Creating it now" -ForegroundColor Yellow
            New-Item -Path "C:\" -Name "BIGLogs" -ItemType "directory"
        }

        Start-Transcript -Path $logfile -Append
}

function sendTeamsMessage {
<#
    .SYNOPSIS
        This function will send a message in the "leaver feed" channel of the IT Staff Team
    .PARAMETER Title
        This is the title of the message in teams
    .PARAMETER Text
        This is the main body of the text. You'll most likely want the error in here
    .PARAMETER JSONBody
        Do not use this parameter, the title and text parameters will be used in this JSON message. It just consolidates the message.
    .EXAMPLE
        sendTeamsMessage -Title "Users PLM" -Text "Remember to check if the user had a PLM account and to disable it"
#>
    
    param (
        $TeamsURI,
        $Title,
        $Text,
        $JSONBody = [PSCustomObject][Ordered]@{
            "@type"      = "MessageCard"
            "@context"   = "http://schema.org/extensions"
            "summary"    = "Incoming Alert Message!"
            "themeColor" = '0078D7'
            "title"      = "$($Title)"
            "text"       = "$($Text)"
            "sections"   = @(
                @{
                    "activityTitle"     = "Alert Subsection"
                    "facts"             = @(
                        @{
                            "name"      = "Hostname"
                            "value"     = "$($runnerhostname)"
                        },
                        @{
                            "name"      = "Log file"
                            "value"     = "$($logfile)"
                        }
                    )
                    "markdown" = $true
                }
            )
        }
    )
    $TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
    $parameters = @{
        "URI"         = $TeamsURI
        "Method"      = 'POST'
        "Body"        = $TeamMessageBody
        "ContentType" = 'application/json'
    }
    Invoke-RestMethod @parameters | Out-Null
}