Hello everyone,<\/p>\n
I m making a powershell script to get values out of a Excel spreadsheet, by first open the file converting to csv and then retrieve the data out of it (found easier and quicker to do this way). The problem i have is getting values out of the CSV columns, the reason is besides the first 4 columns (witch are always identical or all cases) the numer of columns and their names are diferent so i cannot do this:<\/p>\n
write-host $data[$x].randomfield\n<\/code><\/pre>\n
Advertisement
because the “randomfield” is … always diferent.<\/p>\n
Where i m having issues is after “reconstructing” the table from the CSV i m unable to get the values as usable text, in the script i have bellow where i have issues is in this part:<\/p>\n
$skill = $data[$x].psobject.properties | select name | where { $_.name -eq $headers[$i] }\n$level = $data[$x].psobject.properties | select value | where { $_.name -eq $headers[$i] } | Select-Object -Skip ($csvHeaders.Count-$headers.Count)\n<\/code><\/pre>\nI m trying to get something like “guy with ID $variable, has the skill $skill on the level $level”, i always get something like @{value=2,value=1,value=3} instead of 3 for example. I did this script ask me anything you see fit, i really tried to explain the best i can but i found hard to do so … any help i can get a hit or solution is welcome.<\/p>\n
Full script:<\/p>\n
# ---> Configurações\n$xlsx = \"C:\\1\\skills.xlsx\"\n$tmpCSV = \"C:\\1\\skils.csv\"\n$csvHeaders=@('id','nome','categoria','especialidade')\n\n# ---> Abre uma instancia de Excel\n$xl = New-Object -ComObject Excel.Application\n$xl.Visible = $false\n$xl.DisplayAlerts = $false\n\n# ---> Abrir folha de Microsoft Excel na instancia e exporta para csv\n$wb = $xl.Workbooks.Open($xlsx)\n$wb.SaveAs($tmpCSV, 6)\n$wb.Close($false)\n$xl.Quit()\n\n# ---> Mata processos de Excel ainda aberto ($xl.Quit() por vezes não funciona ...)\n$process = Get-Process -Name 'excel' -ErrorAction SilentlyContinue\nif($process.Count -gt 0){\n foreach($toKill in $process){\n $toKill.kill()\n }\n}\n\n# ---> Importar CSV criado do ficheiro de Excel\n$csv = Import-Csv $tmpCSV -Encoding Default\n\n$headers = Get-Member -InputObject $csv[1] | Where-Object {$_.MemberType -eq \"NoteProperty\"} | select-object Name\n$headers = $headers -split \";\" | Where { $_.length -gt 0 }\n$headers = $headers[1..($headers.Length)]\n$headers = $headers -replace '[{}]'\n\n$csvHeaders=$csvHeaders+$headers\n$data = Get-Content $tmpCSV | Select-Object -Skip 3 | ConvertFrom-Csv -Header $csvHeaders -Delimiter ';'\n\nfor($x=0;$x -le $data.Count; $x++){\n\t#$data[$x].id\n\t#$data[$x].nome\n\tfor($i=0;$i -le $headers.Count;$i++){\n\t\t$skill = $data[$x].psobject.properties | select name | where { $_.name -eq $headers[$i] }\n\t\t$level = $data[$x].psobject.properties | select value | where { $_.name -eq $headers[$i] } | Select-Object -Skip ($csvHeaders.Count-$headers.Count)\n\t}\n\twrite-host _______________________________________________\n}\n<\/code><\/pre>","upvoteCount":6,"answerCount":3,"datePublished":"2015-02-27T10:39:51.000Z","author":{"@type":"Person","name":"ricardogomes2","url":"https://community.spiceworks.com/u/ricardogomes2"},"acceptedAnswer":{"@type":"Answer","text":"See if this is the technique you are looking for:<\/p>\n
$csv = Import-Csv \"T:\\110\\in.csv\"\n\n$headers = $csv[0] | \nGet-Member -MemberType NoteProperty | \nSelect-Object -ExpandProperty Name\n\nForEach($item in $csv) {\n ForEach($header in $headers) {\n \"$header :: $($item.$header)\"\n }\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2015-02-27T12:44:45.000Z","url":"https://community.spiceworks.com/t/get-values-out-of-psobject/383423/2","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello everyone,<\/p>\n
I m making a powershell script to get values out of a Excel spreadsheet, by first open the file converting to csv and then retrieve the data out of it (found easier and quicker to do this way). The problem i have is getting values out of the CSV columns, the reason is besides the first 4 columns (witch are always identical or all cases) the numer of columns and their names are diferent so i cannot do this:<\/p>\n
write-host $data[$x].randomfield\n<\/code><\/pre>\nbecause the “randomfield” is … always diferent.<\/p>\n
Where i m having issues is after “reconstructing” the table from the CSV i m unable to get the values as usable text, in the script i have bellow where i have issues is in this part:<\/p>\n
$skill = $data[$x].psobject.properties | select name | where { $_.name -eq $headers[$i] }\n$level = $data[$x].psobject.properties | select value | where { $_.name -eq $headers[$i] } | Select-Object -Skip ($csvHeaders.Count-$headers.Count)\n<\/code><\/pre>\nI m trying to get something like “guy with ID $variable, has the skill $skill on the level $level”, i always get something like @{value=2,value=1,value=3} instead of 3 for example. I did this script ask me anything you see fit, i really tried to explain the best i can but i found hard to do so … any help i can get a hit or solution is welcome.<\/p>\n
Full script:<\/p>\n
# ---> Configurações\n$xlsx = \"C:\\1\\skills.xlsx\"\n$tmpCSV = \"C:\\1\\skils.csv\"\n$csvHeaders=@('id','nome','categoria','especialidade')\n\n# ---> Abre uma instancia de Excel\n$xl = New-Object -ComObject Excel.Application\n$xl.Visible = $false\n$xl.DisplayAlerts = $false\n\n# ---> Abrir folha de Microsoft Excel na instancia e exporta para csv\n$wb = $xl.Workbooks.Open($xlsx)\n$wb.SaveAs($tmpCSV, 6)\n$wb.Close($false)\n$xl.Quit()\n\n# ---> Mata processos de Excel ainda aberto ($xl.Quit() por vezes não funciona ...)\n$process = Get-Process -Name 'excel' -ErrorAction SilentlyContinue\nif($process.Count -gt 0){\n foreach($toKill in $process){\n $toKill.kill()\n }\n}\n\n# ---> Importar CSV criado do ficheiro de Excel\n$csv = Import-Csv $tmpCSV -Encoding Default\n\n$headers = Get-Member -InputObject $csv[1] | Where-Object {$_.MemberType -eq \"NoteProperty\"} | select-object Name\n$headers = $headers -split \";\" | Where { $_.length -gt 0 }\n$headers = $headers[1..($headers.Length)]\n$headers = $headers -replace '[{}]'\n\n$csvHeaders=$csvHeaders+$headers\n$data = Get-Content $tmpCSV | Select-Object -Skip 3 | ConvertFrom-Csv -Header $csvHeaders -Delimiter ';'\n\nfor($x=0;$x -le $data.Count; $x++){\n\t#$data[$x].id\n\t#$data[$x].nome\n\tfor($i=0;$i -le $headers.Count;$i++){\n\t\t$skill = $data[$x].psobject.properties | select name | where { $_.name -eq $headers[$i] }\n\t\t$level = $data[$x].psobject.properties | select value | where { $_.name -eq $headers[$i] } | Select-Object -Skip ($csvHeaders.Count-$headers.Count)\n\t}\n\twrite-host _______________________________________________\n}\n<\/code><\/pre>","upvoteCount":6,"datePublished":"2015-02-27T10:39:51.000Z","url":"https://community.spiceworks.com/t/get-values-out-of-psobject/383423/1","author":{"@type":"Person","name":"ricardogomes2","url":"https://community.spiceworks.com/u/ricardogomes2"}},{"@type":"Answer","text":"Yes thank you very much that worked as i wanted!<\/p>","upvoteCount":0,"datePublished":"2015-02-27T14:29:33.000Z","url":"https://community.spiceworks.com/t/get-values-out-of-psobject/383423/3","author":{"@type":"Person","name":"ricardogomes2","url":"https://community.spiceworks.com/u/ricardogomes2"}}]}}