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 $_
        }
    }
  • 相关阅读:
    tp5中调用接口api中的数据
    TP5.0如何转换成SQL语句输出构造器里面的方法
    unlink() 函数删除文件。
    tp5 图片上传
    Java第十一章多线程
    Java Character类
    前端技术学习路线及技术汇总
    Web发展史
    HTML
    JavaNumber类&JavaMath类
  • 原文地址:https://www.cnblogs.com/edward2013/p/3536644.html
Copyright © 2011-2022 走看看