使用powershell脚本将xml文件作为源创建本地用户
我正在尝试使用powershell脚本创建包含所有详细信息的xml源文件的批量本地用户。下面是我的示例xml文件,其中包含用于创建用户的代码。有人能帮我把这件事做好吗使用powershell脚本将xml文件作为源创建本地用户,powershell,Powershell,我正在尝试使用powershell脚本创建包含所有详细信息的xml源文件的批量本地用户。下面是我的示例xml文件,其中包含用于创建用户的代码。有人能帮我把这件事做好吗 # To run this script use: & "C:\Users\rLisdonk\Desktop\ToServer\Test.ps1" $computerName = "USSECAVDSPDWK27" $serviceAccountWebName = "saAsaWeb" $serviceAccoun
# 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()
}
Powershell将仅使用双引号内的值替换变量,单引号将返回文本值。您需要使用`反勾号字符转义单引号,因此:
$wmiuser = Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND name=`'$serviceAccountWebName`'"
运行它时,需要使用提升的权限运行它。如果您希望在远程计算机上执行此操作,则需要通过远程处理或完全使用WMI。如果没有指定的错误,我假设这个WMI查询很可能是阻碍您的原因