我将JSON文件的内容放入变量$variable中,并按如下代码中的条件过滤$变量,并将过滤后的数据保存在$logs中。echo $logs的输出如下:
0.842093077,
0.792955,
0.8910225,
0.8724875,
0.852885333,
0.774708,
0.987243333,
0.87078,
0.9565,
0.839393333
我的问题是如何将这些十进制值转换为百分比,如84.20%,79.20%等,以便当邮件发出时百分比利用率字段作为百分比值而不是十进制值?
期望的样本输出
Workspace Name : A NAME
Workspace Allowance : 100
Workspace Usage : 79.2955
Workspace Size Free : 20.7
**Percentage Utilization : 79.29**
Predicted Usage : 86.05585477
当前输出
Workspace Name : A NAME
Workspace Allowance : 100
Workspace Usage : 79.2955
Workspace Size Free : 20.7
*Percentage Utilization : 0.792955*
Predicted Usage : 86.05585477
# Convert the CSV file into its JSON equivalent
import-csv "Workspacesize.csv" | ConvertTo-Json | Add-Content -Path "output.json" -Force -ErrorAction Stop
# Import email settings from config file
[xml]$ConfigFile = Get-Content "Settings.xml"
#Write-Output $ConfigFile
$emailTo = @(abc@gmail.com, xyz@gmail.com)
# Create email content
$smtpsettings = @{
From = $ConfigFile.Settings.EmailSettings.MailFrom
Subject = "TEST EMAIL"
SmtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer
}
[String]$messagebody = ""
$variable=Get-Content -raw "output.json" | ConvertFrom-Json
$logs=$variable | Where-Object { [double]$_.'Percentage Utilization' -gt 0.75}
echo $logs
}
foreach ($log in $logs )
{
$messagebody = $messagebody + $log + "`r`n"
}
$messagebody = $logs | Out-String
$messagebody = '<pre>' + $messagebody + '</pre>'
#-------------------------------------------------
# Script
#-------------------------------------------------
try
{
Send-MailMessage @smtpsettings -To $emailTo -Body $messagebody -BodyAsHtml -Encoding utf8 -verbose -ErrorAction Stop
}
catch
{
Write-Warning $_.Exception.Message
}
# MOVE THE JSON & CSV FILE TO AN ARCHIVED LOCATION
Move-Item -Path $Jsonfile -Destination C:UsersSiddhartha.S.Das2Desktop -Force
Move-Item -Path $Csvfile -Destination C:UsersSiddhartha.S.Das2Desktop -Force
#-------------------------------------------------
# The End
#-------------------------------------------------
如前所述,您可以通过使用CSV文件中的数据来简化代码,只有当您确实还需要JSON格式时,才能转换该数据并将其保存在脚本末尾。
$CsvFile = 'C:SomewhereWorkspacesize.csv'
$data = Import-Csv -Path $CsvFile
$logs = $data | Where-Object { [double]$_.'Percentage Utilization' -gt 0.75 }
# update the 'Percentage Utilization' property on each item
foreach ($item in $logs) {
$item.'Percentage Utilization' = [Math]::Round(([double]$item.'Percentage Utilization' * 100), 2)
}
# format the mesage body as monospaced formatted list output
$messagebody = '<pre>{0}</pre>' -f ($logs | Format-List | Out-String)
# Import email settings from config file
[xml]$ConfigFile = Get-Content "Settings.xml"
# BTW. it is better to use:
# $ConfigFile = ([System.XML.XMLDocument]::new()).Load('X:PathToSettings.xml')
# because that way you'll get the encoding right.
# You need to load it using the absolute Full path and filename
# Create email splat hashtable
$smtpsettings = @{
To = 'abc@gmail.com', 'xyz@gmail.com'
From = $ConfigFile.Settings.EmailSettings.MailFrom
Subject = "TEST EMAIL"
SmtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer
}
# try to send the email
try {
Send-MailMessage @smtpsettings -Body $messagebody -BodyAsHtml -Encoding utf8 -Verbose -ErrorAction Stop
}
catch {
Write-Warning $_.Exception.Message
}
# move the csv file to an archived location
Move-Item -Path $Csvfile -Destination 'C:UsersSiddhartha.S.Das2Desktop' -Force
# if you really also need a json file from the data retrieved from the Csv, convert and save it here:
$data | ConvertTo-Json | Set-Content -Path 'C:UsersSiddhartha.S.Das2Desktopoutput.json'
百分比格式说明符p的快速示例,参见https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings。输入为[double]类型。
0.842093077,
0.792955,
0.8910225,
0.8724875,
0.852885333,
0.774708,
0.987243333,
0.87078,
0.9565,
0.839393333 | % tostring p
84.21%
79.30%
89.10%
87.25%
85.29%
77.47%
98.72%
87.08%
95.65%
83.94%