Try {Import-Module NetIQ.DRA.PowerShellExtensions -ErrorAction SilentlyContinue} Catch {Write-Host "Module loaded with errors system may need to be rebooted before module can be used"}
Try {Import-Module importexcel -ErrorAction SilentlyContinue} Catch {Write-Host "Module loaded with Errors"}



Function Show-ProgressBar
    {
        param(
                [string]$Activity,
                [int]$PercentComplete
             )   
        Write-Progress -Activity $Activity -PercentComplete $PercentComplete
    }
<# Disable DRA Computer #>
Function Disable-Computer ($ComputerName,$DRAServer,$Disabled)
    {
        $ComputerName = $ComputerName.split(",")
        $count = $ComputerName.count
        $i = 1        
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        if ($Disabled -eq "") {$Disabled = $False}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        $Recycle = ",OU=NetIQRecycleBin,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"  
        <# Disable Computer in DRA #>
           
        ForEach ($Item in $ComputerName)
            { 
                 $Percent = ($i/$count) * 100
                 Show-ProgressBar -Activity "Disabling $Item" -PercentComplete $Percent
                 Try
                    {
                        $ID = $CN+$Item+$OU
                               Set-DRAComputer -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID
                        -IsDisabled $Disabled `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Could Not Disable $Item"
                    }
                $i++
            }
   
    }
<# Enable DRA Computer #>

Function Enable-Computer ($ComputerName,$DRAServer)
    {
        $ComputerName = $ComputerName.split(",")        
        $count = $ComputerName.count
        $i = 1
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        $Recycle = ",OU=NetIQRecycleBin,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"  
        ForEach ($Item in $ComputerName)
            {
                $Percent = ($i/$count) * 100
                Show-ProgressBar -Activity "Enabling $Item" -PercentComplete $Percent
                $ID = $CN+$Item+$OU   
                 Try
                    {
                               Enable-DRAComputer -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Could not enable $Item"
                    }
                $i++
            }
    }
 
#Disable DRA User
Function Disable-User ($UserName,$DRAServer)
    {
        $UserName = $UserName.split(",")        
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Users,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        $Count = $UserName.count
        $i = 1
               
        <# Disable User in DRA #>
        ForEach ($User in $UserName)
            {
                $Percent = ($i/$count) * 100
                Show-ProgressBar -Activity "Disabling $User" -PercentComplete $Percent
                ID = $CN+$User+$OU   
                Try
                    {
                            Disable-DRAUser -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Could not disable $User"
                    }
            }
   
    }
 
#Enable DRA User
 
Function Enable-User ($UserName,$DRAServer)
    {
        $UserName = $UserName.split(",")        
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Users,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        <# Enable USER in DRA #>
        $count = $UserName.count
        $i = 1
        ForEach($User in $UserName)
            {
                $Percent = ($i/$count) * 100
                $ID = $CN+$User+$OU 
                Show-ProgressBar -Activity "Enabling $User" -PercentComplete $Percent  
                 Try
                    {
                               Enable-DRAUser -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Could not enable $User"
                    }
                $i++
            }
   
    }

<# Unlock DRA User #>


Function Unlock-User ($User,$Server)
    {
        $Users = $User.split(",")
        $Count = $Users.count
        $i = 1
        ForEach ($User in $Users)
            {
                $Percent = ($i/$count) * 100
                Show-ProgressBar -Activity "Unlocking $User" -PercentComplete $Percent
                $test = get-ADUser $User -Properties LockedOut
                $test = $test.LockedOut
                $U = Get-ADUser $User
                $User = $U.Name
        
                If ($Server -eq $null -or $Server -eq "") {$Server = "lackland.dra.us.af.mil"} #Default DRA Server
                If ($test -eq "True")
                    {
                        Unlock-DRAUser -DRARestServer $Server -DRARestPort 8755 -DRAHostServer $Server -DRAHostPort 11192 -Domain "AREA52.afnoapps.usaf.mil" -Identifier $User
                        Start-Pause "$User has been unlocked"
                    }
                Else
                    {
                        Start-Pause "$User is not locked out"
                    }
                $i++
            }
    }
#Get-DRAUser
Function Get-User ($UserName,$DRAServer)
    {
        $UserName = $UserName.Split
        $Count = $UserName.count
        $i = 1   
        
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Users,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        <# Enable USER in DRA #>
        ForEach ($User in $UserName)
            {
                $Percent = ($i/$count) * 100
                Show-ProgressBar -Activity "Getting info for $User" -PercentComplete $Percent
                $ID = $CN+$User+$OU   
                 Try
                    {
                               Get-DRAUser -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Can't find $user in Active Directory"
                    }
                $i++
            }
   
    }

#Get-DRAComputer

Function Get-Computer ($ComputerName,$DRAServer)
    {
        $ComputerName = $ComputerName.split(",")
        $count = $ComputerName.count
        $i = 1
               
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        
        <# Get Info about a computer in DRA #>
        ForEach ($Computer in $ComputerName)
            {
                $ID = $CN+$Computer+$OU   
                 $Percent = ($i/$count) * 100
                 Show-ProgressBar -Activity "Showing $Computer" -PercentComplete $Percent
                 Try
                    {
                               Get-DRAComputer -ErrorAction STOP `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    }
                Catch
                    {
                        Write-Host "Can't find $Computer"
                    }
                $i++
            }
   
    }
 
