从Windows Powershell脚本上载到Artifactory

前端之家收集整理的这篇文章主要介绍了从Windows Powershell脚本上载到Artifactory前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经通过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

不得不创建一个PSCrendential对象,因此它不会提示用户密码.但除此之外,这项工作正如我所需要的那样.

猜你在找的Windows相关文章