2016-06-26 133 views
0

我試圖從powers9下載一個使用powershell的視頻系列,但是我在第一個文件上出現這個錯誤,我認爲它是管道字符,但即使使用替換語句它不工作測試路徑:路徑中的非法字符

function Get-Media 
{ 
    [CmdletBinding()] 
    param 
    (
     [Object] 
     $url, 
     [Object] 
     $title, 
     [Object] 
     $path 
    ) 

    $u = New-Object System.Uri($url) 
    $name = $title 
    $extension = [System.IO.Path]::GetExtension($u.Segments[-1]) 
    $fileName = $name + $extension 

    $fileName = $fileName -replace "’", '' 
    $fileName = $fileName -replace "\?", '' 
    $fileName = $fileName -replace ":", '' 
    $fileName = $fileName -replace '/', '' 
    $fileName = $fileName -replace ",", '' 
    $fileName = $fileName -replace '"', '' 
    $fileName = $fileName -replace '|', '' 
    $fileName = $fileName -replace '\#', '' 
    $fileName = $fileName -replace '-', '' 

    $fileName 

    if (Test-Path($fileName)) { 
     Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow 
    } 
    else 
    { 
     Invoke-WebRequest $url -OutFile (Join-Path -Path $path -ChildPath $fileName) 
    } 
} 

function Get-VideosFromFeed 
{ 
    [CmdletBinding()] 
    param 
    (
     [Object] 
     $feedUrl, 
     [Object] 
     $folder, 
     [Object] 
     $path 
    ) 

    $feed=[xml](New-Object System.Net.WebClient).DownloadString($feedUrl) 

    $downloadPath = (Join-Path -Path $path -ChildPath $folder) 

    if (Test-Path($downloadPath)) { 
     Write-Host 'Skipping folder, already exists' -ForegroundColor Yellow 
    } 
    else 
    { 
     New-Item -Path $downloadPath -ItemType directory -WarningAction SilentlyContinue 
    } 

    foreach($i in $feed.rss.channel.item) { 
     foreach($m in $i.group){ 
      foreach($u in $m.content ` 
        | Where-Object { ` 
          $_.url -like '*mid.mp4' ` 
         } | Select-Object -Property @{Name='url'; Expression = {$_.url}}, ` 
                @{Name='title'; Expression = {$i.title}}) 
      { 
       Get-Media -url $u.url -title $u.title -path $downloadPath 
      } 
     } 
    } 
} 

$physicalPath = "D:\Videos\Series" 

Get-VideosFromFeed -feedUrl 'https://channel9.msdn.com/Blogs/azurestack/feed/mp4high'                   -path $physicalPath -folder 'azurestack' 
+0

我不清楚發生錯誤的位置。請在你的問題中顯示確切的錯誤信息。 –

+0

錯誤是下載第一個視頻,複製粘貼整個代碼,你應該重現它 –

+0

@EstebanV你有沒有嘗試過我的建議? –

回答

2

我相信,你應該更換這個奇怪的一段代碼:

$fileName = $fileName -replace "’", '' 
$fileName = $fileName -replace "\?", '' 
$fileName = $fileName -replace ":", '' 
$fileName = $fileName -replace '/', '' 
$fileName = $fileName -replace ",", '' 
$fileName = $fileName -replace '"', '' 
$fileName = $fileName -replace '|', '' 
$fileName = $fileName -replace '\#', '' 
$fileName = $fileName -replace '-', '' 

有了這個:

$fileName = $fileName -replace '(-|#|\||"|,|/|:|â|€|™|\?)', '' 

我後面的工作嚴格的代理,並不能測試整個過程,但至少我沒有得到'路徑中的非法字符'錯誤。

順便說一句,你不需要更換所有的東西。只有這些字符:<>:"/\|?*根據naming rules。所以更好的正則表達式應該是這樣的:-replace '\<|\>|:|"|/|\\|\||\?|\*', ''

+0

將在今晚稍後測試,並讓你知道! –

1

它看起來像沒有$ Folder或$ Path參數調用Get-VideosFromFeed。

您可能需要設置一些默認值,或者在調用函數時將它們交給它們。

嘗試調用它像這樣:

Get-VideosFromFeed -feedUrl 'https://channel9.msdn.com/Blogs/azurestack/feed/mp4high' -folder "Folder" -Path "C:\Path" 
+0

它看起來像我沒有粘貼整行,參數在那裏,你嘗試運行整個腳本?你能告訴我它對你有效嗎?對於第一部影片,我得到上述錯誤 –

+0

我檢查過了,代碼沒問題,但你必須滾動到右邊,請複製並粘貼所有內容,它也會失敗你從rss源下載第一個視頻時發生錯誤。 Get-VideosFromFeed -feedUrl'https://channel9.msdn.com/Blogs/azurestack/feed/mp4high'-path $ physicalPath -folder'azurestack' –

1

這條線沒有做任何事情:

$fileName = $fileName -replace '|', '' 

到-replace第一個參數是一個正則表達式。它查找$ fileName中的每個空字符串,並用空字符串替換它。除了浪費一堆CPU週期之外,這不起作用。

您需要轉義正則表達式中的管道字符。

$fileName = $fileName -replace '\|', ''