0

我刚刚开始编写 Powershell 脚本来执行重复性任务。我想编写一个脚本,提示输入 AD 组和 AD 用户 ID,然后将该用户添加到安全组。

它是这样开始的:

  $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'
4

1 回答 1

0

将变量周围的单引号替换为双引号 " 或完全删除引号。引号对于您的使用不是必需的。

$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

线

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

应该看起来像这样(用双引号括起来)

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

关于报价规则

于 2021-02-03T06:24:16.927 回答