【问题标题】:Powershell: New-LocalUser -A positional parameter cannot be found that accepts argument 'True'Powershell:New-LocalUser - 找不到接受参数“True”的位置参数
【发布时间】:2018-06-25 00:51:50
【问题描述】:

我一直在关注this guide,尝试编写一个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

但是,运行此程序时,我收到以下错误:

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

如果有人能看出我哪里出错了,我将不胜感激任何帮助或建议,谢谢!

【问题讨论】:

    标签: powershell parameter-passing


    【解决方案1】:

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

    $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
    

    【讨论】:

    • 如果你想将值传递给他们,你会这样做:-UserMayNotChangePassword:$true
    • @EliRushbrook 如果您发现它解决了您的问题,请考虑accepting the answer
    猜你喜欢
    • 2016-07-06
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多