簡體   English   中英

通過使用批處理文件調用.ps1文件在Powershell中使用Invoke -WebRequest下載文件

[英]Download a file using Invoke -WebRequest in Powershell by calling a .ps1 file using a batch file

我正在嘗試執行類似的操作,我必須使用powershell來下載一些文件,該腳本已在hardrive的“ myscript.ps1”文件中編寫,並使用powershell調用-WebRequest cmdlet。現在,我希望通過以下命令使用cmd在Powershell中調用並執行該文件:用於在Powershell中調用Powershell文件的批處理文件該怎么辦?

我不是無禮的意思,但是您根本沒有看過嗎? 還是先搜索Stackoverflow尋找類似問題? 似乎您所要問的是如何運行PowerShell腳本。 我在Google上搜索了一下,並得到了一些關於該主題的非常好的書面指南。

  1. Set-ExecutionPolicy -ExecutionPolicy不受限制

取決於環境,這可能不是您想要的,因為您基本上是在關閉針對未授權腳本的安全性。

  1. Powershell.exe-文件“ myscript.ps1”

http://windowsitpro.com/powershell/running-powershell-scripts-easy-1-2-3

如果您只想下載文件,則無需使用.ps1文件。
例如,要下載MAC地址供應商列表,我可以批量使用:

powershell -nop -command "iwr \"http://standards-oui.ieee.org/oui.txt\" -Outfile \"%temp%\oui.txt\""

我不會永久改變ExecutionPolicyunrestricted ,無論是使用RemoteSigned或包括-ExecutionPolicy Bypass在上述命令。

出於調試目的,請打開提升的PowerShell窗口以查看所有輸出流(尤其是警告和錯誤)。

為此,請使用-NoExit參數

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoExit -NoProfile -ExecutionPolicy Bypass -File ""D:\myscript.ps1""' -Verb RunAs}"

pause功能-Command ""D:\\myscript.ps1;pause""代替-File ""D:\\myscript.ps1""

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command ""D:\myscript.ps1;pause""' -Verb RunAs}"

請注意,如果D:\\myscript.ps1文件不存在,則上述任何方法都將無效。 此外,在調用的腳本終止錯誤的情況下,我不確定pause方法的有效性。

無需將Powershell作為單獨的進程啟動。 只需使用&pathToScript.ps1執行腳本。 這是下載並運行Powershell腳本的示例:

$chocoInstallScriptUrl  = 'http://.../install/Install.Choco.ps1'
$chocoInstallScript     = Join-Path $env:Temp 'Install.Choco.ps1'

Set-ExecutionPolicy Unrestricted

$downloader = New-Object System.Net.WebClient
$downloader.DownloadFile($chocoInstallScriptUrl, $chocoInstallScript)

& $chocoInstallScript

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM