Kikopu
Kikopu

Reputation: 155

Doesn't contain split method

Here is my code:

$fPath = Get-Content .\param.txt | Select-String 'adresse' -SimpleMatch
fPath = $fPath.split("=")[1]
$fPath

$paramListe = Get-Content .\param.txt | Select-String 'type' -SimpleMatch
$paramListe = $paramListe.split("=")[1]
$paramListe

$seuil = Get-Content .\param.txt | Select-String 'seuil' -SimpleMatch
$seuil = $seuil.split("=")[1]
$seuil = $seuil

$relanceAuto = Get-Content .\param.txt | Select-String 'relanceAuto' -SimpleMatch
$relanceAuto = $relanceAuto.split("=")[1]
$relanceAuto

And here is the imported .txt:

adresse=o:\******\******\DataIntegrator
type=logiciel
seuil=0
relanceAuto=yes

I want those variables to get the value next to the "=" sign. But everytime I launch the programm, it keeps telling me those are not strings:

[Microsoft.Powershell.Commands.MatchInfo] does not have a method called "split".

Can someone explain to me why it doesn't work please?

Upvotes: 1

Views: 14761

Answers (2)

FormulaChris
FormulaChris

Reputation: 101

In addition to what arco444 stated, you can also do a string conversion with your existing code by using double-quotes.

$relanceAuto = "$relanceAuto".Split("=")[1]

Upvotes: 3

arco444
arco444

Reputation: 22821

The use of the Select-String means that objects are being stored in the variables you're assigning, not strings, therefore you need to get a string value before you can use the .split() method.

If you do:

PS > $relanceAuto.gettype()

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

You can see that $relanceAuto is actually a MatchInfo object. You can see what properties are associated with it by:

PS > $relanceAuto | Format-List *


IgnoreCase : True
LineNumber : 4
Line       : relanceAuto=yes
Filename   : InputStream
Path       : InputStream
Pattern    : relanceAuto
Context    :
Matches    : {}

This displays that the data you actually want is contained in the Line property, therefore you can do the split like so:

PS > $relanceAuto.Line.split('=')[1]
yes

Upvotes: 7

Related Questions