Y
Y
Young_nigilist2020-05-19 18:36:38
PowerShell
Young_nigilist, 2020-05-19 18:36:38

Powershell script how to set a condition?

Good afternoon. There is a script in which there is an addition of groups from a csv file

foreach($group in $Groups)
{
$create_group = New-ADGroup -Name $group.GroupName -groupScope $group.GroupScope -Path $group.ou
}

How to write a condition for checking existing groups?
There is also a code for adding a user to a group. Data is taken from csv file
foreach($line in $UserGroup)
{
$userGroup=$line.ADUsers -split ";"
$groupsList = $line.ADGroup

foreach($user in $line)
{
Add-ADGroupMember -Identity $groupsList -Members $userGroup
}
}

How can I add a user to a group and indicate this group to him as the primary (primary)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Dmitriev, 2020-05-19
@Young_nigilist

1. Google "powershell if group exists".
2. Google "powershell how to add user to group" and "powershell change primary group".

A
akelsey, 2020-05-19
@akelsey

Try like this:

How to write a condition for checking existing groups?

If (Get-AdGroup MyGroup) {Write-Host "Ok"} else {Write-Host "Not Ok"}

M
MaxKozlov, 2020-05-20
@MaxKozlov

1. Do you need it at all? well, there is already such a group, well, it will give an error ... what will change for the result?
2.

Set-ADUser -Replace @{primaryGroupID = $primaryGroupToken}
, where $primaryGroupToken is the corresponding attribute of the desired group.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question