#
.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
$date = get-date -DisplayHint date
$tag=echo "################### BLACKBIRD REPORT $date ###################
################### HOSTNAME : $env:computername ###################"
$dirtmp="$home\Downloads\TMP\"
$logfile="$dirtmp\log.txt"
##TEST OS
$osname = (Get-WmiObject Win32_OperatingSystem).name
$osarchitecture = (Get-WmiObject Win32_OperatingSystem).OsArchitecture
##7ZIP
#$7zf = "$home\Downloads\Blackbird_v1.0.79.3_x64.zip"
#$7zo = "-aoa"
#$Command = "blackbird.exe -l"
$7zdest32 = "$home\Downloads\blackbirdscript\32bit"
$7zdest64 = "$home\Downloads\blackbirdscript\64bit"
$7zlaunch32 = "$home\Downloads\blackbirdscript\32bit\blackbird.exe"
$7zlaunch64 = "$home\Downloads\blackbirdscript\64bit\blackbird.exe"
$arguments = "-l"
$zipfile = ($env:COMPUTERNAME+".zip")
##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"
#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,
blackbird -l a été lancé sur $hote.$domain
"
##FONCTION DE test de l'OS (windows/linux, 32bit/64bit)
function osversion ()
{
$os = $osname.split("|") | select-object -first 1
if ($os -like '*Win*')
{
Write-Host "your system is a $os $osarchitecture"
}
elseif ($IsLinux -eq $True)
{
Write-Host "your operating system is Linux"
}
else
{
Write-Host "your operating system is MacOS"
}
}
##FONCTION_CREATION_DIRTMP pour stocker les fichiers temporaires et log
Function tmpfolder()
{
try {
New-Item -ItemType Directory -Force -Path $dirtmp
$tag | Out-File -FilePath $logfile
log("TMP folder created")
# ADD-content -Path $logfile -Value "TMP directory created"
} catch
{
#log("impossible to create the TMP directory")
Add-Content -Path $logfile -Value "impossible to create the TMP directory"
}
}
##FONCTION LOG
Function log ($var)
{
ADD-content -Path $logfile -Value `n
ADD-content -Path $logfile -Value $var
}
##FONCTION FREE_SPACE
#Test l'espace libre sur le disque dur : si moins de 1 MO, arrêt du script
function free_space ()
{
$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"
Add-Content -Path $logfile -Value "Not enough disk space : script exited "
Start-Sleep -Seconds 10
}
else
{
Write-Host "Enough free space on your hard drive
The script will start in 5 seconds"
Start-Sleep -Seconds 5
}
}
#test connexion Internet
function ping ()
{
try {
Test-Connection -computername 8.8.8.8 -Count 1 -ErrorAction stop
# Out-File -FilePath $logfile -Append
#log("ping 8.8.8.8 OK")
Add-Content -Path $logfile -Value "PING 8.8.8.8 OK"
}
catch [System.Net.NetworkInformation.PingException]
{
Write-Host "Please, connect to the Internet" -ForegroundColor Red
Add-Content -Path $logfile -Value "No internet connexion, script exited"
#exit
}
}
#TEST DNS
function pingDNS ()
{
try {
Test-Connection -computername www.google.com -Count 1 -ErrorAction stop
#Out-File -FilePath $dirtmp\log.txt -Append
#log("DNS OK")
Add-Content -Path $logfile -Value "DNS OK"
}
catch [System.Net.NetworkInformation.PingException]
{
Write-Host "google.com is unreashable" -ForegroundColor Red
Add-Content -Path $logfile -Value "No DNS : contact your network administrator"
# Exit
}
}
#presentation du script à l'utilisateur
Function test_user ()
{
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 ..."
}
#BLACKBIRD : outil permettant de resoudre les problèmes réseaux (skype, partages, VPN)
function blackbird ()
{
if ($osarchitecture -like '*64*')
{
cd $7zdest64
Start-Process -FilePath $7zlaunch64 -ArgumentList $arguments
#ADD-content -Path $logfile -Value "BLACKBIRD successfully executed"
}
elseif ($osarchitecture -like '*32*')
{
cd $7zdest32
Start-Process -FilePath $7zlaunch32 -ArgumentList $arguments
#ADD-content -Path $logfile -Value "BLACKBIRD successfully executed"
}
else
{
#ADD-content -Path $logfile -Value "impossible"
}
}
#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 ()
{
$tmpfile="$dirtmp\tmp.txt"
#recuperer les noms des browsers et les envoyer dans un fichier texte
$browser_list = Get-ItemProperty HKLM:\Software\Clients\StartMenuInternet\* | Select-Object -ExpandProperty '(default)' |Out-File -FilePath $tmpfile
#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 = (get-content $tmpfile | measure-object -line).lines
#recuperer la valeur de chaque ligne
$ligne1 = Get-Content -Path $tmpfile | where { $_ -ne "$null" } | Select-Object -Index 0
$ligne2 = Get-Content -Path $tmpfile | where { $_ -ne "$null" } | Select-Object -Index 1
$ligne3 = Get-Content -Path $tmpfile | where { $_ -ne "$null" } | Select-Object -Index 2
#lecture de la réponse de l'utilisateur
Write-Host « [1]- $ligne1 »
Write-Host « [2]- $ligne2 »
Write-Host « [3]- $ligne3 »
$browser = Read-Host « Choose your Browser : 1 - 3 »
Switch ($browser)
{
1 {$browser = $ligne1}
2 {$browser = $ligne2}
3 {$browser = $ligne3}
default {$browser=$ligne1}
}
Write-Host « Your browser is $browser ! »
#suppression des fichiers en fonction de la réponse
if ($browser -eq $ligne1) {
###pour les tests, suppression d'un fichier test.txt préalablement créé pour éviter de supprimer les fichiers de profil
#Remove-Item -path "C:$home\AppData\Roaming\Mozilla\Firefox\Profiles\*.default\key4.db" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
#Remove-Item -path "C:$home\AppData\Roaming\Mozilla\Firefox\Profiles\*.default\logins.json" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
Remove-Item -path "$home\Downloads\test1.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
ADD-content -Path $logfile -Value "All your Firefox credentials cleared"
}
elseif ($browser -eq $ligne2) {
#Remove-Item -path "C:$home\AppData\Local\Google\Chrome\User Data\Default\Login Data" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
Remove-Item -path "$home\Downloads\test2.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
ADD-content -Path $logfile -Value "All your Chrome credentials cleared"
}
elseif ($browser -eq $ligne3) {
Remove-Item -path "$home\Downloads\test3.txt" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
ADD-content -Path $logfile -Value "All your Internet Explorer credentials cleared"
}
else
{
ADD-content -Path $logfile -Value "No modification of yours browser credentials "
}
}
#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 \\FILER1-IINS\IINS_4ALL\LOGS -PSProvider FileSystem
Copy-Item "$home\Downloads\TMP\$zipfile" -Destination "\\FILER1-IINS\IINS_4ALL\LOGS"
Remove-PSDrive temp
Write-Host "impossible to send an email : logfile copy into iins_4all/LOGS folder"
Add-Content -Path $logfile -Value "impossible to send an email : logfile copy into iins_4all/LOGS folder"
}
}
Function delete_tmp ()
{
$delete_file = @($dirtmp)
cd C:\Users\jfmoni.NEUROCAMPUS\Downloads
Remove-Item $delete_file -Recurse -Force
}
Function main ()
{
osversion
tmpfolder
log
test_user
ping
pingDNS
free_space
blackbird
clean_browser
wifi_clean
send_email
delete_tmp
}
main