英文:
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 = "test"
FullName = "Test Admin"
Description = "Test User Admin"
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 "Administrators" -Member $user
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $user
<sup>Setting -PasswordNeverExpires $true
shouldn't be necessary anyway, because you created the user with option -NoPassWord
AND UserMayNotChangePassword
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论