Powershell 未转义密码的特殊字符

问题描述 投票:0回答:1

所有,我正在编写一个脚本来批量创建本地用户,除了密码创建部分之外,该脚本工作正常。该脚本从 csv 中提取信息,但是当我使用我认为应该使用的密码测试帐户时,它失败了。我感觉这是由于特殊字符没有被解析。这是我在 csv 密码列中的示例密码。 SDS 科学家密码 $2024 我尝试使用单引号 'Passw0rdforSDSSc1entist$2024' 和双引号“Passw0rdforSDSSc1entist$2024”,以及放置转义符 (

) before the $, Passw0rdforSDSSc1entist
$2024 但仍然不起作用。

# Set variables
$csvFile = "C:\Temp2\Manage_Local_Accounts\Local_Accounts.csv"
$group = "SDS Scientists"

# Import CSV file
$users = Import-Csv $csvFile

# Loop through each user in CSV file
foreach ($user in $users) {
  # Set username and password
  $username = $user.username
  $password = $user.password
  $fullname = $user.FullName

  # Create new user
  $userObj = New-LocalUser -Name $username -Password (ConvertTo-SecureString -String $password -AsPlainText -Force) -FullName $fullname -Description "Created to use with Sequence Detection Software " -PasswordNeverExpires:$false

  # Set "User must change password at next logon" option
  net user $username /logonpasswordchg:No

  # Add user to group
  Add-LocalGroupMember -Group $group -Member $username
} 
powershell
1个回答
0
投票

您提供的代码正在运行。

您确定您的新用户有足够的权限来执行“runas”操作吗?如果您想检查密码的有效性,最有效的方法是以该用户身份登录。如果您想自动检查密码,可以使用以下脚本:

Add-Type -assemblyname system.DirectoryServices.accountmanagement 
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
$DS.ValidateCredentials("$username", "$password")

要验证字符翻译,您还可以解密安全字符串:

$secure = ConvertTo-SecureString -String $password -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
© www.soinside.com 2019 - 2024. All rights reserved.