我已经通过WebClient对象从Artifactory(Generic Repo)成功下载了一个文件.我通过相同的方法上传文件时遇到了麻烦.我试图找出通过Power
shell上传到我们服务器的最简单方法.
请注意,此时安装其他实用程序(如Curl)不是一个选项.我正在编写自动化脚本,并希望坚持使用基本的Windows 2008 r2服务器,不需要安装其他实用程序,因为我无法指望它们在所有服务器上都存在.
如果某人有一个使用Rest API的示例脚本,那将是完美的!
下载代码的示例(这适用):
$SOURCE = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_1.0.zip" $DESTINATION = ".\BF_1.0.zip" $AF_USER ="user" $AF_PWD ="password" $WebClient = New-Object System.Net.WebClient $WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER,$AF_PWD) $WebClient.DownloadFile($SOURCE,$DESTINATION)
$SOURCE = ".\BF_2.0.zip" $DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip" $AF_USER ="user" $AF_PWD ="password" $WebClient = New-Object System.Net.WebClient $WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER,$AF_PWD) $URI = New-Object System.Uri($DESTINATION) $WebClient.UploadFile($URI,$SOURCE)
Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (405) Method Not Allowed." At E:\transient\af_put.ps1:8 char:1 + $WebClient.UploadFile($URI,$SOURCE) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [],MethodInvocationException + FullyQualifiedErrorId : WebException
我尝试了Invoke-WebRequest选项,并且能够使其工作:
$URI = New-Object System.Uri("https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip") $SOURCE = ".\BF_2.0.zip" $AF_USER = "user" $AF_PWD = ConvertTo-SecureString "password" -AsPlainText -Force $CREDS = New-Object System.Management.Automation.PSCredential ($AF_USER,$AF_PWD) Invoke-WebRequest -Uri $URI -InFile $SOURCE -Method Put -Credential $CREDS