我正在尝试使用PowerShell脚本创建包含xml源文件的批量本地用户,其中包含所有详细信息。下面是我的示例xml文件,其中包含用于创建用户的代码。任何人都可以帮我解决这个问题吗?
# To run this script use: & "C:\Users\rLisdonk\Desktop\ToServer\Test.ps1"
$computerName = "USSECAVDSPDWK27"
$serviceAccountWebName = "saAsaWeb"
$serviceAccountWebPassword = "MyPassword123"
"Get computer info"
$computer = [ADSI]("WinNT://" + $computerName + ",computer")
"Determine if user [saAsaWeb] exists"
$serviceAccount = [ADSI]("WinNT://" + $computerName + "/$serviceAccountWebName" + ",user")
if(!$serviceAccount.Name)
{
"Create user [saAsaWeb]"
$user = $computer.Create("user", $serviceAccountWebName)
"Set password"
$user.SetPassword($serviceAccountWebPassword)
$user.SetInfo()
"Disable [User must change password at next logon]"
$user.PasswordExpired = 0
$user.SetInfo()
"Enable [Password never expires]"
$wmiuser = Get-WmiObject -class "Win32_UserAccount" -filter "name=’$serviceAccountWebName’"
$wmiuser.PasswordExpires = $false
$wmiuser.Put()
}
答案 0 :(得分:1)
Powershell只会用双引号内的值替换变量,单引号将返回字面值。你会想要使用'反引号字符来转义单引号,所以它将是:
$wmiuser = Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND name=`'$serviceAccountWebName`'"
运行它时,需要使用提升的权限运行它。如果您希望在远程计算机上执行此操作,则需要通过远程处理来完成此操作,或者完全使用WMI。如果没有指定的错误,我认为这个WMI查询最有可能阻碍你。