Help with output from import-csv and -join / replace


i'm having trouble in getting correct output following script.

foreach ($line in import-csv c:\computers.csv)  {  $computer = $line.computer  $a = $line.info  $b = -join "$line.computer".replace("-","")[0,1,-2,-1]  $computer  $a  $b  }

the .csv file has computer name of "abc-testcomp01" in column "a" , "someinfo" in column "b"

$computer returns "abc-testcomp" correctly

$a returns "someinfo"

$b returns "@{er"

however @ console

-join "abc-testcomp".replace("-","")[0,1,-2,-1]  returns correctly with "abmp"

how can correct value returned $b

monday, january 14, 2013 8:36 pm

$b = -join "$($line.computer)".replace("-","")[0,1,-2,-1]

try encasing $line.computer in $(), value of $line.computer first, return string expanded in double quotes $()

or have done

$b = -join "$computer".replace("-","")[0,1,-2,-1]

seeing added value of $line.computer $computer

if find post has answered question, please mark answer. if find post helpful in anyway, please click vote helpful.




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2