The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings

Snippet Library

blackbird_and_cache_cleaning

Type:
Full Script
Category:
Other
License:
GNU General Public License
Language:
Other Language

Description:
fix some blackbird network issues
clean web browsers cache
clean eduroam wifi profile

Versions Of This Snippet:

Snippet ID Download Version Date Posted Author Delete
151.12020-06-11 12:49jean-francois MONI Delete
Changes since last version:
modif get-location
clean_browser (toujours pas efficient)
1412020-05-28 12:38jean-francois MONI Delete

Download a raw-text version of this code by clicking on “Download Version”


Latest Snippet Version: 1.1

<#
.SYNOPSIS
    Fix network issues on Windows with BLACKBIRD

.DESCRIPTION
    Download BLACKBIRD
    Unzip BLACKBIRD
    Change path into unzipped folder and launch BLACKBIRD
    Send an email


.NOTES
    NAME:  blackbird.ps1
    AUTHOR:  Jean-Francois MONI - IINS
    CONTACT:  jean-francois.moni@u-bordeaux.fr
    DATE:  27/03/2020
    KEYWORDS:  installations soft, network issue, blackbird
  

    VERSION HISTORY:

    1.0     2020.27.03
    1.1     2020.31.03
    1.2     2020.09.04
    1.3     2020.14.04
    1.4     2020.15.04
    2.1     2020.22.04
    2.2     2020.24.04
    2.3     2020.26.05

    31/03 : test execution avec credentials admin
    06/04 : fonction test_user
    09/04 : fonctions clean_browser et wifi_clean
    14/04 : nettoyage script / classement variables, ajout fonction test réseau (PING)
    15/04 : corrections/modif, tests 
    22/04 : modification fonction browser
    23/04 : modification fonction wifi et test_user  
    24/04 : commentaires des fonctions et variables, ajout fonction DNS, fonction disk space, fonction montage partage
    27/04 : fonction delete_folder, reorganisation des fonction dans MAIN  
    26/05 : fonction test_OS, fonction blackbird modifiée, fonction clean_browser modifiée   
            

.LINK
    https://getblackbird.net/

#>
###############
###VARIABLES###
###############

##LOG+environment deploy
$date = get-date -DisplayHint date
$tag=echo "################### BLACKBIRD REPORT $date ###################
################### HOSTNAME : $env:computername  ###################"
$dirtmp=Get-Location
$logfile="$dirtmp\log.txt"
$share_folder="\\FILER1-IINS\IINS_4ALL\LOGS"

##TEST OS
$osname = (Get-WmiObject Win32_OperatingSystem).name
$osarchitecture = (Get-WmiObject Win32_OperatingSystem).OsArchitecture

##NETWORK
$network = Get-WmiObject  -Class "Win32_NetworkAdapterConfiguration"  | Where-Object {$_.IpEnabled -Match "True"} | Select-Object DNSHostName,DHCPEnabled,IPAddress,DNSServerSearchOrder,MACAddress,DNSDomain
$wifi=(netsh.exe wlan show profiles) -match "eduroam"

##BLACKBIRD
$bbdest32 = "$dirtmp\32bit"
$bbdest64 = "$dirtmp\32bit"
$bblaunch32 = "$bbdest32\blackbird.exe"
$bblaunch64 = "$bbdest64\blackbird.exe"
$arguments = "-l"
$zipfile = ($env:COMPUTERNAME+".zip")

#SEND_MAIL
$local = $env:Username
$hote = $Env:COMPUTERNAME
$domain = $Env:UserDnsDomain
$expe = "blackbird@u-bordeaux.fr"
$encodingMail = [System.Text.Encoding]::UTF8
$to = "jean-francois.moni@u-bordeaux.fr"
$from  = "blackbird@u-bordeaux.fr"
$subject = "execution_blackbird"
$smtpServer = "smtp.u-bordeaux.fr"
$body = "Bonjour,<br> blackbird -l a été lancé sur $hote.$domain <br>"

