windows - powershell one liner 下载带有凭据的文件

标签 windows powershell automation

我目前正在尝试指示新创建的 Windows shell 通过 autounattend.xml 文件下载 powershell 脚本。使用这种技术,我需要一个衬垫来完成工作。

我曾经能够使用这个单行从公共(public)环境下载它们:

<Path>powershell -NoLogo -Command "((new-object System.Net.WebClient).DownloadFile('https://some.remote.public.location/myfile.ps1', 'c:\Windows\Temp\myfile.ps1')"</Path>

但现在我需要提供适当的凭据才能从私有(private)位置下载我的文件。我尝试了以下但它不起作用:

<Path>powershell -NoLogo -Command "((new-object System.Net.WebClient).Credentials = new-object System.Net.NetworkCredential("user123", "password123")).DownloadFile('https://some.remote.private.location/myfile.ps1', 'c:\Windows\Temp\myfile.ps1')"</Path>

(我是powershell的菜鸟:/)

我怎样才能在一行中为 DownloadFile 方法提供正确的凭据?

或者还有其他更适合这项工作的命令吗?

谢谢。

最佳答案

如果您使用的是 PowerShell 3.0 或更新版本,您可以试试这个:

<Path>powershell -NoLogo -Command "Invoke-WebRequest -Uri 'https://some.remote.private.location/myfile.ps1' -OutFile 'c:\Windows\Temp\myfile.ps1' -UseBasicParsing -Credential (New-Object PSCredential('user123', (ConvertTo-SecureString -AsPlainText -Force -String 'password123')))"</Path>

在 PowerShell 2.0 中:

<Path>powershell -NoLogo -Command "$webClient = new-object System.Net.WebClient; $webClient.Credentials = new-object System.Net.NetworkCredential('user123', 'password123'); $webClient.DownloadFile('https://some.remote.private.location/myfile.ps1', 'c:\Windows\Temp\myfile.ps1')"</Path>

关于windows - powershell one liner 下载带有凭据的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45067157/

相关文章:

c++ - Qt 小部件在其他非 qt 窗口之上

windows - 在 Python 3 中使用 PythonMagick 进行蒙太奇?

windows - 使用 Start-Process 时禁止打开命令窗口

java - Selenium 网络驱动程序 : How can I click these links?

c# - Outlook 是否在其电子邮件元素中嵌入了 MessageID 或等效项?

windows - 没有网络访问权限的虚拟服务帐户,如 NT AUTHORITY\LocalService

windows - 如何在没有服务器的情况下在本地测试我的 ssh-keys

powershell - 在 Powershell 中设置与标准颜色不同的背景颜色

powershell - 使用 2 个 cmdlet 的输出选择对象

Java 和 CANopen