2016-01-31 32 views
0

我正在嘗試使用文件參數創建一個Jenkins參數化版本。Jenkins參數化版本 - 在powershell中讀取文件

用戶將選擇帶參數的構建,並將提供一個帶有項目列表(每行一個)的文本文檔。

我想嘗試閱讀我的PowerShell腳本中的這個列表,然後關閉並撥打一些服務電話(我有點疑惑)。

我和讓我的PowerShell腳本來讀取已穿入詹金斯文件掙扎,這裏是我迄今爲止

$ids = Import-Csv input.txt 

$array = @() 

Write-Host $array.Length 

foreach($id in $ids){ 
    Write-Host $id.id 
    $array += $id 

    Write-Host "Array now has" $array.Length "items" 
} 

我知道腳本運行正常,因爲我可以運行Powershell ISE並傳入csv/txt文件的絕對路徑。

作爲參考,input.txt中的內容是這樣的:

id 
2884430041011214, 
9751297519392363, 
lfsdkjgskdjflgsdjfg 

這裏有兩個輸出我得到

Powershell的ISE(如預期):

0 
2884430041011214 
Array now has 1 items 
9751297519392363 
Array now has 2 items 
lfsdkjgskdjflgsdjfg 
Array now has 3 items 

詹金斯(不是我所希望的)

Started by user anonymous 
Building in workspace C:\Users\jonec34\.jenkins\jobs\getParty\workspace 
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\jonec34\AppData\Local\Temp\hudson4035619350462822370.ps1'" 
Import-Csv : Could not find file 'C:\Users\jonec34\.jenkins\jobs\getParty\workspace\input.txt'. 
At C:\Users\jonec34\AppData\Local\Temp\hudson4035619350462822370.ps1:1 char:8 
+ $ids = Import-Csv input.txt 
+  ~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OpenError: (:) [Import-Csv], FileNotFoundException 
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand 

0 
Finished: SUCCESS 

任何人都可以告訴我我該如何做到這一點。我也嘗試在腳本的開頭放置Param($ file),並使用$ {WORKSPACE},但是它們不能正常工作,或者我沒有正確理解它。任何幫助不勝感激。

感謝

回答

0

權,我發現我要去哪裏錯了...

在我的文件參數,我沒有設置文件位置(例如我已經離開它留空)但我輸入'\ input.txt'和低,並看到我得到

Building in workspace C:\Users\jonec34\.jenkins\jobs\getParty\workspace 
Copying file to \input.txt 
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\jonec34\AppData\Local\Temp\hudson6216657038417634126.ps1'" 
getting requests from file 
0 
2884430041011214 
Array now has 1 items 
9751297519392363 
Array now has 2 items 
lfsdkjgskdjflgsdjfg 
Array now has 3 items 
Finished: SUCCESS