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을하는 경우 버전 5.1 당신은 그들이 새로운 추가 모듈이 필요하지 않습니다 멋쟁이는 기본적으로 당신이 할 :)

참고 정확히 원하는 것을 위해 모듈을 만들어

https://gallery.technet.microsoft.com/PowerShell-Module-to-255637a3 cmdlets를 사용하여이 작업을 수행 할 수 있습니다.

자격 증명에 따라 PowerShell은 Windows 보안을 우회 할 수 없으므로 스크립트가 명령에서 다른 사용자의 자격 증명을 제공하지 않는 한 스크립트를 실행 한 사용자의 권한으로 실행됩니다.

어떻게 지내는지 알려주십시오.

+0

도움 주셔서 감사합니다. :) –