#Remove DRA Computer

Function Remove-Computer ($ComputerName,$DRAServer,[switch]$Recyclebin) 
    {
           
        If ($Recycle) {Write-Host "Recycle parameter was used"}
           
        $ComputerName = $ComputerName.split(",")
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        $Recycle = ",OU=NetIQRecycleBin,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"  
        $count = $ComputerName.count
        $i = 1
    
        ForEach ($Item in $ComputerName)
            {
                $Percentage = ($i / $count) * 100
                Show-ProgressBar -Activity "Deleting computers" -PercentComplete $Percentage
                <# Delete Computer out of DRA #>
                $ID = $CN+$Item+$OU   
                 Try {
                            Remove-DRAComputer -ErrorAction Stop `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DRAServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DRAServer `
                        –DRAHostPort 11192 `
                        -Identifier $ID `
                        -Force;
                    
                
   
                        <# Delete Computer from Recycle Bin #>
                        If (!$Recycle)
                            {
                                $ID = $CN + $Item + $Recycle
                                    Remove-DRAComputer `
                                -Domain $env:USERDNSDOMAIN `
                                –DRARestServer $DRAServer `
                                -DRARestPort 8755 `
                                –DRAHostServer $DRAServer `
                                –DRAHostPort 11192 `
                                -Identifier $ID `
                                -Force;
                            }
                        }
                   Catch
                    {
                        Write-Host "Comptuer $Item does not exists!"
                    }
                $i++
            }
 
    }
      
Function Reset-Computer
    {
        Param(
                [String]$DRAServer,
                [String]$ComputerName
             )
       
        
        If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
        $CN = "cn="
        $OU = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        
        <# Reset the Computer in DRA #>
        $ID = $CN+$ComputerName+$OU   
            Reset-DRAComputer `
        -Domain $env:USERDNSDOMAIN `
        –DRARestServer $DRAServer `
        -DRARestPort 8755 `
        –DRAHostServer $DRAServer `
        –DRAHostPort 11192 `
        -Identifier $ID `
        -Force;
   
    }

Function New-Computer($ComputerName,$Organization,$BuildingNum,$RoomNum,$Location,$Description,$SecurityGroup,$DRAServer)
        {
            $ComputerName = $ComputerName.Split(",")
            $count = $ComputerName.count
            $i = 1
            If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
            If ($SecurityGroup -eq "" -or $SecurityGroup -eq $null) {$SecurityGroup = "514 FSS SAShare Admins"}
			#Define variables to be used
            If ($Location -eq "" -or $Location -eq $null) {$Location = "BLDG:$BuildingNum;RM:$RoomNum"}
            $Account   = "CN=$SecurityGroup,OU=McGuire AFB Security Groups,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
            $CN        = "cn="
            $CSV       = $File
            #$DRAServer = $Server
            $OU        = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
            ForEach ($Computer in $ComputerName)
                {
                    $Percent = ($i/$count) * 100
                    Show-ProgressBar -Activity "Creating $Computer" -PercentComplete $Percent
                    Try
                        {
                            Add-DRAComputer -Domain $env:USERDNSDOMAIN –DRARestServer $DRAServer -DRARestPort 8755 –DRAHostServer $DRAServer –DRAHostPort 11192 -Properties @{DistinguishedName=$CN+$Computer+$OU;Location=$Location;Description=$Description;AccountDisabled=$false;AccountThatCanAddComputerToDomain=$Account;o=$Organization;l=”McGuire AFB”} -ErrorAction STOP
                        }
                    Catch
                        {
                            Write-Host "Couldn't create $computer"
                        }
                }
        }
Function New-ComputerFromList ($File,$DRAServer)
    {
        $ComputerList = Import-Excel $File
        ForEach ($Computer in $ComputerList)
            {
                If ($DRAServer -eq "" -or $DRAServer -eq $null) {$DRAServer = "lackland.dra.us.af.mil"}
                $CompName = $Computer.ComputerName
                $org = $computer.org
                $Loc = $Computer.Loc
                $Desc = $Computer.Desc
                $Sec = $Computer.SecurityGroup
                New-Computer -ComputerName "$CompName" -Organization "$org" -Location "$Loc" -Description "$Desc" -SecurityGroup "$Sec" -DRAServer $DRAServer
            }
    }
Function Remove-ComputerFromList ($File,$DRAServer)
    {
        $ComputerList = Import-Excel $File
        ForEach ($Computer in $ComputerList)
            {
                If ($DRAServer -eq "" -or $DRAServer -eq $null) 
                    {
                        $DRAServer = "lackland.dra.us.af.mil"
                    }
                
                $CompName = $Computer.'Computer Name'
                
                Delete-Computer -ComputerName "$CompName" 
            }
    }
Function Get-SDCVersion ($ComputerName)
    {
        Try {
                Invoke-Command -ComputerName $ComputerName -ErrorAction STOP
                    {
                        $test = Get-ComputerInfo
                        $test = $test.WindowsVersion
                        Return $test
                    }
            }
        Catch
            {
                Write-Host "Unable to communicate with $ComputerName"
            }
        
    }