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 -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[] -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[] -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 $_
}
}

Install-User.ps1的更多相关文章

  1. 【转】Install Win32 OpenSSH (test release)

    Openssh download url:https://github.com/PowerShell/Win32-OpenSSH/releases Install instruction: Insta ...

  2. Powershell script to install Windows Updates (msu) from folder

    ######################################################### # # Name: InstallWindowsUpdates.ps1 # Auth ...

  3. 利用Nuget打包添加tools下intsall.ps1【powershell脚本】修改.csproj文件

    利用Nuget打包添加tools下intsall.ps1[powershell脚本]修改.csproj文件, 以设置1.项目-生成->输出->选择[XML文件文件] 2.项目->调试 ...

  4. 'Install app for SharePoint': Sideloading of apps is not enabled on this site

    http://blog.lekman.com/2012/11/sharepoint-2013-sideloading-of-apps-is.html Solution: You need to ena ...

  5. vue : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\vue.ps1

    最近因为电脑太卡,小颖把电脑重装了,重装后再执行 npm install -g @vue/cli 时可能是网络问题,一直不能成功,小颖就把npm指向了淘宝镜像: npm install -g cnpm ...

  6. Install chocolatey

    Requirements Windows 7+ / Windows Server 2003+ PowerShell v2+ .NET Framework 4+ (the installation wi ...

  7. 解决npm : 无法加载文件 D:\Code\renren-fast-vue\node_modules\.bin\npm.ps1,因为在......

    解决这个问题: 看看错误信息: npm : 无法加载文件 D:\DevPath\nodejs\npm.ps1,因为在此系统上禁止运行脚本.有关详细信息,请参阅 https:/go.microsoft. ...

  8. OEL上使用yum install oracle-validated 简化主机配置工作

    环境:OEL 5.7 + Oracle 10.2.0.5 RAC 如果你正在用OEL(Oracle Enterprise Linux)系统部署Oracle,那么可以使用yum安装oracle-vali ...

  9. org.jboss.deployment.DeploymentException: Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=egmasDS

    17:34:37,235 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080 17:34:37,281 INFO [ ...

  10. 如何使用yum 下载 一个 package ?如何使用 yum install package 但是保留 rpm 格式的 package ? 或者又 如何通过yum 中已经安装的package 导出它,即yum导出rpm?

    注意 RHEL5 和 RHEL6 的不同 How to use yum to download a package without installing it Solution Verified - ...

随机推荐

  1. Java 声明和访问控制(二) this关键字的访问

    this可以引用本类的静态变量和实例变量,而在静态方法中不能引用实例变量(因为当静态方法加载时,实例变量还没有被定义和初始化) this不可以引用局部变量.例如方法的参数变量,以及在方法中定义的局部变 ...

  2. 【POJ 2152】 Fire (树形DP)

    Fire   Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...

  3. DHTMLX 前端框架 建立你的一个应用程序教程(一)

    介绍 从这里下载官网 示例 此教程包含是多方面的: 1.如何在页面上添加标准的dhtmlx组建 2.如何在页面上组织组件 3.如何添加过滤 4.如何从服务器端获取数据填充组建 5.如何保存用户修改的数 ...

  4. PCR理解

    http://blog.csdn.net/niehanzi/article/details/4450154 PCR的物理意义: PCR存在于TS包的自适应域中,如下图: PCR用来同步前端编码器和后端 ...

  5. 代码演示C#中string和StingBuilder内存中的区别

    关于 string和StringBuilder的区别参考MSDN.本文用程序演示它们在内存中的区别,及其因此其行为不同. //Demo  string memory model namespace C ...

  6. SQL Server 字段状态判断语句

    selct newName=case when  条件 then '否' else '是' end from tableName

  7. Color the ball HDOJ--1556

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 发送带有认证信息的HTTP请求并取回响应

    问题 如何发送 一个带有网络认证证书的HTTP请求并返回相应的HTTP响应. 设计 创建一个WebRequest对象和一个NetWorkCredential对象.把NetWorkCredential对 ...

  9. [GRYZ2014]递增子序列最大和

    [问题描述] 给定长度为n的正整数序列a1,a2,…,an. 求一个递增的子序列,和最大. [输入] 第一行,n,表示给定序列的个数. 第二行,n个用空格隔开的正整数. [输出] 递增子序列的最大和. ...

  10. Linux C 编译错误总结

    1. test.c:59:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by ...