【问题标题】:Powershell array of list objects列表对象的 Powershell 数组
【发布时间】:2016-08-06 17:27:59
【问题描述】:

我正在尝试编写一个脚本,该脚本采用 f5 LTM 结果的文本文件并将其放入可搜索的数组中,以便我可以比较昨天和今天的结果。

这是一个文件示例;

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

所以理想情况下,我想让 Name 成为唯一字段并对列表进行排序,以便我可以比较从昨天到今天的状态变化。

这是我正在处理的通过电子邮件发送结果的代码,但它只提供逐行差异,我宁愿在电子邮件中获取对象更改。

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 simon.thomason@racq.com.au -from simon.thomason@racq.com.au -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 simon.thomason@racq.com.au -from simon.thomason@racq.com.au -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

因此,作为输出,我只想在电子邮件中看到类似的内容

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

【问题讨论】:

  • 请告诉我,您实际上并没有在脚本中包含 F5 的登录信息。
  • 一点也不。这是一个测试虚拟机。生产 f5 具有单因素登录。
  • 好的,只要确定一下,以防您现在需要更改 F5 登录 :)
  • 如果您想每天比较对象的属性,请尝试使用export-clixml 保存它们并使用import-clixml 加载它们,这样您就可以使用实际对象而不仅仅是字符串数组。
  • 这是我的其他问题之一,我如何存储密码的哈希值,所以我不需要以纯文本形式存储它。

标签: arrays powershell f5


【解决方案1】:

好的,关于你的第二个问题,导出和导入密码,加密是按用户完成的(我很确定每台机器),所以你不能导出它,然后让另一个帐户导入它,但是对于只需直接保存加密密码即可使用以下功能:

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 TestP@ssw0rd 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

【讨论】:

    【解决方案2】:

    不确定是对还是错,但这给出了我正在寻找的结果。 json 刚刚被使用,所以我可以存储对象并将其转换回 powershell 对象。

    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 y@x -from y@x -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 y@x -from y@x -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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2012-02-07
      • 2017-11-19
      相关资源
      最近更新 更多