Power Shell issue with create user

huangapple go评论59阅读模式
英文:

Power Shell issue with create user

问题

这里有关于创建本地用户的问题吗?
$user New-LocalUser -Name "test" -NoPassword -AccountNeverExpires -UserMayNotChangePassword -FullName "Test Admin" -Description "Test User Admin " | Set-LocalUser -PasswordNeverExpires $true

英文:

what is here wrong with create a local user?
$user New-LocalUser -Name "test" -NoPassword -AccountNeverExpires -UserMayNotChangePassword -FullName "Test Admin" -Description "Test User Admin " | Set-LocalUser -PasswordNeverExpires $true

答案1

得分: 0

这不起作用是因为在用户创建本身已经添加了 set-Localuser

$user = New-LocalUser -Name "test" -NoPassword -AccountNeverExpires -UserMayNotChangePassword -FullName "Test Admin" -Description "Test User Admin"
$user | Set-LocalUser -PasswordNeverExpires $true

Add-LocalGroupMember -Group "Administrators" -Member $user
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $user
英文:

It is not working because of set-Localuser is added in user creation itself.

$user = New-LocalUser -Name "test" -NoPassword -AccountNeverExpires -UserMayNotChangePassword -FullName "Test Admin" -Description "Test User Admin " 
$user | Set-LocalUser -PasswordNeverExpires $true

Add-LocalGroupMember -Group "Administrators" -Member $user
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $user

答案2

得分: 0

因为你直接通过New-LocalUser返回的对象传递给Set-LocalUser,捕获变量$user将为$null
正如你在文档中所看到的,Set-LocalUser cmdlet 不返回任何输出

修复方法很简单:将这两个命令分为两行。

此外,我建议在需要大量参数的cmdlet上使用Splatting,以帮助保持代码的可读性,而不是使用极长的代码行。

# 创建一个包含所有参数及其值的哈希表,供New-LocalUser cmdlet使用
$userParams = @{
    Name                     = "test"
    FullName                 = "Test Admin"
    Description              = "Test User Admin"
    AccountNeverExpires      = $true
    NoPassword               = $true
    UserMayNotChangePassword = $true
}

# 创建用户,然后在用户对象上设置另一个属性
$user = New-LocalUser @userParams
$user | Set-LocalUser -PasswordNeverExpires $true

Add-LocalGroupMember -Group "Administrators" -Member $user
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $user

<sup>设置`-PasswordNeverExpires $true`本来不应该是必要的因为你创建用户时选择了`-NoPassWord`和`UserMayNotChangePassword`选项</sup>
英文:

Because you pipe the returned object of New-LocalUser directly through to Set-LocalUser, the capturing variable $user will be $null.
As you can see in the docs, the Set-LocalUser cmdlet returns no output

The fix is easy: seperate these two commands in two separate lines.

Also, I would recommend using Splatting on cmdlets that take a lot of parameters to help keep the code readable as opposed to using extremely long code lines.

# create a Hashtable with all parameters and their values 
# for use with the New-LocalUser cmdlet
$userParams = @{
    Name                     = &quot;test&quot;
    FullName                 = &quot;Test Admin&quot;
    Description              = &quot;Test User Admin&quot;
    AccountNeverExpires      = $true
    NoPassword               = $true
    UserMayNotChangePassword = $true
}

# create the user and afterwards set another property to the user object
$user = New-LocalUser @userParams
$user | Set-LocalUser -PasswordNeverExpires $true

Add-LocalGroupMember -Group &quot;Administrators&quot; -Member $user
Add-LocalGroupMember -Group &quot;Remote Desktop Users&quot; -Member $user

<sup>Setting -PasswordNeverExpires $true shouldn't be necessary anyway, because you created the user with option -NoPassWord AND UserMayNotChangePassword</sup>

huangapple
  • 本文由 发表于 2023年2月16日 15:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定