Powershell:New-LocalUser - 找不到接受参数“True”的位置参数

3 powershell parameter-passing

我一直在按照本指南尝试编写一个 powershell 脚本来向 Windows 添加一个新的本地用户。

我需要用户名、全名、描述和密码,并检查PasswordNeverExpiresUserMayNotChangePassword。我已经写了以下尝试这样做:

$Password = Read-Host -AsSecureString -Prompt 'Create password for new user account:'
New-LocalUser -Name "NewUser" -FullName "New User" -Description "New Execution Account" -Password $Password -PasswordNeverExpires $true -UserMayNotChangePassword $true
Run Code Online (Sandbox Code Playgroud)

但是,在运行它时,我收到以下错误:

New-LocalUser :找不到接受参数“True”的位置参数。在 line:2 char:1
+ New-LocalUser -Name "NewUser" -FullName "New User" -Description "New ...
+ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-LocalUser], ParameterBindingException
+fullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewLocalUserCommand

如果有人能看到我出错的地方,我将不胜感激任何帮助或建议,谢谢!

Ola*_*laf 5

最后两个参数是开关参数。您不需要向他们传递参数。只需按原样提供它们就足够了。像这样:

$Password = Read-Host -AsSecureString -Prompt 'Create password for new user account:'
New-LocalUser -Name "NewUser" -FullName "New User" -Description "New Execution Account" -Password $Password -PasswordNeverExpires -UserMayNotChangePassword
Run Code Online (Sandbox Code Playgroud)

  • 如果您无论如何都想将值传递给它们,您可以这样做:`-UserMayNotChangePassword:$true`。 (3认同)