##LOG : fonction créée pour récuperer les logs
Function log ($var)
{
    ADD-content -Path $logfile -Value `n
    ADD-content -Path $logfile -Value $var
}
##FONCTION DE test de l'OS : si non Windows, fin du script
#SI windows présentation script (necessité droits admin : sinon EXIT)
function osversion ()
{
$os = $osname.split("|") | select-object -first 1
if ($os -like '*Win*')
   {
    Write-Host "your system is a $os $osarchitecture" -ForegroundColor Green
    Write-Host "This script allows you to clean up your wifi network profiles, your web browser credentials 
        and fixes a few network issues by using the tool BLACKBIRD.
        You'll need some Administrator rights to execute part of this script. 
        please contact your network administrators"
    Read-Host "Press enter to continue and CTRL-C to exit ..." 
    }
else 
    {
    Write-Host "your need to run this script on a Windows OS.
                The script will EXIT in 5 seconds" -ForegroundColor Red
    Start-Sleep -Seconds 5
exit
    }
}
##FONCTION FREE_SPACE
#Test l'espace libre sur le disque dur : si moins de 1 MO, arrêt du script
#si plus de 1MO : creation d'un repertoire TMP pour les 
function env_deploy ()
{
$diskspace = get-wmiobject win32_logicaldisk |foreach-object {[math]::truncate($_.freespace / 1GB)}  
if ($diskspace -le 1)
    {
    Write-Host "Not enough free space on your hard drive
                make some room and relaunch the script.
                The script will end in 10 seconds" -ForegroundColor Red
    Start-Sleep -Seconds 10
    }
else
    {
    New-Item -ItemType Directory -Force -Path $dirtmp
    ADD-content -Path $logfile -Value `n
    ADD-content -Path $logfile -Value $var
    $tag | Out-File -FilePath $logfile
    log("TMP folder exists")
    Write-Host "Enough free space on your hard drive
                to create a TMP folder             
                The script will start in 5 seconds" -ForegroundColor Green
    Start-Sleep -Seconds 5                
    }
}
#test connexion Internet (ping ip, ping URL)
function network_test ()  
{
   try {
        Test-Connection  -computername 8.8.8.8 -Count 1 -ErrorAction stop
        log("PING 8.8.8.8 OK")
          }
   catch [System.Net.NetworkInformation.PingException]
          {     
       log("No internet connexion, script exited")        
          }
}
if (network_test) {
    Write-Host "internet connection ok, performing dns_test" -ForegroundColor Green
    $test_dns = Test-Connection  -computername www.google.com -Count 1 -ErrorAction stop 
    log("Test DNS OK")  
}           
else {
    Write-Host "no internet connection :
          please connect to the internet.
          script will exit in 5 seconds" -ForegroundColor Red
    Start-Sleep -Seconds 5
    }
if ($test_dns) {
    echo "test dns OK" -ForegroundColor Green
    }
