zoukankan      html  css  js  c++  java
  • Install-User.ps1

     Install-User.ps1

    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 $_
        }
    }
  • 相关阅读:
    #ASP.NET Core 1.0 Key Features
    #asp.net core mvc 的视图注入
    # asp.net core 1.0 项目结构
    dotnet core 初试两个小问题解决
    1044 火星数字(20 分)
    1043 输出PATest(20 分)
    1042 字符统计(20 分)
    1041 考试座位号(15 分)
    1040 有几个PAT(25 分)
    1039 到底买不买(20 分)
  • 原文地址:https://www.cnblogs.com/edward2013/p/3536644.html
Copyright © 2011-2022 走看看