2016-04-14 15 views
0

Ich versuche ein Skript zu schreiben, das eine Textdatei von f5 LTM-Ergebnissen nimmt und diese in ein durchsuchbares Array legt, so dass ich Ergebnisse von gestern bis heute vergleichen kann.Powershell Array von Listenobjekten

Dies ist ein Beispiel für die Datei;

MemberCount : 2 
Name   : /Common/blah1 
Availability : AVAILABILITY_STATUS_GREEN 
Enabled  : ENABLED_STATUS_ENABLED 
Status  : The pool is available 

MemberCount : 2 
Name   : /Common/blah2 
Availability : AVAILABILITY_STATUS_GREEN 
Enabled  : ENABLED_STATUS_ENABLED 
Status  : The pool is available 

So ideal würde Ich mag Namen das einzigartige Gebiet machen und die Art der Liste, so dass ich die Änderungen im Status von gestern bis heute vergleichen.

Hier ist der Code, an dem ich arbeite, um die Ergebnisse per E-Mail zu senden, aber es gibt nur Zeile für Zeile Unterschiede, wo ich lieber die Objektänderungen in der E-Mail erhalten würde.

Add-PSSnapIn iControlSnapIn 

$f5_hosts = '192.168.x.x', '192.168.x.x' 
$uid = 'xx' 
$pwd ='xx' 


foreach($f5_host in $f5_hosts){ 
$f5_host_out = $(get-date -f yyyyMMdd)+"_"+$f5_host+".txt" 
$f5_host_out_yesterday = $((get-date).AddDays(-1).ToString('yyyyMMdd'))+"_"+$f5_host+".txt" 

#Check login details and generate LTM output file for $f5_host 
Initialize-F5.iControl -HostName $f5_host -Username $uid -password $pwd 
Get-F5.LTMPool | out-file $f5_host_out 

#// Check if EMP file for yesterday exists and send results else send error 
if (Test-Path $f5_host_out_yesterday){ 
$f5_host_Result = compare-object -ReferenceObject (Get-Content $f5_host_out) -DifferenceObject (Get-Content $f5_host_out_yesterday) 
$f5_host_out_yesterday+": file is Present!" 
$Text_Body = $f5_host+": difference `r`n" 
$Text_Body += ($f5_host_Result | out-string) 
Send-MailMessage -to [email protected] -from [email protected] -subject $f5_host+": F5 Daily LTM Check" -body $Text_Body -smtpserver mailrelay.racqgroup.local 
}else{ 
$f5_host_out_yesterday+": is not file is Present!" 
Send-MailMessage -to [email protected] -from [email protected] -subject $f5_host+": Check failed" -body "Yesterday's file is not present" -smtpserver mailrelay.racqgroup.local 
} 
} 

#Limit File retention to 30days. 
$limit = (Get-Date).AddDays(-30) 
#Get script location 
$path = Get-Location 

# Delete files older than the $limit. 
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force 

So als Ausgang würde ich nur

so etwas wie dies in einer E-Mail sehen möchten
Difference From yesterday to today 

Yesterday 
MemberCount : 2 
Name   : /Common/blah1 
Availability : AVAILABILITY_STATUS_GREEN 
Enabled  : ENABLED_STATUS_ENABLED 
Status  : The pool is available 

Today 
MemberCount : 2 
Name   : /Common/blah1 
Availability : AVAILABILITY_STATUS_RED 
Enabled  : ENABLED_STATUS_ENABLED 
Status  : The pool is available 
+0

Bitte sagen Sie mir, dass Sie nicht wirklich die Login-Informationen für Ihre F5 in Ihrem Skript dort enthalten. – TheMadTechnician

+0

Nein, überhaupt nicht. Es ist eine Test-VM. Produktion f5 hat Einzelfaktor-Logins. – Sapage

+0

Ok, nur dafür sorgen, falls du gehen musst, ändere dein F5-Login jetzt :) – TheMadTechnician

