2016-03-14 29 views
0

我需要導入csv,選擇一個字符串並將其一部分值更改爲$env:username。我想我必須與select-string一起獲得字符串,但我堅持改變價值。或者是否可以將$env:username直接寫入csv?我無法做到。此外,CSV應該留事情是這樣的,只有當前PowerShell會話需要正確的字符串如何修改當前PowerShell的CSV內部字符串會話

CSV看起來是這樣的,userhome$env:username更換:

Test  Test2 
-----  ----- 
Hi   C:\user\userhome 
something C:\test\install 
hello  C:\windows 

多數民衆贊成我試過,但我米不成功。

$test.Test2 | sls userhome -replace ($env:username) 

另一種方法:

sls ($test.Test2).value -replace ("userhome",$env:username) 

編輯:這是整個命令需要該CSV - 只爲信息:

$Drives = Import-CSV .\NetworkDrives.csv -Delimiter ';' | ? {(($_.Group).split(',') -contains $UserOU) -and (!(Test-Path $_.Letter))} | % { #Do Stuff } 
+1

'$ Csv | %{$ _。PSObject.Properties} | %{$ _。Value = $ _。Value -replace「userhome」,$ env:username}' – PetSerAl

+0

謝謝,工作! – SimonS

回答

0

我很高興PetSerAl的答案在評論爲你工作。就我個人而言,我認爲使用Get-Content,運行-Replace可能會更簡單,然後管道到ConvertFrom-CSV而不是Import-CSV

$Drives= (Get-Content .\NetworkDrives.csv) -Replace "userhome",$env:username | ConvertFrom-CSV -Delimiter ';' | ? {(($_.Group).split(',') -contains $UserOU) -and (!(Test-Path $_.Letter))} | % { #Do Stuff } 
+0

謝謝MadTechnician。我會盡快測試,看看哪個解決方案運行得更快,然後接受你的答案。有趣的是:你在2個月前給了我一個針對同一個腳本的問題的答案。所以你會被列爲合作者;-)。乾杯! – SimonS