循环文件并发送到Web服务

时间:2017-02-25 09:47:33

标签: powershell

我需要遍历文件目录(doc)并将它们发送到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
}

1 个答案:

答案 0 :(得分: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
}