我想用批处理文件执行 powershell 代码来从我的桌面截取屏幕截图,我在这里找到了它使用 PowerShell 截取用户桌面的屏幕截图。
因此,当我将其作为 powershell 脚本执行时,它可以正常工作,并且对我来说 5/5 有效,但是当我将它作为批处理文件时,我收到了这种类型的错误,我不知道如何正确调试它?
Error "TerminatorExpectedAtEndOfString"
因此,如果有人可以向我解释为什么以及从何处出现此错误?
这是我的批处理代码:
@echo off
Title Get a ScreenShot with Batch and Powershell
Call :ScreenShot
pause
::----------------------------------------------------------------------------------------------------------------------------
:ScreenShot
Powershell ^
$Path = "E:\ScreenCapture\";^
Add-Type -AssemblyName System.Windows.Forms;^
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;^
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height);^
$graphic = [System.Drawing.Graphics]::FromImage($image);^
$point = New-Object System.Drawing.Point(0,0);^
$graphic.CopyFromScreen($point, $point, $image.Size);^
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size);^
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds);^
$FileName = (Get-Date -F dd-MM-yyyy_HH_mm_ss)+".jpg";^
$FilePath = $Path$FileName;^
$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;^
$image.Save($FilePath,$FormatJPEG)
Exit /B
::------------------------------------------------------------------------------------------------------------------------
答案1
也许你需要:
用于
^
)
替换
"
为'
$var='value'
使用 To.String
$((Get-Date).ToString('dd-MM-yyyy_HH_mm_ss')+'.jpg')
在命令行中测试它:
Powershell $Path='E:\ScreenCapture\'; Add-Type -AssemblyName System.Windows.Forms;$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height^);$graphic = [System.Drawing.Graphics]::FromImage($image^);$point = New-Object System.Drawing.Point(0,0^);$graphic.CopyFromScreen($point, $point, $image.Size^);$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size^);[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds^);$FileName = $((Get-Date^).ToString('dd-MM-yyyy_HH_mm_ss'^)+'.jpg'^);$FilePath = $Path+$FileName;$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;$image.Save($FilePath,$FormatJPEG^)
在你的 bat 文件中测试它:
@echo off
Title Get a ScreenShot with Batch and Powershell
Call :ScreenShot
pause
goto :eof
::----------------------------------------------------------------------------------------------------------------------------
:ScreenShot
Powershell $Path='E:\ScreenCapture\';^
Add-Type -AssemblyName System.Windows.Forms;^
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;^
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height^);^
$graphic = [System.Drawing.Graphics]::FromImage($image^);^
$point = New-Object System.Drawing.Point(0,0^);^
$graphic.CopyFromScreen($point, $point, $image.Size^);^
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size^);^
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds^);^
$FileName = $((Get-Date^).ToString('dd-MM-yyyy_HH_mm_ss'^)+'.jpg'^);^
$FilePath = $Path+$FileName;$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;^
$image.Save($FilePath,$FormatJPEG^)
答案2
错误来自这一行:
$FilePath = $Path$FileName;^
你不应该连接变量来形成这样的文件路径。
它会如果你用双引号括起来,就像这样:$FilePath = "$Path$FileName";^
,但更好的方法是使用连接路径命令:
$FilePath = Join-Path -Path $Path -ChildPath $FileName;^
或者如果你愿意直接使用.Net:
$FilePath = [System.IO.Path]::Combine($Path, $FileName);^