我有一个用户列表,我需要授予对一组Windows服务的访问权限。它适用于已定义的一组用户,但我希望使脚本更具动态性并动态传递用户名和组名。我可以foreach
,但我希望能够在每个用户的开头为域名添加前缀。例如:
$usrs = "abc", "def", "ghi", "jkl"
当我执行foreach ($usr in $usrs)
时,需要在"abc"
前加上"AU\abc"
,依此类推。
答案 0 :(得分:2)
一种方法:
$usrs = "abc", "def", "ghi", "jkl" | % { "AU\"+$_ }
答案 1 :(得分:2)
这应该有所帮助。
$usrs = "abc", "def", "ghi", "jkl"
foreach ($user in $usrs) {
"AU\${user}"
}
此外,启动PowerShell 4.0,您也可以这样做。
$usrs = "abc", "def", "ghi", "jkl"
$usrs.ForEach({"AU\$_"})
答案 2 :(得分:0)
你可以试试这个:
foreach($usr in $usrs) {
$user = "AU\$usr"
#Continue script...
}
或者如果您需要替换现有阵列:
$usrs = $usrs | Foreach-Object { "AU\$_" }
#or
$usrs = foreach($usr in $usrs) { "AU\$usr" }