如何在powershell studio中创建本地管理员帐户

时间:2018-06-17 09:00:15

标签: javascript powershell

使用此按钮创建我试图用一个要求输入用户名和密码的GUI来提示用户,从那里我想创建一个文件夹和一个对该文件夹具有管理权限的本地用户。但代码适用于powershell而不是powershell studio

$buttonCreate_Click= {

$switch = "True"
if (-not $textbox1.Text)
{
    [System.Windows.Forms.MessageBox]::Show($owner, 'Error, please enter information')
    #Throw 'No Username Specified'   

}

else
{
    $name = $textbox1.Text
    $Location = "C:\$name"
    $compname = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name


    If ((Test-Path $Location) -eq $False)
    {
        New-Item -ItemType directory -Path $Location

        $Username = $textbox1.Text
        $Password = $textbox2.Text

        $group = "Administrators"

        $adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
        $existing = $adsi.Children | Where-Object { $_.SchemaClassName -eq 'user' -and $_.Name -eq $Username }

        if ($existing -eq $null)
        {

            Write-Host "Creating new local user $Username."
            & NET USER $Username $Password /add /y /expires:never

            Write-Host "Adding local user $Username to $group."
            & NET LOCALGROUP $group $Username /add

        }
        else
        {
            Write-Host "Setting password for existing local user $Username."
            $existing.SetPassword($Password)
        }

        Write-Host "Ensuring password for $Username never expires."
        & WMIC USERACCOUNT WHERE "Name='$Username'" SET PasswordExpires=FALSE




    } # End of folder exists test
    Else
    {
        [System.Windows.Forms.MessageBox]::Show($owner, 'Folder Already Exists ')

    }




}

在这里输入代码

1 个答案:

答案 0 :(得分:0)

我添加了结束'}'并成功测试了您的代码,确保右键单击并以管理员身份运行PowerShell Studio。