Reputation: 43
i am not a programmer but i am trying to run this powershell script to get multiple website screenshots with internet exporer ( not my script )
can anyone help to find out what is wrong with it because i am stack and i dont know what to do.
thnx in advance
# 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!";
and i am get this error
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
can anyone help to find out what is wrong with it because i am stack and i dont know what to do.
thnx in advance
Upvotes: 4
Views: 21499
Reputation: 119
If you get path error in powershell, use below script: Basically divide your path into 3 parts.
$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;
Upvotes: 0
Reputation: 440501
Note:
The following answer applies to Windows.
Unix-like platforms (supported by PowerShell [Core] v6+) have different rules; notably, there are no drive letters[1], and :
is therefore a legal character in a file name; except for /
, which is invariably the path separator, there are virtually no restrictions on what characters you may use in a file name, including \
.[2]
As you've since confirmed, the problem was that a filename
column value in your input CSV file contained a :
(colon) character, but a mere file name isn't allowed to contain :
:
is only allowed in a file path, once, as part of a drive specification (e.g., C:
)
A :
in any other part of a path triggers the exception you saw, which surfaces as a statement-terminating error in PowerShell (error message abbreviated below); e.g.:
# On Windows
PS> [System.IO.StreamWriter]::new("C:\foo:bar")
"The given path's format is not supported."
A related error shows if you try to use characters that are fundamentally unsupported in file names, notably one of: : * ? " < > |
(\ /
too cannot be used in file names, only as separators in paths); note that the error message changed in 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."
The above errors are both due to invalid file-system path syntax.
By contrast, if the path is syntactically valid but the target directory doesn't exist, you'll see yet another error:
# On Windows.
PS> [System.IO.StreamWriter]::new("NoSuchSubdir\file.txt")
"Could not find a part of the path '...'."
With /
as the separator, you'd see the same error on Unix-like platforms.
[1] Paths based on PowerShell-only drive definitions (created with New-PSDrive
) can have drive specifications on Unix too, but you cannot pass such paths to .NET methods (such as in this case), because .NET doesn't understand them.
[2] This applies to .NET method calls and calls to native utilities; PowerShell, in an attempt to unify behavior across platforms, allows use of \
as the path separator on Unix as well, which can be problematic - see this GitHub issue.
Upvotes: 4
Reputation: 26
You may have to create Results directory first to save the screenshots, I tested and creating the directory solved the problem
Correction: Issue is in the filename inputs supplied from csv, which contain the characters like ':' which are not supported for filename in windows
Detailed answer given below by @mklement0
Upvotes: 0
Reputation: 13588
You have to meet the following two prerequisites in order to make this script work:
urls.csv
at the location where your script is stored. This file has to contain two comma-delimited columns with headers filename
and url
. Also note, that the filename has to be provided without an extension. Example:filename,url
test,https://www.google.com
Upvotes: 1