gpt4 book ai didi

mysql - PowerShell - 保存路径中的无效字符

转载 作者:行者123 更新时间:2023-11-30 21:44:53 25 4
gpt4 key购买 nike

我敢肯定这个脚本有很多问题 - 但现在唯一会引发任何类型错误的是 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

我假设它与用于 $c$s 查询的 Out-String 有关,但是当我不这样做,我遇到其他问题。没有 .replace$filename 返回:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com