Write some text in foreach-object

Hello,

Clear-Host
#variable

$table = New-Object System.Data.Datatable

#adding columns
[void]$table.Columns.Add("Sites")
[void]$table.Columns.Add("Processus")
[void]$table.Columns.Add("Room")

#adding rows
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("CHALON","Kitchen","209")

#$table | Where-Object {($_.Sites -eq 'MACON') -and ($_.Processus -in 'Direction')}

$table | ForEach-Object {
    $_.Processus
}

This is works. But if a do this :

$table | ForEach-Object {
    write-host " you are in : $_.Processus"
}

i have this :
you are in : System.Data.DataRow.Processus
you are in : System.Data.DataRow.Processus
you are in : System.Data.DataRow.Processus

How can i show processus with some text ?

You need to use the subexpression operator when accessing properties of objects in double quotes:

$table | ForEach-Object {
    write-host " you are in : $($_.Processus)"
}
1 Like

Thanks ! I progress…

now i want only the unique item. I think that I have use select -unique in $tableProcessus, but i don’t find where, and the good syntax

Clear-Host
#variable

$table = New-Object System.Data.Datatable

#adding columns
[void]$table.Columns.Add("Sites")
[void]$table.Columns.Add("Processus")
[void]$table.Columns.Add("Room")

#adding rows
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("CHALON","Kitchen","209")

$tableProcessus=$table | Where-Object {($_.Sites -eq 'MACON')}


$i=1
$tableProcessus | ForEach-Object {
    
    write-host "$i - $($_.Processus)"
    $i++
}

$choixProcessus=Read-Host "choose your processus : "
$choixProcessus=$choixProcessus-1

write-host "valeur processus choisi : $($tableProcessus.Processus[$choixProcessus])"

$tableBureau=$table | Where-Object {($_.Sites -eq 'MACON') -and ($_.Processus -eq $($tableProcessus.Processus[$choixProcessus]))}

$tableBureau | ForEach-Object {   
    write-host "room is : $($_.Room)"
}

Just to mention it at least once … you know there is an easier way of creating data in table format, don’t you?

$table = @'
Sites,Processus,Room
MACON,Communication,101
MACON,Communication,101
MACON,Communication,101
MACON,Communication,101
MACON,Direction,105
MACON,Direction,105
MACON,Direction,105
CHALON,Kitchen,209
'@ |
    ConvertFrom-Csv

And because PowerShell is so forgiving you don’t even need to use double quotes for the strings. :wink:

I’m not sure if I got what you mean … try this:

$table | 
    Select-Object -unique Sites,Processus,Room

i don’t think is the same result.

The result of $table |
Select-Object -unique Sites,Processus,Room
ok i progress ! i progress …

i could do that with your help. Now i need to get the index of an object. How can I do this ?

$town="macon"



$table = @'
Sites,Processus,Room
MACON,Communication,101
MACON,info,101
MACON,Communication,102
MACON,Communication,101
louhans,Direction,105
MACON,Direction,107
MACON,Direction,105
CHALON,Kitchen,209
'@ | ConvertFrom-Csv

#$table | Select-Object -unique Sites,Processus,Room | Where-Object {($_.Sites -eq $town) }


write-host "Welcome in $town. Choose the processus available : "
write-host ""
write-host ""


$Tableprocessus = $table | Where-Object {($_.Sites -eq $town) } | sort-Object -unique Processus

$i=1

#display processus here a want display communication and info
$Tableprocessus  | ForEach-Object{
    write-host  "$i - $($_.Processus)"
    $i++
}

write-host ""

$choiceProcessus = Read-Host " enter a number"



write-host "choose the processus room's available "

$TableRoom = $table | Where-Object {($_.Sites -eq $town) -and ($_.Processus -eq 'communication') }  | sort-Object -unique Room
#display room
$j=1
$TableRoom | ForEach-Object{
    write-host  "$j - $($_.Room)"
    $j++
}  

$choiceRoom = Read-Host " enter a number"




That tends to be an unreliable source of information. :wink:

I’m not sure if I really got what you mean. If it’s what I think I’d do something like this:

$table = @'
Sites,Process,Room
MACON,Communication,101
MACON,info,101
MACON,Communication,102
MACON,Communication,103
louhans,Direction,104
MACON,Direction,107
MACON,Direction,105
CHALON,Kitchen,209
louhans,Direction,108
'@ | 
ConvertFrom-Csv

$SiteGroup =
$table | 
Group-Object -Property Sites

$SiteGroup | 
Select-Object -Property Name, 
@{Name = 'Index'; Expression = {[array]::IndexOf($SiteGroup.Name, $_.Name)}} |
Out-Host

$SelectedSiteIndex =
Read-Host -Prompt 'Please select an index of the desired site'

$ProcessGroup =
$SiteGroup[$SelectedSiteIndex].Group |
Group-Object -Property Process

$ProcessGroup | 
Select-Object -Property Name, 
@{Name = 'Index';Expression = {[array]::IndexOf($ProcessGroup.Name,$_.Name)}} |
Out-Host
    
$SelectedProcessIndex =
Read-Host -Prompt 'Please select an index of the desired process'

$ProcessGroup[$SelectedProcessIndex].Group

nice script !
i have many thing to learn, never use group-object, $ProcessGroup |
Select-Object -Property Name,
@{Name = ‘Index’;Expression = {[array]::IndexOf($ProcessGroup.Name,$_.Name)}} | . Hard to me to understand… need some time

i try to explain what i want :

the process display all site WITHOUT duplicate :
1 MACON
2 louhans
3 CHALON

The user select 1

The process display all processus WITHOUT duplicate linked by chosen site MACON :
1 communication
2 info
3 direction

The user select 1 again

The process display all room without duplicate linked by the chosen site MACON AND the chosen process communication :
1 101
2 102
3 103

No Problem. Take the time you need. :wink:

Whatfor? I already showed to you how I would do it. You can use my suggestions if you like or don’t if you dislike. :man_shrugging:t4: And of course you are allowed to adapt it to whatever requirement you like.

That already happens with my code suggestion

That already happens with my code suggestion.

That already happens with my code suggestion.