SE の雑記

SQL Server の情報をメインに Microsoft 製品の勉強内容を日々投稿

PowerShell でローカルグループのメンバーを操作

leave a comment

PowerShell で Active Directory を操作するときには Active Directory コマンドレット を使用することができますが、ローカルグループやユーザーを操作するときにはコマンドレットが見当たらなかったので、Administrators グループにドメインユーザーを追加する際のサンプルを作ってみました。
以下の情報を参考にしています。
Local Account Management Module 2.1
Hey, Scripting Guy! Windows PowerShell を使用してローカル グループにドメイン ユーザーを追加する方法はありますか
Hey, Scripting Guy! 複数のユーザーを作成し、属性に値を代入して、ユーザーをグループに割り当てる方法はありますか
Retrieve all local user accounts information on remote computers (PowerShell)
Powershell one-liner to add domain user to a local group

以下が作成したサンプルになります。

[CmdletBinding()]
param(
   [Parameter(Mandatory=$true)]
   [ValidateNotNullOrEmpty()]
   [String]$Domain,
   [Parameter(Mandatory=$true)]
   [ValidateNotNullOrEmpty()]
   [String]$Domuser,
   [Parameter(Mandatory=$true)]
   [ValidateNotNullOrEmpty()]
   [String]$DomPwd
)
$lg = [ADSI]"WinNT://localhost/Administrators,group"
$ldapaccount = [ADSI]"WinNT://$Domain/$DomUser"
$ldapaccount.psbase.Username = $DomUser
$ldapaccount.psbase.Password = $DomPwd
$lg.psbase.Invoke("Add", $ldapaccount.psbase.Path)
$laccount = [ADSI]"WinNT://$ENV:COMPUTERNAME/test,user"
$lg.Add($laccount.Path)

 
ドメインユーザーとローカルユーザーをローカルグループに登録するサンプルになります。
現状は ADSI の WinNT プロバイダを使って操作するのが一般的な方法になるのでしょうか。

Share

Written by Masayuki.Ozawa

12月 15th, 2013 at 8:37 pm

Posted in PowerShell

Tagged with

Leave a Reply