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'"
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.