Monday, December 18, 2017

 

[ActiveDirectory] 알쓸신잡 Powershell 스크립트 모음3



알쓸신잡 Powershell 스크립트 모음3







[Explanation]

숫자조합으로 된 컴퓨터 이름을 7개의 그룹으로 분류하여 각 그룹을 Active Directory의 Security group에 grouping을 수행하고 그 결과값을 Logging 하는 스크립트.
$today = get-date
$year = $today.year
$month = $today.month
$day = $today.day
$logdate = [string]$year+"-"+[string]$month+"-"+[string]$day

$TargetOU = "OU=TestOU,DC=Domain,DC=com"
$ComputerList = get-adcomputer -SearchBase $TargetOU -Filter * -searchScope Onelevel 

Foreach ($Computer in $ComputerList){
  if($Computer.memberof -match "CN=GRP0*") 
  { 
  
  }Else {
    #컴퓨터 이름(문자열) 중 숫자부분을 추출하여 int로 변환
    $vmnum=$Computer.Name.Substring(7,4) -as[int] 
    #숫자부분의 나머지 값으로 7그룹으로 분류
    $vmnum=$vmnum%7 
    $fullname=$Computer.DistinguishedName    
    
    switch($vmnum)
    {
"0"

add-adgroupmember -Identity GRP00 -member $fullname ;
$Computer.name + " ; " + "GRP00" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}

"1"

add-adgroupmember -Identity GRP01 -member $fullname ;
$Computer.name + " ; " + "GRP01" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}

"2"

add-adgroupmember -Identity GRP02 -member $fullname ;
$Computer.name + " ; " + "GRP02" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}

"3"

add-adgroupmember -Identity GRP03 -member $fullname ;
$Computer.name + " ; " + "GRP03" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}

"4"

add-adgroupmember -Identity GRP04 -member $fullname ;
$Computer.name + " ; " + "GRP04" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}

"5"

add-adgroupmember -Identity GRP05 -member $fullname ;
$Computer.name + " ; " + "GRP05" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}
"6"

add-adgroupmember -Identity GRP06 -member $fullname ;
$Computer.name + " ; " + "GRP06" | Out-File -Append -Filepath "D:\ResultLog.txt" ;
}
     }  
    $vmnum = $NULL
  }
}




댓글이나 의견은 언제든지 환영합니다.

Your Comments are Always Welcomed!
Share:

0 comments:

Post a Comment