电源外壳 : The given path's format is not supported

标签 powershell

我不是程序员,但我正在尝试运行此 powershell 脚本以使用 Internet exporer 获取多个网站截图(不是我的脚本)
任何人都可以帮助找出问题所在,因为我是堆栈,我不知道该怎么做。
提前谢谢

# Author: Shawn Keene
# Date: 09/12/2019
# Powershell script to automate Internet Explorer and grab screenshots

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
cd $ScriptPath

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")

function screenshot([Drawing.Rectangle]$bounds, $path) {
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)

$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)

$bmp.Save($path)

$graphics.Dispose()
$bmp.Dispose()
}

$bounds = [Drawing.Rectangle]::FromLTRB(4, 110, 1580, 1584)      #area for screenshot

cls
Write-Host -ForegroundColor Yellow "Starting... Move or minimize this window."
sleep 3                                                          #give the user 3 seconds to minimize 
or move the powershell window out of the way

$iterator = 1;
$OutputFile = "screenshot-log.log"                               #just in case we need to debug 
something later

$products = import-csv "urls.csv" | foreach {                    #load a 2-column CSV with headers of 
"filename" (for picture file) and "url" to visit

$ie = New-Object -com internetexplorer.application;          #initialize a fresh browser window for 
each row of the CSV
$ie.top = 0;
$ie.left = -8;                                               #because windows 10 has an invisible 
grab borders unless you use AeroLite theme
$ie.height = 1600;
$ie.width = 1600;
$ie.visible = $true;

$ie.navigate("about:blank");
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1};

$startTime = Get-Date                                       #start the stopwatch and log it
write-host $iterator.ToString() `t $_.url

$ie.navigate($_.url);                                       #go to the website and wait for browser 
to become idle

while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1};
sleep 3                                                     #wait at least 3 seconds and then check 
for idle one more time to be sure
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1};

$endTime = get-date                                         #end timer

$filepath = "Results\" + $_.filename + ".png"               #place screenshots into the Results 
subfolder of the current working directory

screenshot $bounds $scriptpath\$filepath                    #say cheese

#log the result to the log file
$iterator.ToString() + "`t" + $_.filename + "`t" + $_.url + "`t" + $startTime.DateTime + "`t" + 
($endTime - $startTime).TotalSeconds + " seconds" >> $outputFile

$ie.quit();                                                 #close the browser process

$iterator = $iterator+1;
}

Write-Host -ForegroundColor Green "All Done!";
我得到这个错误
Exception calling "Save" with "1" argument(s): "The given path's format is not supported."
At line:7 char:4
+    $bmp.Save($path)
+    ~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
任何人都可以帮助找出问题所在,因为我是堆栈,我不知道该怎么做。
提前谢谢

最佳答案

笔记:

  • 以下回答适用于 Windows .
  • 类 Unix 平台(由 PowerShell [Core] v6+ 支持)有不同的规则;值得注意的是,没有驱动器号 [1] 和 :因此是文件名中的合法字符;除了 / ,它始终是路径分隔符,对于文件名中可以​​使用的字符几乎没有任何限制,包括 \ .[2]

  • 正如您已经确认的那样,问题在于 一个 filename输入 CSV 文件中的列值包含 : (冒号)字符 , 但是 一个单纯的文件名不允许包含:
    :只允许在文件路径中,一次,作为驱动器规范的一部分 (例如,C:)
    一个 :在路径的任何其他部分触发您看到的异常,该异常在 PowerShell 中显示为语句终止错误(错误消息缩写如下);例如。:
    # On Windows
    PS> [System.IO.StreamWriter]::new("C:\foo:bar")
    "The given path's format is not supported."
    

    一个 相关错误 显示您是否尝试使用 文件名中根本不支持的字符 ,特别是以下之一:: * ? " < > | (\ / 也不能用于文件名,只能用作路径中的分隔符);请注意,PowerShell [Core] v6+ 中的错误消息已更改:
    # Windows PowerShell
    PS> [System.IO.StreamWriter]::new("foo?bar")
    "Illegal characters in path."
    
    # PowerShell [Core] v6+ on Windows
    PS> [System.IO.StreamWriter]::new("foo?bar")
    "The filename, directory name, or volume label syntax is incorrect."
    

    以上错误都是由于无效的文件系统路径语法 .
    相比之下,如果路径在语法上有效但目标目录不存在 ,你会看到另一个错误:
    # On Windows.
    PS> [System.IO.StreamWriter]::new("NoSuchSubdir\file.txt")
    "Could not find a part of the path '...'."
    
    /作为分隔符,您会在类 Unix 平台上看到相同的错误。

    [1] 基于仅 PowerShell 驱动器定义的路径(使用 New-PSDrive 创建)在 Unix 上也可以具有驱动器规范,但您不能将此类路径传递给 .NET 方法(例如在本例中),因为 .NET 不了解他们。

    [2] 这适用于 .NET 方法调用和对 native 实用程序的调用;为了统一跨平台的行为,PowerShell 允许使用 \作为 Unix 上的路径分隔符,这可能有问题 - 请参阅 this GitHub issue .

    关于电源外壳 : The given path's format is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62982408/

    相关文章:

    .net - 通过 NuGet 提供命令行工具以在 Visual Studio 中的生成后事件中使用

    xml - 使用PowerShell在XML代码中添加行

    powershell - 从PowerShell执行SQL * Plus

    未显示 Windows 10 分配的访问侧加载应用程序

    python - 第一次尝试从 Powershell 运行 python。我使用的命令是否正确/我需要做什么才能允许使用 Python?

    PowerShell 复制失败且没有警告

    Powershell:脚本 block 中的 & 问题

    powershell - 通过 Graph 更新 AzureAD/O365 UPN

    Powershell - 将文件夹的结构导出到 txt 或 xls,但只能导出 3 个文件夹的深度且没有文件

    powershell - 具有一个条目的多数组[重复项]