L'objectif de ce script est de lister les sessions actives dans vSphere
<#
.SYNOPSIS Retrieve information for all currently logged in vSphere Sessions (excluding current session)
.NOTES Author: William Lam
.NOTES Modification : Antoine Jovelin - Add Functions : verifPowerLI / vcenter-connect / vcenter-disconnect
.NOTES Site: www.williamlam.com
.REFERENCE Blog: http://www.williamlam.com/2016/11/an-update-on-how-to-retrieve-useful-information-from-a-vsphere-login.html
.EXAMPLE
Get-vSphereLogins
#>
cls
# Declaration des fonctions
$herestring = @"
***** L'INSTALLATION A ECHOUEE *****
POUR INSTALLER POWERCLI :
1- RECUPERER LE MODULE DEPUIS CE LIEN : "https://developer.vmware.com/web/tool/vmware-powercli"
2- COPIER LE CONTENU DE L'ARCHIVE DANS "C:\Users\NOM_DU_USER\Documents\WindowsPowerShell\Modules"
3- RELANCER LE SCRIPT
"@
function verifPowerLI {
if($MyModules.name -match "VMware.PowerCLI"){
Write-Host "Le module PowerCLI est installé. Le script va continuer" -ForegroundColor Green
}else{
Write-Host "Le module PowerCLI n'est PAS installé. Le script ne pourra pas continuer" -ForegroundColor Yellow
do{
$reponse = Read-Host " Installer le module VMware.PowerCLI ? [oui/non] "
Switch ($reponse){
oui{
Install-Module -name vmware.powercli -Scope CurrentUser -Confirm:$false -Force
if($MyModules -notcontains "VMware.PowerCLI"){
Write-Host $herestring -ForegroundColor red
sleep 3
Write-Host " Fin du script dans quelques secondes" -ForegroundColor yellow
1..5 | Sort-Object -Descending | %{Start-Sleep -Seconds 1;Write-Host " $_ " -ForegroundColor yellow}
echo " "
exit
}
}
non{
Write-Host " Fin du script dans quelques secondes" -ForegroundColor yellow
1..5 | Sort-Object -Descending | %{Start-Sleep -Seconds 1;Write-Host " $_ " -ForegroundColor yellow}
echo " "
exit
}
}
}while(($reponse -ne "oui") -and ($reponse -ne "non"))
}
}
function vcenter-connect{
$VC = Read-Host -Prompt " Entrer le nom de vCenter "
$cred = Get-Credential
Write-Host;
Write-Host -ForegroundColor cyan "Connexion à vCenter. Merci de patienter..."
Connect-VIServer $VC -Credential $cred
Write-Host;
}
function vcenter-disconnect{
Write-Host;
Write-Host -ForegroundColor cyan "Déconnexion de vCenter..."
Disconnect-viserver -Server $Global:DefaultVIServer -Confirm:$false -Force
Write-Host;
}
Function Get-vSphereLogins {
if($DefaultVIServers -eq $null) {
Write-Host "Error: Please connect to your vSphere environment"
exit
}
# Using the first connection
$VCConnection = $DefaultVIServers[0]
$sessionManager = Get-View ($VCConnection.ExtensionData.Content.SessionManager)
# Store current session key
$currentSessionKey = $sessionManager.CurrentSession.Key
foreach ($session in $sessionManager.SessionList) {
# Ignore vpxd-extension logins as well as the current session
if($session.UserName -notmatch "vpxd-extension" -and $session.key -ne $currentSessionKey) {
$session | Select Username, IpAddress, UserAgent, @{"Name"="APICount";Expression={$Session.CallCount}}, LoginTime
}
}
}
# Debut du script
vcenter-connect
Write-Host;
Write-Host " Vérification des modules nécessaires " -ForegroundColor Cyan
1..3 | Sort-Object -Descending | %{Start-Sleep -Seconds 1}
$MyModules = Get-Module -ListAvailable | select name
Write-Host;
verifPowerLI
Write-Host;
Write-Host -fore yellow " ---- Liste des sessions actives sur vSphere ---- "
Write-Host;
Get-vSphereLogins
Write-Host;
# Fin du script et deconnexion
vcenter-disconnect