Question
Optimisation et gestion des erreurs.
- Dwelatha
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 11
- Remerciements reçus 0
il y a 13 ans 1 mois #14395
par Dwelatha
Optimisation et gestion des erreurs. a été créé par Dwelatha
Bonjour à tous,
Je me suis mis à Powershell depuis une bonne semaine et je sollicite votre aide concernant un souci que je rencontre sur le bout de code suivant.
Le script en question, doit aller chercher un certain nombre d'information concernant l'antivirus et l'OS sur des postes de travail, serveurs.
Le code:
[code:1]Function GetRegInfo
{
# HKLM\SOFTWARE\McAfee\DesktopProtection
$key=\"SOFTWARE\McAfee\DesktopProtection\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key)
$Product = $regKey.GetValue(\"Product\"«»)
$c.Cells.Item($intRow,7) = $Product
$productver = $regKey.GetValue(\"szProductVer\"«»)
if ($productver -ge \"8.8.0.975\"«»){
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 4
} Elseif ($productver -eq \"8.8.0.849\"«») {
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 44
} Else {
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 3
}
$key1=\"SOFTWARE\McAfee\Vscore\On Access Scanner\McShield\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key1)
$key2=\"SOFTWARE\McAfee\AVEngine\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key2)
# HKLM\Software\McAfee\Vscore\On Acess Scanner\McShield (Pour les versions antérieures à VSE 8.8)
$ScanEngine = $regKey.GetValue(\"EngineVersionMajor\"«»)
$c.Cells.Item($intRow,9) = $ScanEngine
# HKLM\Software\McAfee\AVEngine (pour les versions VSE 8.8)
$ScanEngine = $regKey.GetValue(\"EngineVersionMajor\"«»)
$c.Cells.Item($intRow,9) = $ScanEngine
# HKLM\Software\McAfee\Vscore\On Acess Scanner\McShield (pour les versions antérieures à VSE 8.8)
$VirDefVer = $regKey.GetValue(\"AVDatVersion\"«»)
$c.Cells.Item($intRow,10) = $VirDefVer
# HKLM\Software\McAfee\AVEngine (pour les versions VSE 8.8)
$VirDefVer = $regKey.GetValue(\"AVDatVersion\"«»)
$c.Cells.Item($intRow,10) = $VirDefVer
$key2=\"SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key2)
# HKLM\Software\Network Associates\ePolicy Orchestrator\Agent / # Split du fqdn server
$ServerList = $regKey.GetValue(\"ePOServerList\"«») -split{$_ -eq \".\" -or $_ -eq \"|\"}
$c.Cells.Item($intRow,11) = $ServerList
# HKLM\Software\Network Associates\ePolicy Orchestrator\Agent
$AgentGUID = $regKey.GetValue(\"AgentGUID\"«»)
$c.Cells.Item($intRow,12) = $AgentGUID
$key3=\"SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\CMNUPD__3000\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key3)
# HKLM\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\CMNUPD__3000
$AgentVersion = $regKey.GetValue(\"Version\"«»)
if ($AgentVersion -ge \"4.6.0.3122\"«»){
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 4
}Elseif ($AgentVersion -eq \"4.6.0.2935\"«»){
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 44
} Else {
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 3
}
}
#$erroractionpreference = \"SilentlyContinue\"
# Création de l'objet excel
$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
# Création des cellules contenant les titres
$c.Cells.Item(1,1) = \"Nom de machine\"
$c.Cells.Item(1,2) = \"Systeme d'exploitation\"
$c.Cells.Item(1,3) = \"Service Pack\"
$c.Cells.Item(1,4) = \"Domaine\"
$c.Cells.Item(1,5) = \"Architecture\"
$c.Cells.Item(1,6) = \"Utilisateur connecté\"
$c.Cells.Item(1,7) = \"Produit Antivirus\"
$c.Cells.Item(1,8) = \"Version Antivirus\"
$c.Cells.Item(1,9) = \"Version du moteur d'analyse\"
$c.Cells.Item(1,10) = \"Version du fichier DAT\"
$c.Cells.Item(1,11) = \"Serveur EPO\"
$c.Cells.Item(1,12) = \"AgentGUID\"
$c.Cells.Item(1,13) = \"Version de l'Agent\"
$c.Cells.Item(1,14) = \"Statut Ping\"
$c.Cells.Item(1,15) = \"Uptime\"
$c.Cells.Item(1,16) = \"Adresse IP\"
$c.Cells.Item(1,17) = \"Date du report\"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$intRow = 2
# Lecture du fichier d'entrée
$colComputers = get-content c:\temp\test.txt
foreach ($strComputer in $colComputers)
{
# Test ping sur les machines se trouvant dans Machines-list.txt
$Ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer,10)
if ($Reply.status –eq “Success”) {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
} Else {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
}
$Reply = \"\"
$OS = get-wmiobject Win32_OperatingSystem -computername $strComputer
$Computer = get-wmiobject Win32_computerSystem -computername $strComputer
$IP = get-wmiobject Win32_NetworkAdapterConfiguration -computername $strComputer | Where{$_.IPEnabled –eq $true}
$c.Cells.Item($intRow,1) = $strComputer.ToUpper()
$c.Cells.Item($intRow,2) = $OS.Caption
$c.Cells.Item($intRow,3) = $OS.CSDVersion
$c.Cells.Item($intRow,4) = $Computer.Domain
$c.Cells.Item($intRow,5) = $Computer.SystemType
$c.Cells.Item($intRow,6) = $Computer.Username
$c.Cells.Item($intRow,15) = [System.Management.ManagementDateTimeconverter]::ToDateTime($OS.LastBootUpTime)
GetRegInfo
# Récupération de(s) IP active(s) sur les postes.
Foreach ($IPConfig in $IP)
{
if ($Ipconfig.IPaddress) {
Foreach ($addr in $Ipconfig.Ipaddress | where {$_ -notmatch \":\"})
{
$c.Cells.Item($intRow,16) = $addr
}
}
$c.Cells.Item($intRow,17) = Get-date
$intRow = $intRow + 1
}
}
# On redimensionne les colonnes du fichier excel.
$d.EntireColumn.AutoFit()
# On enregistre le fichier dans la directory ci-dessous.
$b.SaveAs(\"C:\temp\Versioning McAfee $(get-date -f dd-MM-yyyy-H-m).xls\"«»)
$b.close()
$a.Quit()
$a = $null
$b = $null
$c = $null
cls[/code:1]
A l'heure actuelle il fonctionne mais je le trouve super long, surtout quand je dois vérifier plus de 600 machines par exemple. Il met beaucoup de temps.
Ce que j'aimerai faire, c'est Ping de la machine, si elle ne réponds pas, on passe à la suivante tout en consignant la machine \"off line\" dans mon fichier excel.
J'ai essayé avec un:
[code:1]If (-not (test-connection -computername $strComputer -Count 1 -Quiet)) {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
Continue
} Else {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
}[/code:1]
à la place de
[code:1]$Ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer,10)
if ($Reply.status –eq “Success”) {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
} Else {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
}
$Reply = \"\"[/code:1]
Mais le soucis, c'est qu'il ne me garde pas les machines offline dans le fichier excel. De plus je crois qu'il fait les requêtes WMI, vérifie les clés de registre quand même
Le second point concernant l'optimisation de la fonction GetRegInfo. Je ne rencontre aucun problème sur des postes en 32 bits mais par contre les machines en 64 bits ne me retourne aucune info.
Normal me direz vous car les clés ne se trouve pas au même endroit.
Clé en 32 bits
[code:1]$key=\"SOFTWARE\McAfee\DesktopProtection\"[/code:1]
Clé en 64 bits
[code:1]$key=\"SOFTWARE\wow6432node\McAfee\DesktopProtection\"[/code:1]
Selon vous, qu'elle serait la façon la plus optimisée pour récupérer ces informations ?
Vous remerciant par avance pour le temps que vous me consacrerez.
Bonne soirée.
Vous trouverez ci-joint le fichier excel qui fait office de log.
Je me suis mis à Powershell depuis une bonne semaine et je sollicite votre aide concernant un souci que je rencontre sur le bout de code suivant.
Le script en question, doit aller chercher un certain nombre d'information concernant l'antivirus et l'OS sur des postes de travail, serveurs.
Le code:
[code:1]Function GetRegInfo
{
# HKLM\SOFTWARE\McAfee\DesktopProtection
$key=\"SOFTWARE\McAfee\DesktopProtection\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key)
$Product = $regKey.GetValue(\"Product\"«»)
$c.Cells.Item($intRow,7) = $Product
$productver = $regKey.GetValue(\"szProductVer\"«»)
if ($productver -ge \"8.8.0.975\"«»){
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 4
} Elseif ($productver -eq \"8.8.0.849\"«») {
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 44
} Else {
$c.Cells.Item($intRow,8) = $productver
$c.Cells.Item($intRow,8).Interior.ColorIndex = 3
}
$key1=\"SOFTWARE\McAfee\Vscore\On Access Scanner\McShield\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key1)
$key2=\"SOFTWARE\McAfee\AVEngine\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key2)
# HKLM\Software\McAfee\Vscore\On Acess Scanner\McShield (Pour les versions antérieures à VSE 8.8)
$ScanEngine = $regKey.GetValue(\"EngineVersionMajor\"«»)
$c.Cells.Item($intRow,9) = $ScanEngine
# HKLM\Software\McAfee\AVEngine (pour les versions VSE 8.8)
$ScanEngine = $regKey.GetValue(\"EngineVersionMajor\"«»)
$c.Cells.Item($intRow,9) = $ScanEngine
# HKLM\Software\McAfee\Vscore\On Acess Scanner\McShield (pour les versions antérieures à VSE 8.8)
$VirDefVer = $regKey.GetValue(\"AVDatVersion\"«»)
$c.Cells.Item($intRow,10) = $VirDefVer
# HKLM\Software\McAfee\AVEngine (pour les versions VSE 8.8)
$VirDefVer = $regKey.GetValue(\"AVDatVersion\"«»)
$c.Cells.Item($intRow,10) = $VirDefVer
$key2=\"SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key2)
# HKLM\Software\Network Associates\ePolicy Orchestrator\Agent / # Split du fqdn server
$ServerList = $regKey.GetValue(\"ePOServerList\"«») -split{$_ -eq \".\" -or $_ -eq \"|\"}
$c.Cells.Item($intRow,11) = $ServerList
# HKLM\Software\Network Associates\ePolicy Orchestrator\Agent
$AgentGUID = $regKey.GetValue(\"AgentGUID\"«»)
$c.Cells.Item($intRow,12) = $AgentGUID
$key3=\"SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\CMNUPD__3000\"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key3)
# HKLM\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\CMNUPD__3000
$AgentVersion = $regKey.GetValue(\"Version\"«»)
if ($AgentVersion -ge \"4.6.0.3122\"«»){
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 4
}Elseif ($AgentVersion -eq \"4.6.0.2935\"«»){
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 44
} Else {
$c.Cells.Item($intRow,13) = $AgentVersion
$c.Cells.Item($intRow,13).Interior.ColorIndex = 3
}
}
#$erroractionpreference = \"SilentlyContinue\"
# Création de l'objet excel
$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
# Création des cellules contenant les titres
$c.Cells.Item(1,1) = \"Nom de machine\"
$c.Cells.Item(1,2) = \"Systeme d'exploitation\"
$c.Cells.Item(1,3) = \"Service Pack\"
$c.Cells.Item(1,4) = \"Domaine\"
$c.Cells.Item(1,5) = \"Architecture\"
$c.Cells.Item(1,6) = \"Utilisateur connecté\"
$c.Cells.Item(1,7) = \"Produit Antivirus\"
$c.Cells.Item(1,8) = \"Version Antivirus\"
$c.Cells.Item(1,9) = \"Version du moteur d'analyse\"
$c.Cells.Item(1,10) = \"Version du fichier DAT\"
$c.Cells.Item(1,11) = \"Serveur EPO\"
$c.Cells.Item(1,12) = \"AgentGUID\"
$c.Cells.Item(1,13) = \"Version de l'Agent\"
$c.Cells.Item(1,14) = \"Statut Ping\"
$c.Cells.Item(1,15) = \"Uptime\"
$c.Cells.Item(1,16) = \"Adresse IP\"
$c.Cells.Item(1,17) = \"Date du report\"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$intRow = 2
# Lecture du fichier d'entrée
$colComputers = get-content c:\temp\test.txt
foreach ($strComputer in $colComputers)
{
# Test ping sur les machines se trouvant dans Machines-list.txt
$Ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer,10)
if ($Reply.status –eq “Success”) {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
} Else {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
}
$Reply = \"\"
$OS = get-wmiobject Win32_OperatingSystem -computername $strComputer
$Computer = get-wmiobject Win32_computerSystem -computername $strComputer
$IP = get-wmiobject Win32_NetworkAdapterConfiguration -computername $strComputer | Where{$_.IPEnabled –eq $true}
$c.Cells.Item($intRow,1) = $strComputer.ToUpper()
$c.Cells.Item($intRow,2) = $OS.Caption
$c.Cells.Item($intRow,3) = $OS.CSDVersion
$c.Cells.Item($intRow,4) = $Computer.Domain
$c.Cells.Item($intRow,5) = $Computer.SystemType
$c.Cells.Item($intRow,6) = $Computer.Username
$c.Cells.Item($intRow,15) = [System.Management.ManagementDateTimeconverter]::ToDateTime($OS.LastBootUpTime)
GetRegInfo
# Récupération de(s) IP active(s) sur les postes.
Foreach ($IPConfig in $IP)
{
if ($Ipconfig.IPaddress) {
Foreach ($addr in $Ipconfig.Ipaddress | where {$_ -notmatch \":\"})
{
$c.Cells.Item($intRow,16) = $addr
}
}
$c.Cells.Item($intRow,17) = Get-date
$intRow = $intRow + 1
}
}
# On redimensionne les colonnes du fichier excel.
$d.EntireColumn.AutoFit()
# On enregistre le fichier dans la directory ci-dessous.
$b.SaveAs(\"C:\temp\Versioning McAfee $(get-date -f dd-MM-yyyy-H-m).xls\"«»)
$b.close()
$a.Quit()
$a = $null
$b = $null
$c = $null
cls[/code:1]
A l'heure actuelle il fonctionne mais je le trouve super long, surtout quand je dois vérifier plus de 600 machines par exemple. Il met beaucoup de temps.
Ce que j'aimerai faire, c'est Ping de la machine, si elle ne réponds pas, on passe à la suivante tout en consignant la machine \"off line\" dans mon fichier excel.
J'ai essayé avec un:
[code:1]If (-not (test-connection -computername $strComputer -Count 1 -Quiet)) {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
Continue
} Else {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
}[/code:1]
à la place de
[code:1]$Ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer,10)
if ($Reply.status –eq “Success”) {
$c.Cells.Item($intRow, 14) = “En ligne”
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 4
} Else {
$c.Cells.Item($intRow, 14) = \"Hors ligne\"
$c.Cells.Item($intRow, 14).Interior.ColorIndex = 3
}
$Reply = \"\"[/code:1]
Mais le soucis, c'est qu'il ne me garde pas les machines offline dans le fichier excel. De plus je crois qu'il fait les requêtes WMI, vérifie les clés de registre quand même
Le second point concernant l'optimisation de la fonction GetRegInfo. Je ne rencontre aucun problème sur des postes en 32 bits mais par contre les machines en 64 bits ne me retourne aucune info.
Normal me direz vous car les clés ne se trouve pas au même endroit.
Clé en 32 bits
[code:1]$key=\"SOFTWARE\McAfee\DesktopProtection\"[/code:1]
Clé en 64 bits
[code:1]$key=\"SOFTWARE\wow6432node\McAfee\DesktopProtection\"[/code:1]
Selon vous, qu'elle serait la façon la plus optimisée pour récupérer ces informations ?
Vous remerciant par avance pour le temps que vous me consacrerez.
Bonne soirée.
Vous trouverez ci-joint le fichier excel qui fait office de log.
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.037 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Optimisation et gestion des erreurs.