我试着使用this post的代码。
当我直接在PowerShell终端上使用这段代码时,它可以正常运行。
Add-Type -AssemblyName presentationCore
$filepath = "C:\Temp\test\Wildlife.wmv"
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2
$duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
$wmplayer.Close()
start playing
$proc = Start-process -FilePath wmplayer.exe -ArgumentList $filepath -PassThru但是当我在.bat文件上运行代码时,一个命令窗口在几秒钟内出现和消失,并且没有进一步的操作。
如果我在CMD上运行.bat文件,会出现以下错误:
.bat文件中插入的代码为:
Add-Type -AssemblyName presentationCore
$filepath = [uri] "C:\Users\??????\Desktop\small.mp4"
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2 # This allows the $wmplayer time to load the audio file
$duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds
$wmplayer.Play()
Start-Sleep $duration
$wmplayer.Stop()
$wmplayer.Close()如果你能帮我解决这个问题,我将不胜感激。
谢谢。
发布于 2017-02-28 18:51:12
您正试图在.bat文件中运行PowerShell命令(结果是PowerShell引擎未用于执行代码,因此命令失败)。
您需要将脚本另存为.ps1文件,然后从命令行通过脚本的完整路径名或通过切换到脚本所在的目录并输入:
.\scriptname.ps1其中scriptname是保存文件的名称。
如果您希望通过.bat文件执行脚本,您仍然需要将其保存为.ps1,然后创建包含以下内容的.bat文件:
Powershell.exe -File C:\path\to\my\script\myscript.ps1明显地相应地修正了路径。注以这种方式运行脚本没有好处,但您可能使用.bat文件的一个原因是,如果您需要更改执行策略以允许脚本执行(我认为您在这种情况下不会这样做),如下所示:
Powershell.exe -executionpolicy unrestricted -File C:\path\to\my\script\myscript.ps1https://stackoverflow.com/questions/42495060
复制相似问题