Tuesday, December 26, 2017

 

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



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





[Explanation]


#Domain별 DHCP 조회 
#$companycode은 상위 function에서 수집
$wshell = New-Object -ComObject Wscript.Shell
$dhcpaccount=$null

if($companycode -like '*A*'){
            $dhcpserver1="server ip or host name"
            $dhcpserver2="server ip or host name"
            $dhcpaccount=domainA\administrator
}elseif ($companycode -like '*B*'){
            $dhcpserver1="server ip or host name"
            $dhcpserver2="server ip or host name"
            $dhcpaccount=domainB\administrator
}elseif ($companycode -like '*C*'){
            $dhcpserver1="server ip or host name"
            $dhcpserver2="server ip or host name"
            $dhcpaccount=domainC\administrator
}elseif ($companycode -like '*D*'){
            $dhcpserver1="server ip or host name"
            $dhcpserver2="server ip or host name"
            $dhcpaccount=domainD\administrator
}elseif ($companycode -like '*E*'){
            $dhcpserver1="server ip or host name"
            $dhcpserver2="server ip or host name"
            $dhcpaccount=domainE\administrator
}    
       
#DHCP 조회 확인Function ConfirmScope{
    Write-Host "DHCP 조회 서비스입니다.`n정상적인 조회를 위해 Scope의 대표 IP를 알고 계셔야 합니다.`n예시>192.168.8.0/24 경우에 대표 IP는 192.168.8.0입니다."
    $inputip=(Read-Host "Scope의 Primary IP (192.168.8.0/24 경우, 192.168.8.0)를 입력해주세요.").trim()   #10.199.194.128
    $resultconf=$wshell.Popup("조회하실 IP정보는 $inputip 이며,`nDHCP서버는 $dhcpserver1 와 $dhcpserver2 입니다.")            
                    
#DHCP 정보 조회 스크립트            
            $result=Invoke-Command -ComputerName $dhcpserver1,$dhcpserver2 -ScriptBlock {param($inputip, $dhcpaccount) ; Get-DhcpServerv4Scope –ScopeID $inputip  |select ScopeId,SubnetMask,StartRange,EndRange,LeaseDuration,Name ;  Get-DhcpServerv4ScopeStatistics -ScopeId $inputip | select ScopeId, SuperscopeName, PercentageInUse, Free, InUse,Reserved,Pending } -credential $dhcpaccount  -ArgumentList $inputip,$dhcpaccount
            $result1=$result | ?{$_.PSComputerName -like $dhcpserver1} 
            $result2=$result | ?{$_.PSComputerName -like $dhcpserver2}             
            $dhcpserver1_result1=$result1[0]            
            $dhcpserver1_result2=$result1[1]
            $dhcpserver2_result1=$result2[0]
            $dhcpserver2_result2=$result2[1]                                 
                          
#DHCP서버 1의 조회 결과
            $ScopeId = $dhcpserver1_result1.ScopeId
            $SubnetMask = $dhcpserver1_result1.SubnetMask
            $StartRange1 = $dhcpserver1_result1.StartRange
            $EndRange1= $dhcpserver1_result1.EndRange
            $LeaseDuration1 = $dhcpserver1_result1.LeaseDuration
            $Name1 = $dhcpserver1_result1.Name
            $SuperscopeName1=$dhcpserver1_result2.SuperscopeName
            $PercentageInUse1=$dhcpserver1_result2.PercentageInUse
            $Free1=$dhcpserver1_result2.Free
            $InUse1=$dhcpserver1_result2.InUse
            $Reserved1=$dhcpserver1_result2.Reserved
            $Pending1=$dhcpserver1_result2.Pending

#DHCP서버 2의 조회 결과
            $SubnetMask2 = $dhcpserver2_result1.SubnetMask
            $StartRange2= $dhcpserver2_result1.StartRange
            $EndRange2= $dhcpserver2_result1.EndRange
            $LeaseDuration2 = $dhcpserver2_result1.LeaseDuration
            $Name2 = $dhcpserver2_result1.Name
            $SuperscopeName2=$dhcpserver2_result2.SuperscopeName
            $PercentageInUse2=$dhcpserver2_result2.PercentageInUse
            $Free2=$dhcpserver2_result2.Free
            $InUse2=$dhcpserver2_result2.InUse
            $Reserved2=$dhcpserver2_result2.Reserved
            $Pending2=$dhcpserver2_result2.Pending

