2014年6月9日 星期一

Write a "add new user account" PowerShell script

This sample teach you how to add new user account with PowerShell.
There are two method as following:

param (
  [Parameter(Mandatory=$false)][String[]]$computer = ".",
  [Parameter(Mandatory=$false)][String[]]$computerName = $env:computername,
  [Parameter(Mandatory=$false)][String]$DefaultDomainName = $env:USERDOMAIN,
  [Parameter(Mandatory=$false)][String]$DefaultUserName = "test",
  [Parameter(Mandatory=$false)][String]$DefaultPassword  = "pwd",
  [Parameter(Mandatory=$false)][String]$Group  = "administrators",
  [Parameter(Mandatory=$false)][Int]$AutoLogonCount
 )



Method 1、Create new user with "ADSI"(Active Directory Service Interfaces)

function create_user_netuser(){
net user /add $DefaultUserName $DefaultPassword
net localgroup $Group $DefaultUserName /add
}

Method 1、Create new user with "net use"

function create_user_ADSI(){
$objou=[ADSI]"WinNT://$computerName,computer"
$objuser=$objou.Create("user", $DefaultUserName)
$objuser.SetInfo()
$objuser.SetPassword($DefaultPassword)

$objou=[ADSI]("WinNT://$computerName/$Group,group")
$objou.add("WinNT://$DefaultUserName,user")
}


沒有留言:

張貼留言