Thomas Lang
Thomas Lang

Reputation: 231

Method "split" not found in [Selected.System.Management.Automation.PSCustomObject]

in my script I want to read a csv-file into an array and split the text in the first column.

The file consists a table with 2 columns. In the first column there are the personal names with the short Usernames in brackets. In the second column are the position of the user.

User:
Hoch,Susane (HOCH05)
Albrecht, Melanie (ALBRE05)

Department:
Managment
Salesoffice

I read the first column in an array and want to split every char after the first "(". So the I have got "Hoch, Susanne" instead of "Hoch, Susane (HOCH05)".

I get the following error message: [Selected.System.Management.Automation.PSCustomObject] contains no method with the name "Split".

The type of the variable "$value is:

    IsPublic IsSerial Name                            BaseType                                                                                                      
    -------- -------- ----                            --------                                                                                                        
    True     False    PSCustomObject                  System.Object                                                                                                         

I can´t find my misstake.

Here is my code:

$Arrayusername_ads_unique = New-Object System.Collections.ArrayList
$AD_User = New-Object System.Collections.ArrayList
$AD_User_table = New-Object System.Collections.ArrayList
$username_AD = New-Object System.Collections.ArrayList

$Arrayusername_ads_unique = Get-Content -Path "C:\temp\Userabgleich\Liste-original\User_ADS-utf8.csv"

$Arrayusername_ads_unique | Out-File C:\temp\Userabgleich\output-temp\User_ADS-utf8.csv -Append -Encoding utf8

$AD_User = Import-CSV 'C:\temp\Userabgleich\output-temp\User_ADS-utf8.csv' -Delimiter ";" | sort User

$AD_User_table = $AD_User | Select-Object User

foreach ($value in $AD_User_table)
{
    $value.GetType()
    $username_AD = $value.Split("(")
}

Upvotes: 1

Views: 1698

Answers (2)

Lee_Dailey
Lee_Dailey

Reputation: 7479

since you did not provide a proper CSV to test against, i made a guess at what it would look like. [grin]

what it does ...

  • uses the .Split() method to split on the (
  • takes the 1st result of that split
  • trims away any leading/trailing whitespace
  • sends the result to the $Names collection
  • displays the content of that collection

here's the code ...

# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
User,Department
"Hoch,Susane (HOCH05)","Managment"
"Albrecht, Melanie (ALBRE05)","Salesoffice"
'@ | ConvertFrom-Csv

$Names = foreach ($IS_Item in $InStuff)
    {
    $IS_Item.User.Split('(')[0].Trim()
    }

$Names

output ...

Hoch,Susane
Albrecht, Melanie

Upvotes: 0

AdminOfThings
AdminOfThings

Reputation: 25001

You can modify your foreach loop to achieve the results:

foreach ($value in $AD_User_table)
{
    ($value.user -split "\(")[0]
}

I am splitting on the .user property of $value to retrieve the value you are after in string format. By default, $value is going to be a [PSCustomObject] with a property called User. I am retrieving index 0 ([0]) because your -split match will consume a line of output whether or not you choose to keep the output.

If you are only looping to retrieve this particular result, you can accomplish this without a loop using regex substitution and named captures:

$ad_user_table.user -replace "(?<Name>.*?)\(.*",'${Name}'

Upvotes: 1

Related Questions