martes, 25 de junio de 2013

Script completo para preparar equipo y lanzar Bitlocker.

Hola.

Hago público el script del que os he hablado estos días.

Este Script, está hecho para que encuentre los portátiles como los encuentre, vaya mandando el comando correspondiente e irá preparando el equipo hasta lanzar el comando que cifra la unidad C:. No hace falta decir que se puede llevar al logonscript del dominio o como es mi caso, lanzarlo de forma continuada en los próximos logon vía SCCM.

Cabe destacar, que para mi, es importante que se apliquen los cambios en la gpo del dominio para que se almacene la clave de bitlocker en directorio activo.

Por partes:

#Comprobamos que la partición de boot está creada por debajo del tamaño Default ya que también se suele crear de 100mb.

    if( $Partitionstatus.size -gt 314572800) { 
        VENTANAPARTREINICIO
        bdehdcfg -target default -restart -quiet
    } 

#comprobamos que hay conectividad con alguno de los DCs antes de lanzar el cifrado ya que las directivas nos obligan a guardar la clave de bitlocker (de todas formas, no habiendo conectividad no funcionaría porque la directiva así lo ordena).

Function CONEXIONREDCIFRADODISCO
{
    $Ping1DC = Test-Connection "!DC1" -Count 1 -Quiet
    $Ping2DC = Test-Connection "!DC2" -Count 1 -Quiet
    if (($Ping1DC -eq $true) -or ($Ping2DC -eq $true)) {
        VENTANANOREINICIO
        manage-bde -on c: -skiphardwaretest -recoverypassword
    }

}

#Comprobamos que el equipo forma parte del dominio, que bitlocker no cifra ya el disco, que la partición es menor y que el equipo es un portátil.

If (($compconfig.domain -eq "!dominio.local") -and ($bitlockstatus -ne "1") -and ($Partitionstatus.size -le 314572800) -and ($tipoequipo -eq "2")) {

 

#----------------------------------------------------------------------------------
#Script para preparar equipos portátiles y lanzar bitlocker en unidad c:
#
# Realizado por Miguel Hernández
#
#Para personalizar este script ha de buscar el signo ! .
#----------------------------------------------------------------------------------

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$tpm = Get-WmiObject -class Win32_Tpm -namespace "root\CIMV2\Security\MicrosoftTpm"
$BitLockDrive = get-wmiobject -namespace root\CIMv2\Security\MicrosoftVolumeEncryption -class Win32_EncryptableVolume -filter "driveletter='c:'"
$Partitionstatus= Get-WmiObject Win32_DiskPartition -filter "bootpartition='True'"
$CompConfig = Get-WmiObject -Class Win32_ComputerSystem

##obtenemos el tipo de equipo = 1 {"Desktop"},2 {"Laptop"},3 {"Workstation"},4 {"EntServer"},5 {"SmallOfficeOrHomeServer"},6 {"AppliancePC"},7 {"PerformanceServer"},8 {"Maximum"},default {"NoConocido"}
$tipoequipo = $CompConfig.PCSystemType

#Obtenemos la información de si bitlocker ya cifra la unidad
$bitlockstatus = $BitLockDrive.protectionstatus

#-------------------------------------------------------------------------------
#Funciones
#-------------------------------------------------------------------------------

function VENTANAPARTREINICIO
{

#Ventana Form donde se comunica el reinicio

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "IT Department."
$objForm.Size = New-Object System.Drawing.Size(300,220)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(80,150)
$OKButton.Size = New-Object System.Drawing.Size(120,25)
$OKButton.Text = "Accept"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,10)
$objLabel0.Size = New-Object System.Drawing.Size(280,30)
$objLabel0.Text = "Como parte del proceso de cifrado se va a preparar la unidad de disco de este equipo."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,45)
$objLabel1.Size = New-Object System.Drawing.Size(280,30)
$objLabel1.Text = "Por favor, cierre todo los programas abiertos ya que al finalizar este proceso el ordenador se reiniciará."
$objForm.Controls.Add($objLabel1)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,85)
$objLabel0.Size = New-Object System.Drawing.Size(280,30)
$objLabel0.Text = "As part of Encrypting process it will be prepared the computer's hard drive."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,115)
$objLabel1.Size = New-Object System.Drawing.Size(280,30)
$objLabel1.Text = "Please close all your opened programs because the computer will be restarted."
$objForm.Controls.Add($objLabel1)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

}

