简体   繁体   中英

How to write a simple Powershell script to assign a user to an AD group

I just getting into writing Powershell scripts to perform repetitive tasks. I want to write a script that will prompt for an ADgroup, and an AD userID, then add that user to the security group.

It starts like this:

  $SecurityGroup = Read-Host -Prompt 'Input the SecurityGroup name'
  $User = Read-Host -Prompt 'Input the userID'
  Get-ADGroup '$SecurityGroup' | Add-ADGroupMember -Members '$User'
  (GET-ADUSER –Identity '$user' –Properties MemberOf | Select-Object MemberOf).MemberOf
  Write-Host 'The user with userID '$user' has been added to the SecurityGroup '$SecurityGroup'

Replace the single quotes surrounding your variables with double quotes " or remove the quotes completely. The quotes are not necessary for your usage.

$user = "SomeUser"
Write-Host '$user' # Output: $user / variables inside single quotes will be left as-is
Write-Host "$user" # Output: SomeUser / variables inside double-quotes will be expanded
Write-Host $user   # Output: SomeUser

Line

Write-Host 'The user with userID '$user' has been added to the SecurityGroup  $SecurityGroup'

should look something like this (with double quotes surrounding)

Write-Host "The user with userID '$user' has been added to the SecurityGroup '$SecurityGroup'"

About Quoting Rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM