-1

Well i want only to know what i Have to do if that what read out (includs like words like "leiter" in Verkaufsabteilungsleiter or Gesamtleitung and save in Variables like MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3 , $Mitgliedlesen3 filtered out that this things doesn't generate.

Or It finds things like Verkaufsabteilung-Inland and add there something in String. So i think to compare two arrays are shorter If i write line for line.

if ($Abt -match "leiter") {$Standortsleitunglesen2 = $Mtl2 + "s" + "leitung" + "-" + "Lesen"}


if ($Abt -match "leiter") {$Standortsleitunglesen2 = $Mtl2 + "s" + "leitung" + "-" + "Lesen"}

if ($Standortsleitunglesen2 -match "Geschäft" ) {$Standortsleitunglesen2 = "" }

So i can set the terms in array2 and i don't need to write lots of lines and type it manualy. I hope you understand it now.

So I think my Solution goes in following direction:

$array = @($MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3 , $Mitgliedlesen3)
$array2 = "leiter" , "leitung"

for([int] $i = 0; $i -le ($array.Count -1); $i++)
{
    foreach($word in $array2)
    {
        if($array[$i] -like "*$word*")
        {
         if ($array[$i] -match $MitgliedÄndern2) { ($MitgliedÄndern2 = "") }
         if ($array[$i] -match $Mitgliedlesen2) { ($Mitgliedlesen2 = "") }  
         if ($array[$i] -match $MitgliedÄndern3) { ($MitgliedÄndern3 = "") }
         if ($array[$i] -match $Mitgliedlesen3) { $Mitgliedlesen3 = ""}
         $array[$i] = ""            
           
        }
    }
}

I have a problem in Variables would generate Usernames of a Table of CSV. CSV: Inhalt

Nummer;Ordner1;Ordner2;Ordner3;Benutzername;;;Beschreibung;Mitglied;Mitglied 2;Gruppenbeschreibung;Gruppenbeschreibung 2

1;Hamburg;Geschäftsleitung;HH-GL;Stefan Berti;;;Standortleiter;GG-H-Geschäftsleiter;;Geschäftsleitung Hamburg;

in array2 i would like to Words that the the loop have to search in array1.

and if he find the string of array2 for example 'leiter' or another word like "Sekretär" in $array1 in $MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3 it would be Write "" in $MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3.

I have a method that works with an easy array but that method doesn't work in that example.

I'm new in Powershell and have find lots of things out but that. I don't know.

$File=Import-Csv '.\Datenbank\Hamburg.csv' -Delimiter ";" -Encoding UTF8 | foreach-object {


$Mtl1 = ""
$Mtl2 = ""
$Mtl3 = ""
$Org1 = ""
$Org2 = ""
$Org3 = ""
$Nummer = $_.Nummer
$User = ""
$Ordner1 = $_.Ordner1
$Ordner2 = $_.Ordner2
$Ordner3 = $_.Ordner3
$Beschreibung = $_.Gruppenbeschreibung 
$Beschreibung2 = $_.Gruppenbeschreibung2 

if ($Ordner1 -ne '') {$Org1 = echo HH}
if ($Ordner2 -ne '') {$Org2 = $($_.'Ordner2')}
if ($Ordner3 -ne '') {$Org3 = $($_.'Ordner3')}

$Mtl1 = "$Org1"
$Mtl2 = "$Org1" + "-" + "$Org2"
$Mtl3 = "$Org1" + "-" + "$Org2" + "-" + "$Org3" 


#Lesen
$Mitgliedlesen3 = ""
$Mitgliedlesen2 = ""
$MitgliedÄndern3 = ""
$MitgliedÄndern2 = ""
$Bes = $_.Beschreibung

if ($Org3 -ne '') {$Mitgliedlesen3 = $Mtl3 + "-"+ "Lesen"}
if ($Org2 -ne '') {$Mitgliedlesen2 = $Mtl2 + "-" + "Lesen"}

$MitgliedÄndern2 = "Abteilungsleiter" 
$Mitgliedlesen2  = "Bundesleiter"
$MitgliedÄndern3 = "Mitarbeiter"
$Mitgliedlesen3 = "Mitarbeiterleiter"

$array = $MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3 
$array2 = "leiter" , "" 

have tested follow things.

#$array3 = (Compare-Object $array2 $array).InputObject
 
#$Array4 = $Array | where {$_ -match "leiter"}
 
$result = $array | Where {$array -notContains "leiter"}
 
 
 #$result = $array1 | ?{$_.Split('=')[0] -in ($array2 | %{$_.Split('=')[0]})}
 
 
#$result = compare $Array $Array2 -Property Key -IncludeEqual -ExcludeDifferent -, 
Passthru $result
 
 
#$Array -like "*$array2*"
 
#$Array4
 
 
#$array3[0..1]
 
 
#ForEach ($array in $array2){
#$array  -replace "^[$i0 .. $i]$array2","" 

#}

.. }

thanks for help.

Joe
  • 11
  • 5
  • 1
    It would be helpful if you could include an example of the csv data you have, and the one you want. It's not yet clear to me what you want, and which of your commented-out code is relavant or not. – marsze Nov 05 '21 at 19:23
  • Ok then i edit my question ;) and add that thing. $MitgliedÄndern is a result of composition of different parts of csv File . it is generated of – Joe Nov 06 '21 at 08:21
  • @marsze have edit it and add more infos – Joe Nov 06 '21 at 19:11
  • One question on stackflow. Why does Stackflow inform me if i wrote an answer on my own question? What happen then ? Can't i after re answer new answers ? Well the thing is follow I have the database and i have to create Users. So i have different departments of company like sells and warehousing , management etc and i need different groups for example sells-writeing , sells-reading for folders in active Directory. I have to create the folders and i have to create the groups , set the properties in groups and folder and add the user to group. – Joe Nov 07 '21 at 11:39
  • for([int] $i = 0; $i -le ($array.Count -1); $i++) { foreach($word in $array2) { if($array[$i] -like "*$word*") { if ($array[$i] -match $MitgliedÄndern2) { ($MitgliedÄndern2 = "") } if ($array[$i] -match $Mitgliedlesen2) { ($Mitgliedlesen2 = "") } if ($array[$i] -match $MitgliedÄndern3) { ($MitgliedÄndern3 = "") } if ($array[$i] -match $Mitgliedlesen3) { $Mitgliedlesen3 = ""} $array[$i] = "" } } } like if its detect in array2 a word it should change it in variable. – Joe Nov 07 '21 at 11:42
  • @marsze sorry my English isn't very well. I have edit my Question and i Hope you understand it now what i mean. – Joe Nov 08 '21 at 21:20

