我目前正在尝试指示新创建的 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/