[英]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. 使用“创建”按钮,我试图用要求用户名和密码的GUI提示用户,从中我要创建一个文件夹以及一个对该文件夹具有管理权限的本地用户。 But the code works in powershell and not powershell studio
但是代码可以在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 ')
}
}
enter code here 在这里输入代码
我添加了结尾的'}'并成功测试了您的代码,请确保右键单击并以管理员身份运行PowerShell Studio。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.