powershell:不支持给定路径的格式

时间:2020-07-19 15:43:43

标签: powershell

我不是程序员,但我正在尝试运行此powershell脚本来使用Internet Exporer(不是我的脚本)获取多个网站屏幕截图

任何人都可以帮我找出问题所在,因为我在堆纸堆里,不知道该怎么办。

提前thnx

# 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

任何人都可以帮我找出问题所在,因为我在堆纸堆里,不知道该怎么办。

提前thnx

4 个答案:

答案 0 :(得分:3)

注意:

  • 以下答案适用于 Windows

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


您已经确认,问题是输入CSV文件中的filename列值包含:(冒号),但是仅有文件 name 不能包含:

:仅作为驱动器规范 的一部分,仅在文件 path 中被允许strong>(例如C:

路径的任何其他部分中的:会触发您看到的异常,该异常在PowerShell中显示为 statement-termination 错误(错误消息在下面缩写);例如:

# On Windows
PS> [System.IO.StreamWriter]::new("C:\foo:bar")
"The given path's format is not supported."

相关的错误:如果您尝试使用文件名称中根本不受支持的字符,请特别注意以下一项:{{1} }(: * ? " < > | 也不能在文件名称中使用,只能用作 paths 中的分隔符);请注意,错误消息在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方法调用和对本机实用程序的调用。为了统一平台上的行为,PowerShell还允许使用/作为Unix上的路径分隔符,这可能会出现问题-请参见this GitHub issue

答案 1 :(得分:1)

您必须满足以下两个先决条件才能使此脚本起作用:

  1. 您在脚本存储位置需要一个名为urls.csv的CSV文件。该文件必须包含两行,用逗号分隔,标题为filenameurl。还要注意,文件名必须不带扩展名。示例:
filename,url
test,https://www.google.com
  1. 在此脚本的存储位置,您需要一个名为“结果”的文件夹。然后,您将在此文件夹中找到屏幕截图。

答案 2 :(得分:0)

您可能必须先创建Results目录以保存屏幕截图,我进行了测试并创建了目录,以解决问题

更正:问题出在csv提供的文件名输入中,其中包含像':'这样的字符,Windows中的文件名不支持

下面由@ mklement0给出的详细答案

答案 3 :(得分:0)

如果在powershell中遇到路径错误,请使用以下脚本:将路径基本分为3部分。

$sSecStrPassword = "Come up with something secure!";
$FilePath = "c:\a\";
$FileName = "mycert";
$FileType = ".pfx";
$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificateObject.Import($FilePath+$FileName+$FileType, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet);
return $certificateObject.Thumbprint;