2017-12-21 22 views
-3

Windowsアカウントを無効にするPowerShellスクリプトを作成したい場合、ターゲットホスト名が引数として提供されます。管理者だけがこのタスクを実行できるはずです。ターゲットホスト上でWindowsアカウントを無効にするスクリプトを作成します。管理者のみがこのアクションを実行できます

これは私が試みたものです。このアプローチが正しいかどうか、誰かに教えてもらえますか、これを行うにはより良い方法がありますか?物事の

param([Parameter(Mandatory=$true)] [String] $TargetHost , 
     [Parameter(Mandatory=$true)] [String] $TargetUserName , 
     [String] $User , 
     [String] $Password) 

# Set up a trap to properly exit on terminating exceptions 
trap [Exception] { 
    write-error $("TRAPPED: " + $_) 
    exit 1 
    } 

function DeactivateAccount($TargetHost , $TargetUserName ,$User , $Password){ 

    $TargetHost = $TargetHost #Target Host on which windows account deactivation will be done. 
    $TargetUserName = $TargetUserName #User Name of Target. 
    $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() #Domain name of the localhost. 
    $localHost = [System.Net.Dns]::GetHostName() 
    $localIP = [System.Net.Dns]::GetHostAddresses("$localHost") 

    #if TargetHost and LocalHost are same. 
    if($localHost -like $TargetHost -OR $localIP -like $TargetHost) { 

     if($Domain -eq [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()){ 
      $process = net user $TargetUsername /domain /active:no #Performs the operation on the domain controller in the computer's primary domain. 
     } else { 
      $process = net user $TargetUsername /active:no 
     } 
     Write-host " $TargetUsername account deactivated " 
    } 

    #If TargetHost is remote Host. 
    else { 
     $User = $User #Creds to perform admin function. 
     $Password = $Password 
     $SecurePassword = new-Object System.Security.SecureString #Convert password into secure string. 
     $Password.ToCharArray() | % { $SecurePassword.AppendChar($_) } 
     $Cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist "$User",$securePassword 

     $newSession = New-PSSession -ComputerName "$TargetHost" -credential $Cred #Used PSSession for persistent connection and credentials to Specify a user account that has permission to perform this action. 

     $export_username = Invoke-Command -Session $newSession -ScriptBlock {$username=args[1]} # Invoke-Command command uses the Session parameter(here newSession) to run the commands in same session. 
     if($Domain -eq [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()){ 
       $process = Invoke-Command -Session $newSession -ScriptBlock {net user $username /domain /active:no} 
      } else { 
       $process = Invoke-Command -Session $newSession -ScriptBlock {net user $username /active:no} 
      } 
     Write-host " $TargetUsername account deactivated " 
     Remove-PSSession $newSession # Closes Windows PowerShell sessions. 
     } 

    if(-not $?) { # Returns true if last command was successful. 
     Write-Error "Windows Deactivation Failed!!" 
     exit 1 
    } 
} 

DeactivateAccount($TargetHost , $TargetUserName ,$User , $Password) 

答えて

0

カップル:

あなたのあなたが試みたが、あなたはPowerShellのに新しいしているので、私はそれがローカルのWindowsのそのスライド:)

をもらおう示すために、いくつかのコードを示すためのもの無効にしようとしているアカウントまたはADアカウントこれの目的のために、私は地元を仮定します。

グラブこのモジュール:https://gallery.technet.microsoft.com/PowerShell-Module-to-255637a3

男は基本的にあなたがする:)

注意をしたい正確に何のためのモジュールを作った:あなたはPowerShellを持っている場合は5.1+あなたは、彼らが新たに追加モジュールを必要としませんコマンドレットをネイティブで実行します。

資格のある方PowershellはWindowsのセキュリティをバイパスできません。スクリプトでは、コマンドで別のユーザーの資格情報が指定されていない限り、スクリプトを実行したユーザーの権限で実行されます。

あなたがどのように乗っているか教えてください。

+0

ありがとうございました:) –

関連する問題