2017-02-25 90 views
0

我已經搜索了這個,但我一直在搞這個。 我認爲它應該很容易,但從來沒有在PowerShell中做任何事情沒有幫助:)循環槽文件併發送到網絡服務

我需要循環槽文件目錄(文檔),並將它們發送到Web服務,獲取一個PDF文件(與doc相同的名稱)。

爲一個文件,這個工程:

$MyInvocation.MyCommand.Path | Split-Path | Push-Location 

$uri = "http://someinternalurl" 
$doc= "somefilename.doc" 
$pdf= "somefilename.pdf" 

Invoke-WebRequest -Uri $uri -Method POST -InFile $doc-ContentType "application/octet-stream" -OutFile $pdf 

但有循環我一直搞亂了,我目前正試圖:

$files = get-childitem c:\in\doc -Filter *.doc 
ForEach ($file in $files) { 
    $MyInvocation.MyCommand.Path | Split-Path | Push-Location 

    $uri = "http://someinternalurl" 
    $doc = Get-Content $file.Fullname ## <<-- this is where i'm surely wrong 
    $pdf = $file.Basename + '.pdf' 

    Invoke-WebRequest -Uri $uri -Method POST -InFile $in -ContentType "application/octet-stream" -OutFile $pdf 
} 

任何人都願意伸出援助之手?

回答

0
$DocPath = 'c:\in\doc' 
$Uri = 'http://someinternalurl' 

Get-ChildItem -Path $DocPath -Filter '*.doc' | ForEach-Object { 
    $PdfPath = Join-Path -Path $_.Directory.FullName -ChildPath ($_.BaseName + '.pdf') 
    Invoke-WebRequest -Uri $Uri -Method POST -InFile $_.FullName -ContentType 'application/octet-stream' -OutFile $PdfPath 
} 
+0

謝謝,這經歷了沒有錯誤,最終結果是一個PDF的正確名稱。不幸的是,它沒有通過Web服務正確處理,結果是一個pdf文件,其中包含純文本內容的文件,但仍然遠遠超過我現在得到的。 – Crimson

相關問題