Bitlocker script check and one question

by Dec 12, 2014

Created a script that will check the bitlocker status on each computer that is stored in a text file.

1. It will get the content from the text file containing the computer names
2. Tries to ping the computer
3. Checks if WinRM is enabled
4. Query using "gwmi"
5. Adds the results to a custom object
6. Exports it to a .csv file
7. At the end it removes the computer from the text file so it doesn't check the computer again. 

Are there any improvements that can be made to the script? Cleanups?

Second question : Sometimes it happens that a user isn't logged in, it then returns a error stating : the object cannot be a null…. Is it possible to write a new value to the variable $username such as: User not logged in? If so, how?

#Content path computer accounts

$computers = Get-Content -Path "c:tempcomputers.txt"

#Loop through the computers

Foreach ($computer in $computers) {

     #Ping check

     $connection = Test-Connection -Computername $computer -BufferSize 16 -Count 1 -Quiet

     if ($connection -eq $true) {

                  #WinRM check

                     winrm id -r:$computer 2>$null

                     if ($LastExitCode -eq 0) {


                                 write-host "able to contact $computer" -ForegroundColor green


#Bitlocker Status Check

$status = Invoke-Command -ComputerName $computer -ScriptBlock {manage-bde -cn $Env:COMPUTERNAME -status} | select-string "protection"

$string = $status[0].ToString()

$split = $string.Split(':')

$protectionstatus = $split[1].trim() 

#Country check

$ou = (get-adcomputer $computer -Properties CanonicalName | select-object -expandproperty CanonicalName).Split('/')[1].trim()

#Username Check 

$username = gwmi -ClassName win32_computersystem -comp $computer |select -ExpandProperty Username 

#Model Check

$model = gwmi -ClassName win32_computersystem -comp $computer |select -ExpandProperty model

#OS Check

$Osversion = gwmi -ClassName Win32_OperatingSystem -ComputerName $computer | select-object -ExpandProperty caption

#Creating a custom object

$obj = New-Object PSObject

$obj | Add-Member Date $date

$obj | Add-Member Computername $computer

$obj | Add-Member Username $username

$obj | Add-Member Bitlocker $protectionstatus

$obj | Add-Member OS $Osversion 

$obj | Add-member Model $model

$obj | Add-member Country $OU

#Exporting the results to a csv file

write-host "Adding $computer info to CSV file" -ForegroundColor Yellow

$obj | export-csv C:temptest.csv -append

#deleting computernames from text-file

$Computers = $Computers | Where {$_ -ne $computer}

   $Computers | Out-File "C:tempcomputers.txt" -Force




If ($connection -eq $false) {write-host "Cannot contact $computer, skipping" -ForegroundColor red}