我确定这个脚本有很多问题 - 但是现在唯一能引发任何错误的是SaveImage部分。因此,脚本将从MySql查询中提取结果,并使用该信息来制作图表。除了它不会保存图表。
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
$scriptPath = "C:\Users\Public\reports\img"
Connect-MySqlServer -ComputerName [SERVER] -Port 3306 -Database [DB] -Credential (Get-Credential)
$locations = Invoke-MySqlQuery "[...]"
ForEach ($location in $locations) {
$loc = $location.locationID
$dataSource = Invoke-MySqlQuery "[...]"
$c = (Invoke-MySqlQuery "[...]").name | Out-String
$s = (Invoke-MySqlQuery "[...]").name | Out-String
$chart1 = New-Object System.Windows.Forms.DataVisualization.Charting.Chart
$chart1.Width = 600
$chart1.Height = 400
$chart1.BackColor = [System.Drawing.Color]::White
[void]$chart1.Titles.Add("Test Chart")
$chart1.Titles[0].Font = "Segoe UI,13pt"
$chart1.Titles[0].Alignment = "topLeft"
$chartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$chartArea.Name = "ChartArea1"
$chartArea.AxisY.Title = "Y Axis"
$chartArea.AxisX.Title = "X Axis"
$chartArea.AxisY.Interval = 5
$chartArea.AxisX.Interval = 1
$chartArea.AxisY.MaximumAutoSize = $false
$chartArea.AxisX.MaximumAutoSize = $false
$chartArea.AxisY.Maximum = 80
$chartArea.AxisX.Maximum = 30
$chart1.ChartAreas.Add($chartArea)
$legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$legend.Name = "Legend1"
$chart1.Legends.Add($legend)
[void]$chart1.Series.Add("U")
$chart1.Series["U"].ChartType = "Line"
$chart1.Series["U"].BorderWidth = 3
$chart1.Series["U"].IsVisibleInLegend = $True
$chart1.Series["U"].ChartArea = "ChartArea1"
$chart1.Series["U"].Legend = "Legend1"
$chart1.Series["U"].Color = "#7CB5EC"
ForEach ($point in $dataSource) {$chart1.Series["U"].Points.addxy($($point.t),[math]::Round(($point.u) / 1024),2))}
[void]$chart1.Series.Add("D")
$chart1.Series["D"].ChartType = "Line"
$chart1.Series["D"].BorderWidth = 3
$chart1.Series["D"].IsVisibleInLegend = $True
$chart1.Series["D"].ChartArea = "ChartArea1"
$chart1.Series["D"].Legend = "Legend1"
$chart1.Series["D"].Color = "#FFA500"
ForEach ($point in $dataSource) {$chart1.Series["D"].Points.addxy($($point.t),[math]::Round(($($point.d) / 1024),2))}
$filename = $scriptPath + "\" + ($c).replace(" ","") + "_" + ($s).replace(" ","") + ".png"
$filename = ($filename).replace("`n","")
$chart1.SaveImage("$filename","png")
}
返回:
Exception calling "SaveImage" with "2" argument(s): "Illegal characters in path."
At line:62 char:1
+ $chart1.SaveImage("$filename","png")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
我认为它与Out-String
和$c
查询中使用的$s
有关,但当我不这样做时,我得到了其他问题。 $filename
没有.replace
返回:
C:\Users\Public\reports\img\$c
_$s
.png
因此,我使用以下内容检查$filename
是否包含无效字符:
$pattern = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidFileNameChars() -join '' ))
$filename -match $pattern
$matches
返回:
True
Name Value
---- -----
0 :
当我cd
到目录,并尝试将文件保存为$filename
而没有$scriptPath
时,摆脱':'在Drive Letter之后,我得到以下$matches
:
Name Value
---- -----
0 ...
所以,我完全不知所措。如果有人有任何见解,我会非常感激。
答案 0 :(得分:0)
使用文件名设置变量,清理它并使用
$fn = ($c).replace(" ","") + "_" + ($s).replace(" ","") + ".png"
$fp = $scriptPath + "\" + ($f -replace ("[{0}]" -f [RegEx]::Escape([IO.Path]::GetInvalidFileNameChars() -join '')))
$chart1.SaveImage($fp,"png")