AutoRek.IT.psm1
function mountNAS ($creds, $nasScriptLogsFolder) { $Global:nasName = "NAS" $description = "NAS Scriptlogs Folder" if ($creds) { try { Write-Host "Credentials supplied, attempting to connect to NAS..." -ForegroundColor Cyan New-PSDrive -Name $nasName -PSProvider FileSystem -Root $nasScriptLogsFolder -Description $description -Credential $creds -Scope Global } catch { handleError "Couldn't connect to the NAS as the supplied user" -stop $false } } else { Write-Host "Connecting to NAS as current runner $runner" -ForegroundColor Cyan try { New-PSDrive -Name $nasName -PSProvider FileSystem -Root $nasScriptLogsFolder -Description $description -Scope Global } catch { handleError "Couldn't connect to NAS as the current runner $runner" -stop $false } } if (Get-PSDrive -Name $nasName) { Write-Host "Connected to NAS" -ForegroundColor Green } else { Write-Host "Couldn't connect to NAS" -ForegroundColor Red } } function startLogging { param ( [string]$scriptname, [string]$logdirectory ) $Global:scriptName = $scriptname $Global:dateTime = Get-Date -Format s | foreach { $_ -replace ":", "-" } $Global:logfile = "$($logdirectory)\$($scriptname)-$($dateTime).log" if (!(Test-Path $logdirectory)) { Write-Host "$($logdirectory) does not exist. Creating it now" -ForegroundColor Yellow New-Item -Path $logdirectory -ItemType "directory" } Start-Transcript -Path $logfile -Append } function handleError ($message, [bool]$stop) { Write-Host "Error: $message" -ForegroundColor Red Write-Host "Exception: $($_.Exception.Message)" -ForegroundColor Red Write-Host "Stack Trace: $($_.Exception.StackTrace)" -ForegroundColor Red if ($stop) { Stop-Transcript exit } } function stopLogging { Stop-Transcript if (Get-PSDrive -Name $nasName) { Write-Host "NAS is reachable. Saving a copy of the log there" -ForegroundColor Yellow $hostname = $env:COMPUTERNAME $serialNumber = (Get-CimInstance -Class Win32_BIOS).SerialNumber $nasLogFile = "$($nasName):\$serialNumber-$hostname-$scriptName-$dateTime.log" try { Write-Host "Copying logfile to NAS" -ForegroundColor Yellow Copy-Item -Path $logfile -Destination $nasLogFile Write-Host "Script log saved to $nasLogFile" Write-Host "Removing NAS PSDrive" -ForegroundColor Yellow Remove-PSDrive -Name $nasName } catch { handleError "Error when trying to copy the script file to the NAS" -stop $false } } else { Write-Host "NAS isn't reachable. Logfile will only be saved locally" -ForegroundColor Yellow } } function checkforModule ($module) { function confirmInstallation ($module) { Write-Host "Confirming that $module is installed" -ForegroundColor Yellow if (Get-Module -ListAvailable -Name $module) { Write-Host "Module $module is installed!" -ForegroundColor Green } else { Write-Host "Module $module isn't installed" -ForegroundColor Red } } $trusted = Get-PSRepository -Name "PSGallery" | Select InstallationPolicy -ExpandProperty InstallationPolicy if ($trusted -eq "Untrusted") { Write-Host "PSGallery isn't trusted. Trusting it now. (why the trust issues?)" -ForegroundColor Yellow Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted } if (!(Get-Module -ListAvailable -Name $module)) { Write-Host "You don't have the $module installed. `nDon't worry fam, I'll install it for you" -ForegroundColor Yellow Install-Module -Name $module confirmInstallation $module } else { confirmInstallation $module } } function checkIfAdmin ($elevate) { if ($elevate) { $currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) $isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (!($isAdmin)) { Write-Host "Script must be run as Administrator." -ForegroundColor Red Stop-Transcript exit } } } 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 log { param ( $log, $Colour = "Yellow" ) Write-Host $log -ForegroundColor $Colour Start-Sleep -s 1 } function checkIfEXOConnected { $connectionInfo = Get-ConnectionInformation $connected = $null foreach ($connection in $connectionInfo) { if ($connection.Name -like "ExchangeOnline*") { Write-Host "Already connected to EXO." -ForegroundColor Yellow $connected = 1 } } if (!($connected)) { Write-Host "Not connected to EXO. Connecting now." -ForegroundColor Yellow Connect-ExchangeOnline } } function endScript () { Show-2Menu -Title "Do you want to disconnect ExchangeOnline and MSGraph?" -option1 "Yes" -option2 "No" $choice = Read-Host "Please make a selection" switch ($choice) { '1' { Write-Host "Disconnecting Exchange Online" -ForegroundColor Yellow Disconnect-ExchangeOnline -Confirm:$false Write-Host "Disconnecting MSGraph" -ForegroundColor Yellow Disconnect-Graph stopLogging } '2' { stopLogging } } } |