Need help with Powershell Command

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Hamid A Hamid A 2 years, 7 months ago.

  • Author
    Posts
  • #15103
    Profile photo of Hamid A
    Hamid A
    Participant

    Hello,

    I am new with powershell and I need some help with command.

    Here is my data:

    Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\

    What I am want to get the value after the = sign, the output should look like this
    c:\users\db2admin\

    Here is the command I running to get the data, but I can't figure out how to get c:\users\db2admin\

    db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}

    If ther an option to add to above command to print last value after = sign?

  • #15107
    Profile photo of GregW
    GregW
    Participant

    Hello, you can use the Split method of System.String, ie:

    (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1]

    and in your case even

    (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1].Trim()
    ^ ^
    to get rid of the whitespace (Note the parentheses)

    I always feel awkward specifying the array index like that, but if you know there's only one split character, it seems straightforward, maybe someone has a more elegant approach.

    http://msdn.microsoft.com/en-us/library/system.string.split.aspx

  • #15128
    Profile photo of Hamid A
    Hamid A
    Participant

    Hi Greg,

    Thank you for reply. I am getting the following error while excuting the command.

    PS C:\Users\db2admin> (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}).Split("=")[
    1].Trim()
    Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'Split'.
    At line:1 char:93
    + (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}).Split <<<< ("=")[1].Trim() + CategoryInfo : InvalidOperation: (Split:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

  • #15140
    Profile photo of Hamid A
    Hamid A
    Participant

    Hi deiandrei and Greg,

    Thank you for both of suggestion. I found a solution, it seems very easy:

    ((db2 get dbm cfg | select-string DIAGPATH) -split ' = ')[1]

  • #15132
    Profile photo of deiandrei
    deiandrei
    Participant

    Hi Hamid,

    I was actually expecting to see a post of Dave's, since he is a Regex expert 🙂
    But maybe I can help you too:


    $a = "Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\"
    $null = $a -match "(?< == ).*" $Matches.Values

    Explanation: first we assign the string to a variable.
    Then we do a Regex match: find all characters after "= ". We assign the match to the $null variable, just to avoid the output "True" which tells you that there is a valid match.
    At last point, we just call the found matches.

    Hope this helps!

You must be logged in to reply to this topic.