#할당된 DHCP 정보 조회 
            if($ScopeId -ne $null -or $StartRange1 -ne $null -or $StartRange2 -ne $null){
                 $wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
                 $value=$wshell.Popup("$ScopeId 에 대한 DHCP설정 정보를 알려드립니다.`n`n$dhcpserver1 서버에서는`n$name1 이름으로 등록됨`n1)Superscope명:$SuperscopeName1 `n2)범위:$StartRange1 ~ $EndRange1 `n3)할당시간:$LeaseDuration1, 할당율:$PercentageInUse1 %`n4)현재사용대역수 : $InUse1 /할당가능대역수 : $Free1 `n`n$dhcpserver2 서버에서는`n$name1 이름으로 등록됨`n1)Superscope명:$SuperscopeName2 `n2)범위:$StartRange2 ~ $EndRange2 `n3)할당시간:$LeaseDuration2, 할당율:$PercentageInUse2 %`n4)현재사용대역수 : $InUse2 / 할당가능대역수 : $Free2 `n`n`nDo you want to clean up the garbaged IPs? `n(OK-Cleanup,Cancel-Close)",0,"DHCP Garbaged IP Clean up",1+64)
               
#Garbaged IP 삭제(Confirm :1 ,cancel : 2)
                if($value -eq 1){
                    Invoke-Command -ComputerName $dhcpserver1,$dhcpserver2 -ScriptBlock {param($inputip,$dhcpserver1,$dhcpserver2) ; Get-DhcpServerv4Scope | Get-DhcpServerv4Lease -ComputerName $dhcpserver1 -BadLeases | Remove-DhcpServerv4Lease -ComputerName $dhcpserver1 -BadLeases ; Get-DhcpServerv4Scope | Get-DhcpServerv4Lease -ComputerName $dhcpserver2 -BadLeases | Remove-DhcpServerv4Lease -ComputerName $dhcpserver2 -BadLeases ; Get-DhcpServerv4Lease -ComputerName $dhcpserver1 -ScopeId $inputip |Sort-Object LeaseExpiryTime | select -First 5 | %{ Remove-DhcpServerv4Lease -ScopeId $inputip -ClientId $_.ClientId };Get-DhcpServerv4Lease -ComputerName $dhcpserver2 -ScopeId $inputip |Sort-Object LeaseExpiryTime | select -First 5 | %{ Remove-DhcpServerv4Lease -ScopeId $inputip -ClientId $_.ClientId }} -credential hiplaza\ad_iyamus  -ArgumentList $inputip
                  }       
              }else{
              $wshell.Popup("요청하신 대역에 대한 정보가 없습니다. 확인하시고 다시 시도하시기바랍니다.",0,"검색오류",0)
              }        
              
#초기화
            $ScopeId = $null
            $SubnetMask =$null
            $StartRange1 =$null
            $EndRange1=$null
            $LeaseDuration1 =$null
            $Name1 =$null
            $SuperscopeName1=$null
            $PercentageInUse1=$null
            $Free1=$null
            $InUse1=$null
            $Reserved1=$null
            $Pending1=$null
            $SubnetMask2 = $null
            $StartRange2= $null
            $EndRange2=$null
            $LeaseDuration2 = $null
            $Name2 = $null
            $SuperscopeName2=$null
            $PercentageInUse2=$null
            $Free2=$null
            $InUse2=$null
            $Reserved2=$null
            $Pending2=$null  
     }
     
#조회 결과 없을 경우Function ErrorInput{
     $wshell.Popup("입력하신 정보가 정확하지 않습니다. 확인 후 다시 서비스를 이용해주시기 바랍니다. ",0,"Done")
     }




♔♔♔♔♔♔♔♔♔♔

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

Your Comments are Always Welcomed!
Share:

0 comments:

Post a Comment