powershell one liner用于下载带凭据的文件

Qua*_*dah 1 windows powershell automation

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

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

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

但现在我需要提供适当的凭据从私人位置下载我的文件.我尝试了以下内容,但它不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

(我是powershell的新秀:/)

如何在单行中为DownloadFile方法提供正确的凭据?

还是有另一个更适合这份工作的命令吗?

谢谢.

Geo*_*dze 7

如果您使用的是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>
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)