Antwort

0

Ok, auf Ihre zweite Frage, Exportieren und Passwörter importieren, wird die Verschlüsselung pro Benutzer durchgeführt (und ich bin mir ziemlich sicher, dass pro Maschine), so dass Sie es nicht exportieren, und haben dann ein anderes importieren Konto, aber für nur gerade ein verschlüsseltes Passwort Speichern Sie diese Funktionen nutzen können:

Function Out-EncryptedPasswordFile{ 
[cmdletbinding()] 
Param(
    [Parameter(Mandatory = $true)] 
    [string]$Password, 
    [Parameter(Mandatory = $true)] 
    [ValidateScript({If(Test-Path (Split-Path $_)){$true}else{Throw "Unable to create file, directory '$(Split-Path $_)\' does not exist."} })][String]$Path 
) 
    ConvertTo-SecureString -AsPlainText $Password -Force | ConvertFrom-SecureString | Set-Content $Path -Encoding Unicode 
} 

#Usage Example 
#Out-EncryptedPasswordFile [email protected] c:\temp\password.txt 

Function Import-EncryptedPasswordFile{ 
[cmdletbinding()] 
Param(
    [Parameter(Mandatory = $true)] 
    [ValidateScript({Test-Path $_})][string]$Path 
) 
    $SSPassword = Get-Content $Path | ConvertTo-SecureString 
    $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SSPassword) 
    [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) 
    [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) 
} 

#Usage Example 
#Import-EncryptedPasswordFile C:\temp\password.txt 
0

nicht sicher, ob Recht oder wr Aber das ergab das Ergebnis, nach dem ich gesucht hatte. json wurde gerade benutzt, damit ich das Objekt speichern und zurück in das Powershell-Objekt konvertieren konnte.

Add-PSSnapIn iControlSnapIn 

$f5_hosts = 'x.x.x.x', 'x.x.x.x' 
$uid = 'xx' 
$pwd ='xx' 


foreach($f5_host in $f5_hosts){ 
$f5_host_out = $(get-date -f yyyyMMdd)+"_"+$f5_host+".json" 
$f5_host_out_yesterday = $((get-date).AddDays(-1).ToString('yyyyMMdd'))+"_"+$f5_host+".json" 

#Check login details and generate LTM output file for $f5_host 
Initialize-F5.iControl -HostName $f5_host -Username $uid -password $pwd 
Get-F5.LTMPool | ConvertTo-Json | out-file $f5_host_out 


#// Check if EMP file for yesterday exists and send results else send error 
if (Test-Path $f5_host_out_yesterday){ 

$f5_host_json_today = Get-Content -Raw $f5_host_out | ConvertFrom-Json 
$f5_host_json_yesterday = Get-Content -Raw $f5_host_out_yesterday | ConvertFrom-Json 


$f5_host_Result = Compare-Object -ReferenceObject ($f5_host_json_today | Sort-Object) -DifferenceObject ($f5_host_json_yesterday | Sort-Object) -property MemberCount, Name, Status, Availability, Enabled, Status | sort-object -property Name 
#$f5_host_Result 
$f5_host_out_yesterday+": file is Present!" 
$Text_Body = $f5_host+": difference `r`n" 
$Text_Body += ($f5_host_Result | out-string) 
Send-MailMessage -to [email protected] -from [email protected] -subject $f5_host+": F5 Daily LTM Check" -body $Text_Body -smtpserver blah 
}else{ 
$f5_host_out_yesterday+": is not file is Present!" 
Send-MailMessage -to [email protected] -from [email protected] -subject $f5_host+": Check failed" -body "Yesterday's file is not present" -smtpserver blah 
} 
} 

#Limit File retention to 30days. 
$limit = (Get-Date).AddDays(-30) 
#Get script location 
$path = Get-Location 

# Delete files older than the $limit. 
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force 
Verwandte Themen