2 Answers2

-1

I had some problems with the characters in your variables so I renamed them.

$MitgliedAndern2  = "Abteilungsleiter" 
$Mitgliedlesen2  = "Bunde"
$MitgliedAndern3  = "Mitarbeiter"
$Mitgliedlesen3 = "Mitarbeiterleiter"

$array1 = @($MitgliedAndern2 , $Mitgliedlesen2 , $MitgliedAndern3 , $Mitgliedlesen3)
$array2 = "leiter" , "Sekretär" 

"array1 before before script runs: $array1`r"


for([int] $i = 0; $i -le ($array1.Count -1); $i++)
{
    foreach($word in $array2)
    {
        if($array1[$i] -like "*$word*")
        {
           $array1[$i] = ""
        }
    }
}
    
"`rarray1 after script run: $array1`r"

enter image description here

Mogash
  • 130
  • 1
  • 7
  • Thanks for answer :) I would try that what you have make . So i see i have an mistake in thinking of solve the Problem . I would try it and give feedback . Which charakters do you mean ? – Joe Nov 06 '21 at 08:17
  • The variablenames was causing problems in powershell for me: $Mitglied**Ä**ndern2 – Mogash Nov 06 '21 at 10:55
  • @Mogash, PowerShell happily accepts non-ASCII-range characters such as `Ä` in variable names. If they don't work, the implication is that your script file is saved as UTF-8 _without a BOM_. Re-save it with a BOM, and it should work. – mklement0 Nov 06 '21 at 14:24
  • Yes @mklement0 sais it. But !! Powershell ISE have Problems with that. Powershell at its own haven't the problem. Is that a visual Basic editior? – Joe Nov 06 '21 at 19:13
  • @Joe, _Windows PowerShell_ - whether run in the (obsolescent) ISE or in a regular console window - requires a BOM to recognize UTF8, so with a BOM present in a given file, the file should also be read correctly in the ISE. Note that _PowerShell (Core) 7+_ now _defaults_ to UTF-8 and therefore no longer requires a BOM. However, if you need your scripts to run in _both_ PowerShell editions, be sure to use UTF-8 _with a BOM_. – mklement0 Nov 06 '21 at 19:50
  • @Joe, what Mogash is using is [Visual Studio Code](https://code.visualstudio.com/). With its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell), it is arguably the best choice to replace the [obsolescent ISE](https://learn.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support), which notably cannot run PowerShell (Core) 7+. See also: [this answer](https://stackoverflow.com/a/57134096/45375) (bottom section). – mklement0 Nov 06 '21 at 19:57
  • well, i have experiments lots of times with powershell ISE and there is a bug that Microsoft doesn't fix. It works sometimes with ANSI -Windows1252 or was it 8859 The Problem is that it works half half. Yes on the one side it works but if you protocol results you have problems with there . Its a special problem. Have need 2 month to understand what ise makes there. https://social.technet.microsoft.com/Forums/windowsserver/de-DE/eeb5570b-df97-44e9-b802-19d0cd504f9d/powershell-ise-kann-keine-umlaute?forum=powershell_de On Powershell its own, there isn't any problem. – Joe Nov 07 '21 at 11:20
-1

the solution is this is my groundsolution:

$MitgliedÄndern2 = "Abteilungsleiter" 
    $Mitgliedlesen2  = "Bundespräsident"
    $MitgliedÄndern3 = "Mitarbeiter"
    $Mitgliedlesen3 = "Mitarbeiterleiter"
    
    $array = @($MitgliedÄndern2, $Mitgliedlesen2 , $MitgliedÄndern3 , $Mitgliedlesen3)
    $array2 = "leiter" , "präsident"
    
    
    "array1 before before script runs: $array`r"
    
    
    for([int] $i = 0; $i -le ($array.Count -1); $i++)
    {
        foreach($word in $array2)
        {
            if($array[$i] -like "*$word*")
            {
             if ($array[$i] -match $MitgliedÄndern2) { if ($MitgliedÄndern2 -like "*$word*")  {$MitgliedÄndern2 = ""}}
             if ($array[$i] -match $Mitgliedlesen2) { if ($Mitgliedlesen2 -like "*$word*")  {$Mitgliedlesen2 = ""} }  
             if ($array[$i] -match $MitgliedÄndern3) { if ($MitgliedÄndern3 -like "*$word*")  {$MitgliedÄndern3 = ""} }
             if ($array[$i] -match $Mitgliedlesen3) { if ($Mitgliedlesen3 -like "*$word*")  {$Mitgliedlesen3 = ""} }
             $array[$i] = ""            
               
            }
        }
    }
    
    #$array[$i] = ""
     
        
    "`rarray1 after script run: $array`r"
    
    $MitgliedÄndern2
    $Mitgliedlesen2  
    $MitgliedÄndern3 
    $Mitgliedlesen3 
Joe
  • 11
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 13 '21 at 03:00
  • @Bot have to look at what you mean – Joe Nov 14 '21 at 13:10