ntaflos.de

#copitos #8man #o365 #avepoint #microsoft #sharepoint #yammer #office365 #windows #nintex #fotografie

Kategorien-Archiv PowerShell

How to add a new a alias to an existing Office 365 group

How to add a new a alias to an existing Office 365 group

Tolles Script, sehr hilfreich! Ziel das umbennen einer O365 Gruppe!! Gefunden auf Microsoft

Scenario:

  • You want to add a new alias to an existing Office 365 Group.
  • You want external senders to be able to send emails to the new alias.

 

#——————————————————————————
#
# Copyright © 2017 Microsoft Corporation. All rights reserved.
#
# THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#——————————————————————————
#
# PowerShell Source Code
#
# NAME:
# Office365_add_alias.ps1
#
# VERSION:
# 1.0
#
#——————————————————————————
[CmdletBinding()]
param
(
[Parameter(Mandatory = $True, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$CurrentGroupAddress,
[Parameter(Mandatory = $True, Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$NewGroupAddress
)
#region Set Up Variables
$Matcheduser = $Null
$Matchedgroup = $Null
$MatchedOldGroup = $Null
$Objid = $Null
$CheckDomain = $Null
$AllUsers = $Null
$AllGroups = $Null
$MatchedUnifiedOldGroup = $Null
$MatchedRecipient = $Null
$MatchedMSODS = $Null
$MatchedEXO = $Null
$UserMatchFound = $false
$GroupMatchFound = $false
$MSODSEmailAddress = $Null
$CounterEXO = 6
$CounterMSODS = 6
$CheckEXO = $Null
$CheckMSODS = $Null
#endregion
#region Connect to Office 365
try
{
# Connect to Azure AD
Write-Host „Please enter your Office 365 credentials“ -ForeGroundColor Green
$O365Cred = Get-Credential
Import-Module MSOnline
Write-Host „`nConnecting to Azure AD…“
Connect-MsolService –Credential $O365Cred
# Connect to Exchange Online
Write-Host „`nConnecting to Exchange Online…“
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri „https://outlook.office365.com/powershell-liveid/“ -Credential $O365Cred -Authentication „Basic“ -AllowRedirection
Import-PSSession $O365Session -DisableNameChecking -AllowClobber | ft
}
catch
{
Write-Host „`nUnable to connect to Office 365`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
#endregion
#region Checking if the old address exists
Write-Host „Checking the address $CurrentGroupAddress in Exchange Online“
$MatchedUnifiedOldGroup = Get-UnifiedGroup -Identity $CurrentGroupAddress -ErrorAction SilentlyContinue | select Name, EmailAddresses
if ($MatchedUnifiedOldGroup -eq $Null)
{
Write-Host „`tI was not able to find $CurrentGroupAddress in Exchange Online `n“-Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
else
{
Write-Host „`tDone. The address $CurrentGroupAddress was found in Exchange Online`n“ -Foregroundcolor Yellow
}
Write-Host „Checking the address $CurrentGroupAddress in Azure AD“
$MatchedOldGroup = Get-MsolGroup -All -SearchString $CurrentGroupAddress | select ObjectId,proxyAddresses
if ($MatchedOldGroup -eq $Null)
{
Write-Host „`tI was not able to find $CurrentGroupAddress. Are you sure this is the primary address of the group?`n“-Foregroundcolor Red
$Objid = (Get-UnifiedGroup $CurrentGroupAddress).ExternalDirectoryObjectId
$MSODSEmailAddress = Get-MsolGroup -ObjectId $objid |select EmailAddress
Write-Host „Based on Exchange Online info $($MSODSEmailAddress.EmailAddress) might be the primary address. Would you like me to use this address instead of $($CurrentGroupAddress)? (Default is No)“ -Foregroundcolor Yellow
$Readhost = Read-Host „Enter your choice ( y / n ) “
Write-Host „`n“
Switch ($ReadHost)
{
Y {$CurrentGroupAddress = $MSODSEmailAddress.EmailAddress}
N {Read-Host “Press ENTER to exit…”; Exit}
Default {Read-Host “Press ENTER to exit…”; Exit}
}
}
else
{
Write-Host „`tDone. The address $CurrentGroupAddress was found `n“ -Foregroundcolor Yellow
}
#endregion
#region Verify the new address
Write-Host „Checking if the address $NewGroupAddress is part of an accepted domain“
$CheckDomain = Get-AcceptedDomain -Identity ($NewGroupAddress.Split(„@“))[1] -ErrorAction SilentlyContinue
If ($CheckDomain -eq $Null)
{
Write-Host „`tDomain does not exists or is not correct`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
else
{
Write-Host „`tDone. The domain was found `n“ -Foregroundcolor Yellow
}
#endregion
#region Checking if new address exists in MSODS
Write-Host „Checking if any user in Azure AD contains $NewGroupAddress (this may take a while)…“
$AllUsers = Get-MsolUser –All | Select userPrincipalName,proxyAddresses
$AllGroups = Get-MsolGroup –All | Select ObjectId,proxyAddresses
#loop all users
ForEach ($User in $AllUsers)
{
#loop proxyAddresses
ForEach ($UPA in $User.ProxyAddresses)
{
If ($UPA –Match $NewGroupAddress)
{
$UserMatchFound = $True
$Matcheduser = $User
}
}
}
write-host „`tDone. `n“ -Foregroundcolor Yellow
write-host „Checking if any group in Azure AD contains $NewGroupAddress (this may take a while)…“
#loop all groups
ForEach ($Group in $AllGroups)
{
#loop proxyAddresses
ForEach ($GPA in $Group.ProxyAddresses)
{
If ($GPA –Match $NewGroupAddress)
{
$GroupMatchFound = $True
$Matchedgroup = $Group
}
}
}
write-host „`tDone.`n“ -Foregroundcolor Yellow
If ($UserMatchFound -or $GroupMatchFound)
{
$MatchedMSODS = $true
If ($UserMatchFound)
{
Write-Host „`nThe new address exists in Azure AD“
Write-Host „The user $($Matcheduser.userPrincipalName) contains the following addresses: $($Matcheduser.ProxyAddresses)`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
Else
{
Write-Host „`nThe new address exists in Azure AD“
Write-Host „`The group $($Matchedgroup.ObjectId) contains the following addresses: $($Matchedgroup.ProxyAddresses)`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
}
Else
{
write-host „`tDone. No user or group found in Azure AD`n“ -Foregroundcolor Yellow
}
#endregion
#region Checking if new address exists in EXO
Write-Host „Checking the address $NewGroupAddress in Exchange Online “
$MatchedUnifiedGroup = Get-UnifiedGroup -Identity $NewGroupAddress -ErrorAction SilentlyContinue | select Name, EmailAddresses
$MatchedRecipient = Get-Recipient -Identity $NewGroupAddress -ErrorAction SilentlyContinue | select Name, EmailAddresses
if (($MatchedUnifiedGroup -ne $Null) -or ($MatchedRecipient -ne $Null))
{
$MatchedEXO = $true
If ($MatchedRecipient -ne $Null)
{
Write-Host „`tDone. receipint $($MatchedRecipient.name) contains the following addresses: $($MatchedRecipient.EmailAddresses) `n“ -Foregroundcolor Yellow
}
Else
{
Write-Host „`tDone. Unified Group $($MatchedUnifiedGroup.Name) contains the following addresses: $($MatchedUnifiedGroup.EmailAddresses) `n“ -Foregroundcolor Yellow
}
Write-Host „The new address already exists`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
Else
{
Write-Host „`tDone. No user or group was found in Exchange Online`n“ -Foregroundcolor Yellow
}
#endregion
#region Adding the new address
Write-Host „Finished all checks. Moving forward…`n“ -Foregroundcolor Green
if (!$MatchedMSODS -and !$MatchedEXO)
{
$Objid = (Get-UnifiedGroup $CurrentGroupAddress).ExternalDirectoryObjectId
Write-Host „Adding $NewGroupAddress in Exchange Online“
# add the new address
Set-UnifiedGroup -Identity $CurrentGroupAddress -EmailAddresses: @{add=“smtp:$NewGroupAddress“}
do
{
sleep 10
$CounterEXO–
$CheckEXO = Get-UnifiedGroup -Identity $NewGroupAddress
}
while (($CounterEXO -ge 0) -and ($CheckEXO -eq $Null))
if ($CheckEXO -eq $Null)
{
Write-Host „I was not able to add the new address`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
Else
{
Write-Host „`tDone.`n“ -Foregroundcolor Yellow
}
Write-Host „Adding $NewGroupAddress in Azure AD“
# Changing the primary address to force the sync
Get-UnifiedGroup $CurrentGroupAddress | Set-UnifiedGroup -PrimarySmtpAddress $NewGroupAddress
do
{
sleep 10
$CounterMSODS–
$CheckMSODS = Get-MsolGroup -All -SearchString $NewGroupAddress | select emailaddress
Write-Host „`tPlease wait…“
}
while (($CounterMSODS -ge 0) -and ($CheckMSODS -eq $Null))
if ($CheckMSODS -eq $Null)
{
Write-Host „I was not able to sync the new address`n“ -Foregroundcolor Red
Read-Host “Press ENTER to exit…”
Exit
}
Else
{
Write-Host „`tDone.`n“ -Foregroundcolor Yellow
}
# Changing back the primary address
Get-UnifiedGroup $NewGroupAddress | Set-UnifiedGroup -PrimarySmtpAddress $CurrentGroupAddress
Write-Host „`nAll Done! Wait 15 mins before doing a test!`n“ -Foregroundcolor Yellow
Read-Host “Press ENTER to exit…”
Remove-PSSession $O365Session
}
#endregion

PowerShell – Hinzufügen eines einzelnen Office 365-Benutzers via PowerShell und CSV

PowerShell – Hinzufügen eines einzelnen Office 365-Benutzers via PowerShell und CSV

Wenn Sie sich die Syntax des New-MsolUser-Cmdlets genauer ansehen, werden Sie feststellen, dass von den vielen Parametern nur -DisplayName und –UserPrincipalName erforderlich sind. Sie können entscheiden, welche Informationen Sie speichern möchten, aber meist werden Vorname, Nachname und Speicherort angegeben. Dadurch wird ein Benutzer erstellt. Um die Lizenz zuzuweisen, verwenden Sie die –LicenseAssignment-Option.

Hinzufügen eines einzelnen Office 365-Benutzers

Um dem Contoso-Mandanten einen einzelnen Benutzer, z. B. Caleb Sills, hinzuzufügen und ihm eine Lizenz für „ENTERPRISEPACK“ zuzuweisen, geben Sie Folgendes ein:

New-MsolUser -DisplayName “Caleb Sills” -FirstName Caleb ` -LastName Sills -UserPrincipalName calebs@contoso.onmicrosoft.com ` –Department Operations -UsageLocation US ` -LicenseAssignment contoso:ENTERPRISEPACK

Der Ausgabe wird ein zufälliges Kennwort zugewiesen:

Sie können mithilfe des Password-Parameters ein bestimmtes Kennwort zuweisen:

New-MsolUser -DisplayName “Caleb Sills” -FirstName Caleb ` -LastName Sills -UserPrincipalName calebs@contoso.onmicrosoft.com ` –Department Operations -UsageLocation US –PassWord TempP@@sW0rd ` -LicenseAssignment contoso:ENTERPRISEPACK

Das Kennwort muss stark sein, d. h., es muss mindestens einen Großbuchstaben und mindestens eine Zahl enthalten.

Gleichzeitiges Hinzufügen mehrerer Benutzer zu Office 365

Sie können eine durch Kommas getrennte Datei (CSV) verwenden, um mehrere Benutzer gleichzeitig hinzuzufügen. In diesem Beispiel erstellen Sie eine CSV-Datei mit Spalten für Abteilung (Department), Anzeigename (DisplayName), Vorname (FirstName), Nachname (LastName), Kennwort (Password), Verwendungsspeicherort (UsageLocation) und Benutzerprinzipalname (UserPrincipalName). Sie fügen Daten für drei Benutzer hinzu und verwenden dann Azure Active Directory-Modul für Windows PowerShell, um sie zur gleichen Zeit hinzuzufügen. Dieses CSV-Beispieldatei enthält auch ein Startkennwort.

Department DisplayName FirstName LastName Password UsageLocation UserPrincipalName
Operations ShawnM Shawn Melendez P@33word1 US ShawnM@contoso.onmicrosoft.com
IT LynneB Lynne Baxter P@33word2 US LynneB@contoso.onmicrosoft.com
Development ClaudeL Claude Loiselle P@33word3 US ClaudeL@contoso.onmicrosoft.com

Speichern Sie die obige CSV-Datei in C:\Input\NewAssignments.csv, und geben Sie Folgendes ein:

Import-Csv -Path C:\Input\NewAssignments.csv | ForEach-Object { ` New-MsolUser –Department $_.Department -FirstName $_.FirstName ` -DisplayName $_.DisplayName -LastName $_.LastName ` -Password $_.Password -UserPrincipalName $_.UserPrincipalName ` -LicenseAssignment ‚contoso:ENTERPRISEPACK‘ ` -UsageLocation $_.UsageLocation}| Export-Csv -Path c:\output\LogNewAssignments.csv

PowerShell_Beispiel

 

 

PowerShell – Exportieren User aus einer O365 Gruppe

PowerShell – Exportieren User aus einer O365 Gruppe

#Exportiere User aus Gruppe XYZ
#Import der Azure Active Directory Module (PowerShell Session)
Import-Module MSOnline

#Capture administrative credential for future connections.
$credential = get-credential

#Aktuelles Datum auslesen und in die Variable $Heute schreiben
$Heute = Get-Date -Format d

#Exportieren der Gruppen XYZ (GrouObjectID erforderlich)
Get-MsolGroupMember -GroupObjectId XYZ| export-csv C:\Reports\AlleUser_$Heute.csv