2015-08-13 41 views
0

我想我已經從腳本的其餘部分得到了我需要的東西,我只需要幫助閱讀XML文件,然後通過腳本的其餘部分管道化它。如果有人能指出我正確的方向,我將不勝感激。提示和閱讀XML文件

param([string] $file) 

try { 
    $ErrorActionPreference = "Stop" 
    $xml = [xml](Get-Content $file) 
} catch [System.Management.Automation.ItemNotFoundException] { 
    Write-Host "File not found." 
} catch { 
    Write-Host "Please enter a file path." 
} 

回答

0

使用讀 - 主機獲得來自用戶的輸入,用一個循環來確保路徑存在,就像這樣:

param([string] $file) 

try { 
    $ErrorActionPreference = "Stop" 
    $xml = [xml](Get-Content $file) 
} catch [System.Management.Automation.ItemNotFoundException] { 
    Write-Host "File not found." 
} catch { 
    Do 
    { 
    $XMLFile = Read-Host "Please enter a file path" 
    } 
    until (Test-Path $XMLFile) 
    $xml = [xml](Get-Content $XMLFile) 
} 
+0

得到:術語「獲得」不被識別爲cmdlet的名稱,功能,腳本文件或可操作程序。檢查名稱的拼寫,或者如果包含路徑 ,請驗證路徑是否正確,然後重試。 在C:\ Users \ Administrator \ Desktop \ Manage-Users.ps1:14 char:18 + $ xml = [xml](Get - Content XMLFile) + ~~~ + CategoryInfo:ObjectNotFound:(Get:String )[],ParentContainsE rrorRecordException + FullyQualifiedErrorId:CommandNotFoundException – CaptainTeancum

+0

再次檢查此問題,此法術中存在錯誤... – Avshalom