首页 > 其他 > 详细

Install-User.ps1

时间:2014-01-31 13:34:08      阅读:438      评论:0      收藏:0      [点我收藏+]

 Install-User.ps1

bubuko.com,布布扣
function Install-User
{
    param(
        [Parameter()]
        [string]$ComputerName = $env:computername,
        
        [Parameter(Mandatory=$true)]
        [string]$UserName,
        
        [Parameter(Mandatory=$true)]
        [string]$Password,
        
        [Parameter()]
        [bool]$PasswordChangeable = $true,
        
        [Parameter()]
        [bool]$PasswordExpired = $true,
        
        [Parameter()]
        [string]$Description,
        
        [Parameter()]
        [string]$FullName,
        
        [Parameter()]
        [string]$Group,
        
        [Parameter()]
        [switch]$PassThru
    )
    
    Write-Verbose "Installing user ‘$Username‘ on ‘$ComputerName‘..."
    
    if(!(Test-Connection $ComputerName -Count 1 -Quiet))
    {
        Write-Error "Unable to connect ‘$ComputerName‘. The network path not found."
        return
    }
    try
    {
        if([ADSI]::Exists("WinNT://$ComputerName/$UserName"))
        {
                Write-Error "User ‘$UserName‘ is already exist on ‘$ComputerName‘."
                return
        }
        
        if($Group)
        {
            if(!([ADSI]::Exists("WinNT://$ComputerName/$Group")))
            {
                Write-Error "Group ‘$Group‘ could not be found on ‘$ComputerName‘."
                return
            }
        }
        
        #Create User account
        $account = ([ADSI]"WinNT://$ComputerName,computer").Create(user,$UserName)
        #Set password on account
        $account.psbase.invoke("SetPassword",$Password)
        #Commit the changes made
        $account.psbase.CommitChanges()
        #Set description on account
        if($Description) { $account.description = $Description }
        #Set description on account
        if($FullName) { $account.fullname = $FullName }
        #Set flag for password to not expire
        if(!$PasswordExpired)
        {
            $ADS_UF_DONT_EXPIRE_PASSWD = 0x10000
            $account.userflags = $account.userflags[0] -bor $ADS_UF_DONT_EXPIRE_PASSWD
        }
        #Set flag for not allow user to change password
        if(!$PasswordChangeable) {
            $ADS_UF_DO_NOT_ALLOW_PASSWD_CHANGE = 0x0040
            $account.userflags = $account.userflags[0] -bor $ADS_UF_DO_NOT_ALLOW_PASSWD_CHANGE
        }
        #Commit the changes
        $account.psbase.CommitChanges()
        Write-Verbose "Creating user ‘$Username‘ on ‘$ComputerName‘ was successfully."
        
        if($Group)
        {
            #Add account to Local group
            $localGroup = [ADSI]"WinNT://$ComputerName/$Group,group"
            $localGroup.PSBase.Invoke("Add",$account.PSBase.Path)
            Write-Verbose "Adding user ‘$Username‘ to group ‘$Group‘ on ‘$ComputerName‘ was successfully."
        }
        
        Write-Verbose "User ‘$Username‘ has been installed on ‘$ComputerName‘."
        
        if($Passthru)
        {
            $pso = New-Object PSObject -Property @{
                 ComputerName = $ComputerName.ToUpper()
                 UserName = $UserName
                 FullName = $FullName
                 Description = $Description
                 PasswordExpired = $PasswordExpired
                 PasswordChangeable = $PasswordChangeable
                 Group = $Group
            }
            $pso.PSTypeNames.Clear()
            $pso.PSTypeNames.Add(MKServerBuilder.UserAccount)
            $pso
        }
    }
    catch
    {
        Write-Error $_
    }
}
bubuko.com,布布扣

Install-User.ps1

原文:http://www.cnblogs.com/edward2013/p/3536644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!