function VENTANAREINICIO
{
#Ventana Form donde se comunica el reinicio

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "IT Department."
$objForm.Size = New-Object System.Drawing.Size(300,220)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(80,150)
$OKButton.Size = New-Object System.Drawing.Size(120,25)
$OKButton.Text = "Accept"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,10)
$objLabel0.Size = New-Object System.Drawing.Size(280,30)
$objLabel0.Text = "Se va a proceder a la activación del cifrado de su disco."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,40)
$objLabel1.Size = New-Object System.Drawing.Size(280,30)
$objLabel1.Text = "Tras reiniciar, si su equipo es HP tendrá que pulsar la tecla F1 y si es TOSHIBA la tecla F10."
$objForm.Controls.Add($objLabel1)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,75)
$objLabel0.Size = New-Object System.Drawing.Size(280,20)
$objLabel0.Text = "It will proceed to activate the hard drive encryption."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,95)
$objLabel1.Size = New-Object System.Drawing.Size(280,40)
$objLabel1.Text = "After restarting your computer if it an HP you will need to press the F1 Key however if it is a TOSHIBA press the F10 key."
$objForm.Controls.Add($objLabel1)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

}

function VENTANANOREINICIO
{
#Ventana Form donde se comunica el proceso

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "IT Department."
$objForm.Size = New-Object System.Drawing.Size(300,220)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(80,150)
$OKButton.Size = New-Object System.Drawing.Size(120,25)
$OKButton.Text = "Accept"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,10)
$objLabel0.Size = New-Object System.Drawing.Size(280,20)
$objLabel0.Text = "Se va a lanzar el proceso de cifrado de su disco."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,30)
$objLabel1.Size = New-Object System.Drawing.Size(280,40)
$objLabel1.Text = "Usted podrá trabajar con normalidad, aunque apreciará cambios en el espacio libre de su unidad de disco."
$objForm.Controls.Add($objLabel1)

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,85)
$objLabel0.Size = New-Object System.Drawing.Size(280,20)
$objLabel0.Text = "It will proceed to activate the hard drive encryption."
$objForm.Controls.Add($objLabel0)

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,105)
$objLabel1.Size = New-Object System.Drawing.Size(280,30)
$objLabel1.Text = "You can work normally although during this process the hard drive free space will changes."
$objForm.Controls.Add($objLabel1)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

}

#Prepara el disco para bitlocker
function PREPDISCO 
{

    if( $Partitionstatus.size -gt 314572800) {
        VENTANAPARTREINICIO
        bdehdcfg -target default -restart -quiet
    } 
}

#Comprobamos que estamos en red.
Function CONEXIONREDCIFRADODISCO
{
    $Ping1DC = Test-Connection "!DC1" -Count 1 -Quiet
    $Ping2DC = Test-Connection "!DC2" -Count 1 -Quiet
    if (($Ping1DC -eq $true) -or ($Ping2DC -eq $true)) {
        VENTANANOREINICIO
        manage-bde -on c: -skiphardwaretest -recoverypassword
    }

}

#-------------------------------------------------------------------------------
#Ejecución script general
#-------------------------------------------------------------------------------

PREPDISCO

#Entrará en el if si el disco no está cifrado, la unidad de disco está preparada, es un portátil y está en dominio
If (($compconfig.domain -eq "!dominio.local") -and ($bitlockstatus -ne "1") -and ($Partitionstatus.size -le 314572800) -and ($tipoequipo -eq "2")) {
    if (($tpm.isEnabled_InitialValue -eq $true) -and ($tpm.isActivated_InitialValue -eq $true) -and ($tpm.IsOwned_InitialValue -eq $true)){
        #TPM está listo para habilitar bitlocker    
        CONEXIONREDCIFRADODISCO
    }
    elseif (($tpm.isEnabled_InitialValue -eq $true) -and ($tpm.isActivated_InitialValue -eq $true) -and ($tpm.IsOwned_InitialValue -eq $false)){
        #Se obtendrá propiedad de tpm"
        $valor=1
        manage-bde -tpm -takeownership %date%%random%%random%
        CONEXIONREDCIFRADODISCO
    }
    elseif (($tpm.isEnabled_InitialValue -eq $false) -and ($tpm.isActivated_InitialValue -eq $false) -and ($tpm.IsOwned_InitialValue -eq $false)){
        #TPM Se pondrá en ON y se forzará reinicio
        VENTANAREINICIO
        manage-bde -tpm -t
        shutdown -r -t 60 
    }
    elseif (($tpm.isEnabled_InitialValue -eq $false) -and ($tpm.isActivated_InitialValue -eq $false) -and ($tpm.IsOwned_InitialValue -eq $true)){
        #TPM Se pondrá en ON y se forzará reinicio
        VENTANAREINICIO
        manage-bde -tpm -t
        shutdown -r -t 60
    }
    elseif (($tpm.isEnabled_InitialValue -eq $true) -and ($tpm.isActivated_InitialValue -eq $false) -and ($tpm.IsOwned_InitialValue -eq $false)){
           #TPM está encendido peri no activado, se activará y forzará reinicio
        VENTANAREINICIO
        manage-bde -tpm -t
        shutdown -r -t 60
    }
}

1 comentario:

MxS dijo...

Tus entradas son espectaculares!!. Yo no tengo tanto nivel en powershell y siempre utilizo Autoit para automatización y scripting, lo mismo le echas un vistazo y te gusta.

Saludos!