Skip to content

Instantly share code, notes, and snippets.

@bahamut45
Created March 4, 2016 06:12
Show Gist options
  • Save bahamut45/1fcd5216182691b8e5d3 to your computer and use it in GitHub Desktop.
Save bahamut45/1fcd5216182691b8e5d3 to your computer and use it in GitHub Desktop.
Powershell logon + logoff
# Filename : logoff.ps1
# Date : January 18, 2016
# Author : Nicolas JOUBERT
# Description: Logoff script
# *****************************
$strName = $env:username
$strProfile = $env:userprofile
$srvName = "srvweb"
$atomProcess = Get-Process atom -ErrorAction SilentlyContinue
Write-Host "Script de deconnexion" -foregroundcolor green
Write-Host "Au revoir $strName" -foregroundcolor green
# Application atom
If ($atomProcess)
{
Write-Host "Atom est démarré, extinction en cours" -foregroundcolor green
Get-Process atom | Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process -force
}
# Serveur Web
If (&vmrun list | Select-String -Quiet ServerWeb)
{
Write-Host "Serveur Web est démarré, extinction en cours" -foregroundcolor green
&vmrun -T ws stop "I:\Program Files (x86)\VmWare\ServerWeb\ServerWeb.vmx" soft
}
# Filename : logon.ps1
# Date : January 17, 2016
# Author : Nicolas JOUBERT
# Description: Logon script
# *****************************
$strName = $env:username
$strProfile = $env:userprofile
$srvName = "srvweb"
$today = (get-date).Date
$logFile = $strProfile + "\" + "login.log"
If (Test-Path $logFile)
{
Get-ItemProperty -Path $logfile | where { $_.CreationTime.Date -lt $today } | Remove-Item
}
function WriteTo-Log
{
param (
[string]$String="*",
[string]$LogFile = ('.\'+(Get-History -Id ($MyInvocation.HistoryId -1) | select StartExecutionTime).startexecutiontime.tostring('yyyy-MM-dd-HH.mm')+'-'+[io.path]::GetFileNameWithoutExtension($MyInvocation.ScriptName)+'.log'),
[Switch]$OutputToScreen,
[ValidateSet("Black","DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White")]
[String]$ForegroundColor=(Get-Host).ui.RawUI.ForegroundColor,
[ValidateSet("Black","DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White")]
[String]$BackgroundColor=(Get-Host).ui.RawUI.BackgroundColor
)
if (!(Test-Path $LogFile))
{
Write-Output "Creating log file $LogFile"
$LogFile = New-Item $LogFile -Type file
}
$datetime = (Get-Date).ToString('yyyy-MM-dd HH.mm.ss')
$StringToWrite = "$datetime : $String"
if ($OutputToScreen)
{
if ( $BackgroundColor -ne '-1')
{
Write-Host $StringToWrite -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor
}
else
{
Write-Host $StringToWrite -ForegroundColor $ForegroundColor
}
}
Add-Content -Path $LogFile -Value $StringToWrite
}
$keepassProcess = Get-Process keepass -ErrorAction SilentlyContinue
$keepassExe = "C:\Program Files (x86)\KeePass Password Safe 2\KeePass.exe"
$keepassDb = $strProfile + "\" + "Google Drive\Personnel\KeePass.kdbx"
$keepassArgsMinimize = '-minimize'
$keepassArgsPwd = '-pw:<motdepassedelabasekeepass>'
$keepassAllArgs = @($keepassArgsMinimize,$keepassArgsPwd)
$dragkingProcess = Get-Process dragking -ErrorAction SilentlyContinue
$dragkingExe = $strProfile + "\" + "AppData\Local\DragKing\DragKing.exe"
$dragkingIni = $strProfile + "\" + "AppData\Local\DragKing\DragKing.ini"
WriteTo-Log "Script de connexion" $logFile -OutputToScreen -ForegroundColor Green
WriteTo-Log "Bonjour $strName" $logFile -OutputToScreen -ForegroundColor Green
# Serveur Web
If (&vmrun list | Select-String -Quiet ServerWeb)
{
WriteTo-Log "Serveur Web est déjà demarré" $logFile -OutputToScreen -ForegroundColor Red
If (!(Test-Path Z:))
{
$mount = New-SmbMapping -LocalPath Z: -RemotePath \\SRVWEB\DEVELOPPEMENT -Username $strName -Password <mot de passe samba> -Verbose
WriteTo-Log "Montage $mount" $logFile
WriteTo-Log "Montage du lecteur réseau Z [OK]" $logFile -OutputToScreen -ForegroundColor Green
}
}
Else
{
&vmrun -T ws start "I:\Program Files (x86)\VmWare\ServerWeb\ServerWeb.vmx" nogui
WriteTo-Log "Démarrage de ServerWeb en cours" $logFile -OutputToScreen -ForegroundColor Green
do {
WriteTo-Log "En attente de réponse de $srvName..." $logFile -OutputToScreen -ForegroundColor Yellow
sleep 1
} until(Test-Connection $srvName -quiet )
WriteTo-Log "[$srvName] ping [OK]" $logFile -OutputToScreen -ForegroundColor Green
do {
WriteTo-Log "En attente du démarrage de samba..." $logFile -OutputToScreen -ForegroundColor Yellow
sleep 1
} until(Test-NetConnection $srvName -CommonTCPPort SMB -InformationLevel Quiet)
WriteTo-Log "[$srvName] samba [OK]" $logFile -OutputToScreen -ForegroundColor Green
If (!(Test-Path Z:))
{
$mount = New-SmbMapping -LocalPath Z: -RemotePath \\SRVWEB\DEVELOPPEMENT -Username $strName -Password <mot de passe samba> -Verbose
WriteTo-Log "Montage $mount" $logFile
WriteTo-Log "Montage du lecteur réseau Z [OK]" $logFile -OutputToScreen -ForegroundColor Green
}
}
# Application Keepass2
if ($keepassProcess)
{
WriteTo-Log "Keepass est déja démarré" $logFile -OutputToScreen -ForegroundColor Red
}
Else
{
&$keepassExe $keepassAllArgs "$keepassDb"
WriteTo-Log "Keepass est démarré [OK]" $logFile -OutputToScreen -ForegroundColor Green
}
# Application DragKing
If ($dragkingProcess)
{
WriteTo-Log "DragKing est déja démarré" $logFile -OutputToScreen -ForegroundColor Red
}
Else
{
&$dragkingExe $dragkingIni
WriteTo-Log "DragKing est démarré [OK]" $logFile -OutputToScreen -ForegroundColor Green
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment