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并发】线程同步工具Semaphore的使用

    Semaphore通常用于限制可以访问某些资源(物理或逻辑的)的线程数目,我们可以自己设定最大访问量.它有两个很常用的方法是acquire()和release(),分别是获得许可和释放许可.  官方J ...

  2. 转载:遍历Map的四种方法

    http://www.cnblogs.com/kristain/articles/2033566.html 遍历Map的四种方法 public static void main(String[] ar ...

  3. 如何修复在Microsoft Azure中“虚拟机防火墙打开,关闭RDP的连接端口”问题

     注:下列步骤并不一定适用所有场景,提供思路,请灵活应用 我们在使用Microsoft Azure 中Windows 虚拟机,有时会发生错误打开防火墙或一些管家软件错误的关闭了"远程桌面 ...

  4. (转载)php循环检测目录是否存在并创建(循环创建目录)

    (转载)http://www.jb51.net/article/25917.htm php循环检测目录是否存在并创建,需要的朋友可以参考下. 循环创建目录方法 这个会生成image.gif目录 代码如 ...

  5. List<object>排序 z

    一般我們在撰寫程式時,很常會使用到List<>來裝取自定義的類別陣列,跟一般的陣列插在哪呢?!好處是什麼?!好處就是一般的陣列需要先 宣告長度,而List<>不用,所以在資料個 ...

  6. Windows Phone8开发工具包简述(转载)

    Windows Phone 软件开发包 (SDK) 8.0 可为您提供开发 Windows Phone 8 和 Windows Phone 7.5 应用和游戏所需的工具. 概述Windows Phon ...

  7. vijosP1038 添加括号

    vijosP1038 添加括号 链接:https://vijos.org/p/1038 [思路] 区间DP. 本题的关键在于如何输出解.对于求和表达式而言可以用一个p[][]记录决策然后递归输出,对于 ...

  8. Keepass 2.x 的一些新发现

    近期将 Keepass 从 1.22 升级到了 2.24,经过一番折腾,发现有了很多新功能,也有一些之前被忽视的地方.再一次感叹这个软件的强大,向作者的无私奉献致敬! 其实,这个软件一直有 1.x 和 ...

  9. JavaScript高级程序设计43.pdf

    事件类型 Web浏览器中有很多事件类型,“DOM3级事件”规定了以下几类事件 UI事件(用户界面),当用户与页面上的元素交互时触发: 焦点事件,当元素获得或失去焦点时触发 鼠标事件,当用户通过鼠标在页 ...

  10. Axure原型用pmdaniu在线托管尝试

    这次把原型中语音模块的坑填了一部分,实现了拖拽按钮控制的界面效果 http://www.pmdaniu.com/prototype/view?id=WXpVNwNhUmYMPFN3AkA