简体   繁体   中英

How to create a local admin account in powershell studio

With this button create I am trying to prompt the user with a GUI that asks for the username and password, from there I want to create a folder and a local user that has administrative rights to the folder. But the code works in powershell and not 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 ')

    }




}

enter code here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM