powershellico

Powershell: Auswertung von Empirum Installationsaufträgen für einen bestimmten Zeitraum eines Rechners, oder Rechnergruppe

Im Kundenumfeld gab und gibt es oft die Anforderung, das man für einen Rechner, oder eine Rechnergruppe die über Empirum durchgeführten Installationen für einen Zeitraum x ermitteln soll, oder muss.

Hierfür habe ich ein Skript geschrieben welches die Paketinstallationen eines Rechners, oder einer Rechnergruppe für einen bestimmten Zeitraum auswertet. Dabei werden die Daten aus der Empirum Datenbank gelesen und in einem Excel Sheet aufbereitet zur Verfügung gestellt.

Die Steuerung findet über eine GUI statt.
ps_auswertung_01

Bei der Auswertung werden die Benutzerteile eines Empirum Paketes herausgefiltert, so das nur die eigentlichen Paketinstallation übrig bleiben.

ps_auswertung_02

Actions-help-about-icon Tipp: Über die exclusion.ini können Pakete hinterlegt werden, die nicht in die Auswertung mit einbezogen werden sollen. Ebenfalls können die PatchInstallationen exkludiert werden.

Hinweis: Im PowerShell Skript muss noch entsprechend der Datenbankserver und die Datenbank der Umgebung angepasst werden!

Powershell Skript:   Download

#-----Auslesen des Skriptpfad-----------------------------------------------
function Get-ScriptDirectory {
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
#---------------------------------------------------------------------------

#-----Variablen-------------------------------------------------------------
$global:cc_single_output = ""
$global:cc_multi_output = ""
$global:DataSet = ""
$cc_input_pcname = ""
$cc_input_datemin = ""
$cc_input_datemax = ""
$cc_input_customname = ""
$cc_input_checkpatches = "no"
$cc_input_checkexc = "no"
$cc_exclusion_file = Join-Path (Get-ScriptDirectory) exclusion.ini;
#---------------------------------------------------------------------------

#-----GUI-------------------------------------------------------------------
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Empirum Auswertungs-Tool"
$objForm.Size = New-Object System.Drawing.Size(600,280)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(200,210)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$cc_input_customname=$objTextBox_Fname.Text; $cc_input_pcname=$objTextBox_PCName.Text;$cc_input_datemin=$objTextBox_startdate.Text;$cc_input_datemax=$objTextBox_enddate.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(320,210)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(400,20)
$objLabel.Text = "PC Name/n: (Mehrere PCs mit Semikolon trennen >> vm9900111;vm9900112)"
$objForm.Controls.Add($objLabel)

$objTextBox_PCName = New-Object System.Windows.Forms.TextBox
$objTextBox_PCName.Location = New-Object System.Drawing.Size(10,40)
$objTextBox_PCName.Size = New-Object System.Drawing.Size(400,20)
$objForm.Controls.Add($objTextBox_PCName)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,70)
$objLabel.Size = New-Object System.Drawing.Size(120,20)
$FontBold = new-object System.Drawing.Font("Arial",8,[Drawing.FontStyle]'Bold' )
$objLabel.Font = $fontBold
$objLabel.Text = "Auswertungszeitraum"
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,90)
$objLabel.Size = New-Object System.Drawing.Size(160,20)
$objLabel.Text = "Anfangsdatum (dd.mm.yyyy):"
$objForm.Controls.Add($objLabel)

$objTextBox_startdate = New-Object System.Windows.Forms.TextBox
$objTextBox_startdate.Location = New-Object System.Drawing.Size(170,90)
$objTextBox_startdate.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox_startdate)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(290,90)
$objLabel.Size = New-Object System.Drawing.Size(130,20)
$objLabel.Text = "Enddatum (dd.mm.yyyy):"
$objForm.Controls.Add($objLabel)

$objTextBox_Enddate = New-Object System.Windows.Forms.TextBox
$objTextBox_Enddate.Location = New-Object System.Drawing.Size(430,90)
$objTextBox_Enddate.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox_Enddate)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,120)
$objLabel.Size = New-Object System.Drawing.Size(150,20)
$FontBold = new-object System.Drawing.Font("Arial",8,[Drawing.FontStyle]'Bold' )
$objLabel.Font = $fontBold
$objLabel.Text = "Dateiname:"
$objForm.Controls.Add($objLabel)

$objTextBox_Fname = New-Object System.Windows.Forms.TextBox
$objTextBox_Fname.Location = New-Object System.Drawing.Size(170,120)
$objTextBox_Fname.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox_Fname)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(270,124)
$objLabel.Size = New-Object System.Drawing.Size(180,20)
$objLabel.Text = "_01.01.2012_01.04.2012.xlsx"
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,150)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "MS Patche in die Auswertung mit einbeziehen"
$objForm.Controls.Add($objLabel)

$Checkbox = new-object System.Windows.Forms.Checkbox
$Checkbox.Location = new-object System.Drawing.Size(290,150)
$Checkbox.size = new-object System.Drawing.Size(20,20)
$objForm.Controls.Add($Checkbox)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,170)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Paketausschlüsse in die Auswertung mit einbeziehen"
$objForm.Controls.Add($objLabel)

$CheckboxExc = new-object System.Windows.Forms.Checkbox
$CheckboxExc.Location = new-object System.Drawing.Size(290,170)
$CheckboxExc.size = new-object System.Drawing.Size(20,20)
$objForm.Controls.Add($CheckboxExc)

# Auswertung der Checkbox
$Checkbox.add_CheckStateChanged({
If ($Checkbox.CheckState -eq "unchecked" )
{$cc_input_checkpatches = "no"}
Else
{$cc_input_checkpatches = "yes"}
})

# Auswertung der CheckboxExc
$CheckboxExc.add_CheckStateChanged({
If ($CheckboxExc.CheckState -eq "unchecked" )
{$cc_input_checkexc = "no"}
Else
{$cc_input_checkexc = "yes"}
})

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

#$cc_input_pcname = "vm8700133"
#$cc_input_datemin = "20.03.2012"
#$cc_input_datemax = "24.04.2012"

If ($cc_input_pcname -eq "" -and $cc_input_datemin -eq "" -and $cc_input_datemax -eq "" -and $cc_input_customname -eq ""){
Exit
}

If ($cc_input_pcname -eq ""){
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup("Es wurde kein PC Name angegeben. Die Auswertung wird abgebrochen!",30,"Fehler",16)
Exit
}

If ($cc_input_datemin -eq ""){
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup("Es wurde kein Startdatum angegeben. Die Auswertung wird abgebrochen!",30,"Fehler",16)
Exit
}

If ($cc_input_datemax -eq ""){
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup("Es wurde kein Enddatum angegeben. Die Auswertung wird abgebrochen!",30,"Fehler",16)
Exit
}

If ($cc_input_customname -eq ""){
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup("Es wurde kein Dateiname angegeben. Die Auswertung wird abgebrochen!",30,"Fehler",16)
Exit
}

$cc_input_pcname_split = $cc_input_pcname.split(";")

$cc_datum_date_min = get-date ($cc_input_datemin)
$cc_datum_date_max = get-date ($cc_input_datemax)
##Da der aktuelle Tag im Max wert nicht berücksichtigt wird
$cc_datum_date_max = $cc_datum_date_max.AddDays(1)

#-----Einlesen der ini Datei für die Paketausschlüsse------------------
If ($cc_input_checkexc -eq "yes"){
If (test-path $cc_exclusion_file) {
$cc_input_exclusion = Get-Content $cc_exclusion_file
}
Else {
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup("Ausschlussdatei exclusion.ini existiert nicht im Skriptverzeichnis. Die Auswertung wird abgebrochen!",30,"Fehler",16)
Exit
}
}
#----------------------------------------------------------------------

#-----Erstellen eines xlsx--------------------------------------------------
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$workbook = $Excel.workbooks.add()

$excel.cells.item(1,1) = "PC-Systeme"
$excel.cells.item(1,2) = "$cc_input_pcname"

$excel.cells.item(3,1) = "Zeitraum"
$excel.cells.item(3,2) = "$cc_input_datemin bis $cc_input_datemax"

$excel.cells.item(5,1) = "Anzahl an Aufträgen"
$excel.cells.item(5,3) = "Benutzerteile ausgeschlossen!"

$excel.cells.item(7,1) = "Nr."
$excel.cells.item(7,2) = "Datum"
$excel.cells.item(7,3) = "PC Name"
$excel.cells.item(7,4) = "Software"
$excel.cells.item(7,5) = "Installationsmodus"
$excel.cells.item(7,6) = "Ergebnis"

$excel.cells.item(1,1).Font.Bold = $True
$excel.cells.item(3,1).Font.Bold = $True
$excel.cells.item(5,1).Font.Bold = $True
$excel.cells.item(7,1).Font.Bold = $True
$excel.cells.item(7,2).Font.Bold = $True
$excel.cells.item(7,3).Font.Bold = $True
$excel.cells.item(7,4).Font.Bold = $True
$excel.cells.item(7,5).Font.Bold = $True
$excel.cells.item(7,6).Font.Bold = $True
$excel.cells.item(5,3).Font.Italic = $True

$excel.cells.item(1,2).Interior.ColorIndex = 37
$excel.cells.item(3,2).Interior.ColorIndex = 37
$excel.cells.item(5,2).Interior.ColorIndex = 37
$excel.cells.item(7,1).Interior.ColorIndex = 37
$excel.cells.item(7,2).Interior.ColorIndex = 37
$excel.cells.item(7,3).Interior.ColorIndex = 37
$excel.cells.item(7,4).Interior.ColorIndex = 37
$excel.cells.item(7,5).Interior.ColorIndex = 37
$excel.cells.item(7,6).Interior.ColorIndex = 37

#---------------------------------------------------------------------------

#----Datenbankzugriff / Auslesen der Pakete / Ablage in Datei--
function execute-sql-multi-output {
param (
[string]$dbserver,
[string]$db,
[string]$SQLCommand
)

#Connection to SQL-Server
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $dbserver; Database = $db; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLCommand
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$global:cc_multi_output = New-Object System.Data.DataSet
$SqlAdapter.Fill($global:cc_multi_output)
$SqlConnection.Close()
}
#--------------------------------------------------------------
#---------------------------------------------------------------------------

$excel_line = 8
foreach ($cc_pcname in $cc_input_pcname_split){
#-----Ausführen der Funktion zur Abfrage der Client ID----------------------
$cc_output_count = execute-sql-multi-output "empirumpro" "empirumdb" "SELECT client_id FROM Clients where name = '$cc_pcname'"
$h = 0
$cc_clientid = ""
foreach ($entry in $global:cc_multi_output.Tables[0]){
$cc_clientid = $global:cc_multi_output.Tables[0].Rows[$h][0]
$h++
}

#Wenn der Name nicht gefunden wird, wird ein break ausgeführt
If ($cc_clientid -eq ""){
cls
#Ausgabetext
write-host ""
write-host "--------------------------------------------------------------------------------"
write-host "                               Bitte warten..."
write-host "--------------------------------------------------------------------------------"
write-host ""
write-host "PC " $cc_pcname " wurde in der Datenbank nicht gefunden"
start-sleep -s 4
Break
}
#---------------------------------------------------------------------------

#-----Ausführen der Funktion zur Abfrage der Aufträge des entsprechenden Clients--
$cc_output_count = execute-sql-multi-output "empirumpro" "empirumdb" "SELECT * FROM ArchiveDistJobs where ClientID = '$cc_clientid'"
$i = 0
foreach ($entry in $global:cc_multi_output.Tables[0]){
$cc_clickdown = 0
cls

#Formatierung der Rückgabewerte
$cc_datum = $global:cc_multi_output.Tables[0].Rows[$i][1]
$cc_software = $global:cc_multi_output.Tables[0].Rows[$i][7]
$cc_mode = $global:cc_multi_output.Tables[0].Rows[$i][11]
$cc_result = $global:cc_multi_output.Tables[0].Rows[$i][12]

$cc_datum_str = [STRING]$cc_datum
$cc_datum_dd = "{0:dd.MM.yyyy}" -f [datetime]$cc_datum
$cc_software_str = [STRING]$cc_software
$cc_mode_str = [STRING]$cc_mode
$cc_result_str = [STRING]$cc_result
$cc_datum_date = get-date ($cc_datum)

#Ausgabetext
write-host ""
write-host "--------------------------------------------------------------------------------"
write-host "                               Bitte warten..."
write-host "--------------------------------------------------------------------------------"
write-host ""
write-host "Überprüfung der Softwareaufträge in der Datenbank für PC " $cc_pcname ": " $i
write-host $cc_software_str

If ($cc_datum_date -gt $cc_datum_date_min){
If ($cc_datum_date -le $cc_datum_date_max){
If ($cc_mode -match "User") {$cc_clickdown = 1}
If ($cc_input_checkpatches -eq "no") {
If ($cc_software_str -match "Security Update for" -or $cc_software_str -match "Update for Windows" -or $cc_software_str -match "Cumulative Security Update for" -or $cc_software_str -match "Critical Update for Office" -or $cc_software_str -match "Update for Office" -or $cc_software_str -match "Update for Microsoft" -or $cc_software_str -match "Microsoft Office Compatibility Pack Service" -or $cc_software_str -match "Service Pack 3 for Visio" -or $cc_software_str -match "co-install of .net 3.0 sp" -or $cc_software_str -match "Microsoft .NET Framework 2.0 Service Pack" -or $cc_software_str -match "Microsoft .NET Framework 3.0 Service Pack" -or $cc_software_str -match "Microsoft .NET Framework 3.5 Service Pack" -or $cc_software_str -match "Microsoft .NET Framework 3.5 Family Update" -or $cc_software_str -match "Service Pack 3 for Visio")
{$cc_clickdown = 1}
}
If ($cc_input_checkexc -eq "yes"){
foreach ($cc_exclusion in $cc_input_exclusion) {
If ($cc_exclusion -eq $cc_software_str){$cc_clickdown = 1}
}
}

If ($cc_clickdown -eq 0){
$cc_nr = $excel_line - 7
$excel.cells.item($excel_line,1) = "$cc_nr"
$excel.cells.item($excel_line,2) = "$cc_datum_dd"
$excel.cells.item($excel_line,3) = "$cc_pcname"
$excel.cells.item($excel_line,4) = "$cc_software_str"
$excel.cells.item($excel_line,5) = "$cc_mode_str"
$excel.cells.item($excel_line,6) = "$cc_result_str"
$excel_line++
}
}
}
$i++
}
}
#-----Speichern des Excel Files---------------------------------------------
$cc_filename = "Auswertungen\" + $cc_input_customname + "_" + $cc_input_datemin + "_" + $cc_input_datemax + ".xlsx"
$cc_xlsx_file = Join-Path (Get-ScriptDirectory) $cc_filename;
$excel.cells.item(5,2) = "$cc_nr"
If (test-path $cc_xlsx_file) {Remove-Item $cc_xlsx_file}
$Excel.Cells.EntireColumn.AutoFit()
$workbook.saveas($cc_xlsx_file)
$Excel.Quit()
#---------------------------------------------------------------------------

#-----Öffnen des Excel Sheets-----------------------------------------------
Start-Process Excel -ArgumentList $cc_xlsx_file
#---------------------------------------------------------------------------

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *