2017-09-01 80 views
-1

我試着做從PowerShell的腳本來讀取像這樣的文本文件:PowerShell腳本獲得變量文件

text1,data1,data2 
text2,data1,data2 

,並使用這些數據的命令文件中的每一行。

謝謝你的幫助。

編輯:

我認爲我可以使用命令

while read line do 
    if [[ $line =~ $regex ]]; then 
     (commands here for each row, but i cant find how to get the variable) 
    fi 
done < file.txt 

英里文本文件是這樣的:

prueba1,vSwitch0,10 
prueba2,vSwitch0,20 
prueba3,vSwitch0,30 

,我必須把這些數據轉化爲這樣的代碼:

New-VirtualPortGroup -Name prueba1 -VirtualSwitch vSwitch0 -VlanId 10 
+0

請問您能介紹一下嗎? 「並將該數據用於文件每一行的命令中。」 –

+0

你已經試過了什麼?請展示一些努力和代碼並指出具體問題。這不是一個代碼寫入服務。 –

+0

編輯,感謝您的重新申請 – kaler26

回答

0

既然你r文本文件似乎以逗號分隔而沒有標題。您可以使用Import-CSV-Header標誌將數據導入PowerShell對象。然後循環對象中的數據。

$Data = Import-CSV C:\path\to\file.csv -Header 'Name','vSwitch','Id' 
ForEach ($Entry in $Data) { 
    New-VirtualPortGroup -Name $Entry.Name -VirtualSwitch $Entry.vSwitch -VlanId $Entry.Id 
} 
+0

它完美地工作,謝謝! – kaler26