powershell-保存路径中的字符无效

j13ufse2  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(394)

我确信这个脚本有很多错误,但是现在唯一会引发错误的是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                              ...

所以,我完全不知所措。如果有人有任何见解,我将不胜感激。

5tmbdcev

5tmbdcev1#

用文件名设置变量,清除并使用

$fn = ($c).replace(" ","") + "_" + ($s).replace(" ","") + ".png"
$fp = $scriptPath + "\" + ($f -replace ("[{0}]" -f [RegEx]::Escape([IO.Path]::GetInvalidFileNameChars() -join '')))
$chart1.SaveImage($fp,"png")

相关问题