我确定此脚本有很多错误-但是现在唯一引发任何类型错误的是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
时,要删除驱动器号后的':',我将得到以下$matches
:Name Value
---- -----
0 ...
所以,我完全不知所措。如果有人有任何见解,我将不胜感激。
最佳答案
用文件名设置变量,清理并使用
$fn = ($c).replace(" ","") + "_" + ($s).replace(" ","") + ".png"
$fp = $scriptPath + "\" + ($f -replace ("[{0}]" -f [RegEx]::Escape([IO.Path]::GetInvalidFileNameChars() -join '')))
$chart1.SaveImage($fp,"png")
关于mysql - PowerShell-保存路径中的无效字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49928961/