Add optional -Encoding flag

Signed-off-by: Luke Tainton <luke@tainton.uk>
This commit was merged in pull request #11.
This commit is contained in:
2020-08-07 17:46:17 +01:00
committed by Luke Tainton
parent 20307da1e3
commit ab72c8c2ba

View File

@@ -16,6 +16,9 @@ Function Import-TeamsUsers {
.PARAMETER Delimiter .PARAMETER Delimiter
If specified, overrides the default CSV delimiter of ','. If specified, overrides the default CSV delimiter of ','.
.PARAMETER Encoding
If specified, manually sets the encoding of the CSV file.
.EXAMPLE .EXAMPLE
Import-TeamsUsers -File "users.csv" Import-TeamsUsers -File "users.csv"
@@ -29,27 +32,35 @@ Function Import-TeamsUsers {
[parameter(Mandatory=$false, position=2, ParameterSetName='Params', HelpMessage="Create new Teams group")] [parameter(Mandatory=$false, position=2, ParameterSetName='Params', HelpMessage="Create new Teams group")]
[switch]$Create, [switch]$Create,
[parameter(Mandatory=$false, position=3, ParameterSetName='Params', HelpMessage="Override default CSV delimiter")] [parameter(Mandatory=$false, position=3, ParameterSetName='Params', HelpMessage="Override default CSV delimiter")]
[string]$Delimiter [string]$Delimiter,
[parameter(Mandatory=$false, position=4, ParameterSetName='Params', HelpMessage="Manually set CSV encoding")]
[string]$Encoding
) )
Begin { Begin {
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
##### IMPORT CSV FILE ##### ##### IMPORT CSV FILE #####
Try { Try {
If ($Delimiter) { $ImportCmd = "Import-CSV $File"
$Users = Import-CSV $File -Delimiter $Delimiter If ($Delimiter) { $ImportCmd = $ImportCmd + " -Delimiter $Delimiter" }
} Else { If ($Encoding) { $ImportCmd = $ImportCmd + " -Encoding $Encoding" }
$Users = Import-CSV $File $Users = Invoke-Expression $ImportCmd
}
} Catch { } Catch {
Write-Host -ForegroundColor Red "$File is not a valid CSV file." Write-Host -ForegroundColor Red "$File is not a valid CSV file."
Exit
} }
##### CHECK MODULE IS INSTALLED AND IMPORTED ##### ##### CHECK MODULE IS INSTALLED AND IMPORTED #####
if (Get-Module -ListAvailable -Name MicrosoftTeams) { if (Get-Module -ListAvailable -Name MicrosoftTeams) {
try {
Import-Module -Name MicrosoftTeams Import-Module -Name MicrosoftTeams
$Email = (Connect-MicrosoftTeams -Verbose:$false).Account $Email = (Connect-MicrosoftTeams -Verbose:$false).Account
} Catch {
Write-Host -ForegroundColor Red "There was an error during authentication."
Write-Host "If you're not on Windows and use Multi-Factor Authentication, please manually pass the MFA check in your browser, then try again."
Exit
}
} else { } else {
Write-Host -ForegroundColor Red "Module MicrosoftTeams doesn't exist. Please run 'Install-Module -Name MicrosoftTeams' and retry." Write-Host -ForegroundColor Red "Module MicrosoftTeams doesn't exist. Please run 'Install-Module -Name MicrosoftTeams' and retry."
Exit Exit