PowerShell用法-文件上传目录

时间:2019-06-03 02:12:25

标签: powershell

我希望获得有关HTTPS文件上传的PowerShell使用方面的帮助。我当前的任务是自动将PDF文件目录从本地网络路径复制到HTTPS://网站。虽然我在网络服务器上有一个本地帐户,但是在文件上传和重新列出所有PDF列表时遇到了问题。

我尝试了Invoke-RestMethod -URI

$sourceFilePath = Get-ChildItem "\\DFS-Netwrok\PDF" -Recurse -Include *.pdf
$siteAddress = "https://contoso.com/PDF"; 
$urlDest = "{0}/{1}" -f ($siteAddress, "*.pdf"); 
$UserName = "UserName" 
$Password = "Password" 

function uploadFile() { 
    Param ([string] $sourceFilePath, 
             [string] $siteAddress , 
             [string] $urlDest,
             [string] $UserName, 
             [string] $Password) 
    $webClient = New-Object System.Net.WebClient;
    $webClient.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password);

     ("*** Uploading {0} file to {1} ***" -f ($sourceFilePath, $siteAddress) ) | write-host -ForegroundColor Green 

    $webClient.UploadFile($urlDest, "PUT", $sourceFilePath); 

    $httpresponse = $httprequest.GetResponse() 
} 

Upload-File -File $SourceFilePath -URI $SiteAddress -Dest $URLDest -Username $MyUsername -Password $MyPassword

当前我在Upload-File上遇到错误。

我很高兴转换为可以提供上述任务的任何代码,使用用户/密码从内部路径列出连接到网站的PDF,并从本地目录上载所有PDF。我可以确认该帐户可以连接并且对目标网站URL拥有正确的权限。

我也在看这个例子,错误为“禁止”:

$Dir="\\Netwrok\Path"
$Url = "https://Website/PDF"
$user = "User"
$pass = "Password"
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)

foreach($item in (dir $Dir "*.pdf"))
{ 
    "Uploading $item..."
    $uri = New-Object System.Uri($Url+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName)  
}

返回错误:

  

使用“ 2”作为参数调用“ UploadFile”的异常:“远程服务器   返回错误:(403)禁止。“
  在第16行char:22
  + $ webclient.UploadFile($ uri,$ item.FullName)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo:未指定:(:) [],MethodInvocationException
  + FullyQualifiedErrorId:WebException

0 个答案:

没有答案