Share via


PowerShell ile güvenlik gruplarını yönetme

Bu makale hem Microsoft 365 Kurumsal hem de Office 365 Kurumsal için geçerlidir.

Güvenlik gruplarını yönetmek için Microsoft 365 yönetim merkezi alternatif olarak Microsoft 365 için PowerShell'i kullanabilirsiniz.

Bu makalede, ayarları listeleme, oluşturma, değiştirme ve güvenlik gruplarını kaldırma işlemleri açıklanmaktadır.

Bu makaledeki bir komut bloğu değişken değerleri belirtmenizi gerektirdiğinde bu adımları kullanın.

  1. Komut bloğunu panoya kopyalayın ve Not Defteri'ne veya PowerShell Tümleşik Betik Ortamı'na (ISE) yapıştırın.
  2. Değişken değerlerini doldurun ve "" ve "<>" karakterlerini kaldırın.
  3. Komutları PowerShell penceresinde veya PowerShell ISE'de çalıştırın.

PowerShell ile grup üyeliğini yönetmek için bkz. Güvenlik grubu üyeliğini koruma.

Microsoft Graph PowerShell kullanarak güvenlik gruplarını yönetme

Not

Azure Active Directory modülünün yerini Microsoft Graph PowerShell SDK'sı alır. Tüm Microsoft Graph API'lerine erişmek için Microsoft Graph PowerShell SDK'sını kullanabilirsiniz. Daha fazla bilgi için bkz. Microsoft Graph PowerShell SDK'sını kullanmaya başlama.

İlk olarak Microsoft 365 kiracınıza bağlanın.

Güvenlik gruplarının yönetilmesi için Group.ReadWrite.All izin kapsamı veya 'List subscribedSkus' Graph API başvuru sayfasında listelenen diğer izinlerden biri gerekir. Bu makaledeki bazı komutlar farklı izin kapsamları gerektirebilir; bu durumda bu durum ilgili bölümde not edilir.

Connect-Graph -Scopes Group.ReadWrite.All

Gruplarınızı listeleme

Tüm gruplarınızı listelemek için bu komutu kullanın.

Get-MgGroup -All

Belirli bir grubun ayarlarını görünen adına göre görüntülemek için bu komutları kullanın.

$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }

Yeni grup oluşturma

Yeni bir güvenlik grubu oluşturmak için bu komutu kullanın.

Connect-MgGraph -Scopes "Group.Create"
New-MgGroup -Description "<group purpose>" -DisplayName "<name>" -MailEnabled:$false -SecurityEnabled -MailNickname "<email name>"

Grubun ayarlarını görüntüleme

Bu komutlarla grubun ayarlarını görüntüleyin.

$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName } | Select-Object *

Güvenlik grubunu kaldırma

Güvenlik grubunu kaldırmak için bu komutları kullanın.

$groupName="<display name of the group>"
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
Remove-MgGroup -GroupId $group.Id

Güvenlik grubunun sahiplerini yönetme

Bir güvenlik grubunun geçerli sahiplerini görüntülemek için bu komutları kullanın.

$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "GroupMember.Read.All"

# Display group owners
Get-MgGroupOwner -GroupId (Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }).Id

Bir güvenlik grubunun geçerli sahiplerine kullanıcı asıl adına (UPN) göre bir kullanıcı hesabı eklemek için bu komutları kullanın.

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
$userId = (Get-MgUser -Filter "userPrincipalName eq '$userUPN'").Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

Bir güvenlik grubunun geçerli sahiplerine görünen adına göre bir kullanıcı hesabı eklemek için bu komutları kullanın.

$userName="<Display name of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.Read.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }
$userId = (Get-MgUser -All | Where-Object { $_.DisplayName -eq $userName }).Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

Bir kullanıcı hesabını UPN'siyle bir güvenlik grubunun geçerli sahiplerinden kaldırmak için bu komutları kullanın.

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'" | Select-Object -First 1
$user = Get-MgUser -Filter "userPrincipalName eq '$userUPN'" | Select-Object -First 1

# Remove the user from the group
Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

Bir kullanıcı hesabını bir güvenlik grubunun geçerli sahiplerinden görünen adına göre kaldırmak için bu komutları kullanın.

$userName="<Display name of the user account to remove>"
$groupName="<display name of the group>"
$group = Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }
$user = Get-MgUser | Where-Object { $_.DisplayName -eq $userName }

Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

Ayrıca bkz.

PowerShell ile Microsoft 365 kullanıcı hesaplarını, lisanslarını ve gruplarını yönetme

PowerShell ile Microsoft 365’i yönetme

Microsoft 365 için PowerShell'i kullanmaya başlama