else {
    Write-Host "no DNS available :
          please contact your network administrator" -ForegroundColor Red
    log("no DNS available")
}
#BLACKBIRD : outil permettant de resoudre les problèmes réseaux (skype, partages, VPN)
function blackbird ()
{
    if ($osarchitecture -like '*64*')
    {
        cd $bbdest64
        Start-Process -FilePath $bblaunch64 -ArgumentList $arguments 
        log("BLACKBIRD 64bit successfully executed")
        Write-Host "BLACKBIRD is running
          the script will continue in 30sec" -ForegroundColor Green
    Start-Sleep -Seconds 30
    }
    elseif ($osarchitecture -like '*32*')
    {
        cd $bbdest32
        Start-Process -FilePath $bblaunch32 -ArgumentList $arguments 
        log("BLACKBIRD 32bit successfully executed")
    }
    else 
    {
        log("impossible to execute BLACKBIRD")
    }
}
#clean_browser_credentials  / RESTE A FAIRE : envoyer la liste dans un tableau et faire un FOR pour chaque entrée du tableau
Function clean_browser ()
{
#recuperer les noms des browsers et les envoyer dans un fichier texte
Write-host "Are you Sure You Want To delete your browser cache and credentials: (y/n) (Default is No)" -ForegroundColor Red 
    $Readhost = Read-Host " ( y / n ) " 
    Switch ($ReadHost) 
     { 
       Y {Write-host "delete my browser cache and credentials"; $delete=$true} 
       N {Write-Host "No, keep my browser cache and credentials"; $delete=$false} 
       Default {Write-Host "Default, keep my browser cache and credentials"; $delete=$false} 
     } 
if ($delete -eq $false)
{
    wifi_clean
}
else
{
[String[]]$liste = @()
$browser_list = Get-ItemProperty HKLM:\Software\Clients\StartMenuInternet\* | Select-Object -ExpandProperty '(default)'
#lister les browser
$liste = $browser_list
$num = 0
$liste |foreach {
    $line=$_
    $num++
	Write-Host « [$num]- $line »
                }
#recuperer choix user
$browser = Read-Host « Choose your Browser : »
$liste = Switch ($browser)
{
   $num {$browser = $line}
   default {'exit without clean any browser'}
}
    Write-Host « Your browser is $browser ! »
    log("your browser is $browser")
if ($browser -like '*Mozilla*')
   {
    Remove-Item -path "$dirtmp\test1.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    ADD-content -Path $logfile -Value "All your Firefox credentials cleared"
    }
elseif ($browser -like '*Google*')
    {
    Remove-Item -path "$dirtmp\test2.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    ADD-content -Path $logfile -Value "All your google chrome credentials cleared"
    }
elseif ($browser -like '*Internet*')
    {
    Remove-Item -path "$dirtmp\test3.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    ADD-content -Path $logfile -Value "All your internet explorer credentials cleared"
    }
elseif ($browser -like '*Vivaldi*')
    {
    Remove-Item -path "$dirtmp\test4.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    ADD-content -Path $logfile -Value "All your vivaldi credentials cleared"
    }
else
{
    Write-Host "no browser selected"
}
}    
}
#wifi_clean_profile
Function wifi_clean ()
{
$wififile="$dirtmp\wifi.txt"
$wifi_list=(netsh.exe wlan show profiles) -match 'mom|Cent' -replace ('[a-zA-Z]+\w\:*\s') |Out-File -FilePath $wififile
#compter nombre de ligne : il faudrait conditionner la suite à ce nombre de ligne mais pas trouvé. pour l'instant, 3 lignes pour 3 browser "classiques"
$nbre_ligne_wifi = (get-content $wififile | measure-object -line).lines

#recuperer la valeur de chaque ligne
$wifiligne1 = Get-Content -Path $wififile | where { $_ -ne "$null" } | Select-Object -Index 0
$wifiligne2 = Get-Content -Path $wififile | where { $_ -ne "$null" } | Select-Object -Index 1

#lecture de la réponse de l'utilisateur
Write-Host « [1]- $wifiligne1 »
Write-Host « [2]- $wifiligne2 »
$browser = Read-Host « Choose your Network : 1 - 2 »
Switch ($wifi_network)
{
   1 {$wifi_network = $wifiligne1}
   2 {$wifi_network = $wifiligne2}
   default {$wifi_network=$wifiligne1}
}

   if ($wifi_network -eq $wifiligne1 ) { 
    Remove-Item -path "$home\Downloads\test1.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    #netsh wlan delete profile "Centre Broca Nouvelle Aquitaine"
    ADD-content -Path $logfile -Value "Centre Broca Nouvelle Aquitaine profile deleted"
    }
    elseif ($wifi_network -eq $wifiligne2 ) {
    Remove-Item -path "$home\Downloads\test1.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
    #netsh wlan delete profile "Eduroam"
    ADD-content -Path $logfile -Value "EDUROAM profile deleted"
    }
    else
    {
    Write-Host "No  profile"
    ADD-content -Path $logfile -Value "No wifi profile deleted"
    }
}

#ENVOI de mail avec les logs
Function send_email()
{     
    try {       
        Send-MailMessage -to $to -From $from -Subject $subject -SmtpServer $smtpServer -BodyAsHtml $body -Encoding $encodingMail -Attachments $logfile
        }
    catch{
        set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
        cd $dirtmp
        sz a -tzip $zipfile log.txt
        New-PSDrive temp -Root $share_folder -PSProvider FileSystem
        Copy-Item "$dirtmp\$zipfile" -Destination $share_folder
        Remove-PSDrive temp
        Write-Host "impossible to send an email : logfile copy into iins_4all/LOGS folder"
        log("impossible to send an email : logfile copy into iins_4all/LOGS folder")
        }
}
Function delete_tmp ()
{
$delete_file = @($dirtmp)
Remove-Item $dirtmp -Include *.txt -Exclude *.ps1 -Recurse -Force
}
Function main ()
{
    osversion
    env_deploy
    network_test
    blackbird
    clean_browser
   # wifi_clean
    send_email
    delete_tmp
}
main

		

Add a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others.