Question about Get-Variable

Welcome Forums General PowerShell Q&A Question about Get-Variable

This topic contains 10 replies, has 5 voices, and was last updated by

 
Participant
2 years, 5 months ago.

  • Author
    Posts
  • #57884

    Participant
    Points: 0
    Rank: Member

    Hey Powershell Community,
    I'd like to ask for first time a question or help for the Problem that I'm stuck at.
    Example:

    $var = "lol"
    $var2 = "lol2"
    $variantA = 'This is example with $var and $var2'
    $variantB = "This is example with $var and $var2"

    Basically I could get a string in my script where I have the names of variables written in String. Can anyone help me out or suggest how could I get values from variables that are in string written? In this example that means How could I get "lol" and "lol2" from variantA and variantB?

    I would be so grateful for any answer or question.

    Thank you ALL in advance.

    Sincerely,
    Bernard

  • #57887

    Participant
    Points: 2
    Rank: Member

    $VariantB should already contain the expanded values of $var and $var2. Using double quotes will evaluate variables and "expand" their contents into the string. Single quotes are literal and will not expand the values.

    If you just want to get the values of $var and $var2, simply run:

    $var
    $var2
    
  • #57890

    Participant
    Points: 1,374
    Helping Hand
    Rank: Community Hero

    I didn't get your question completely but to explain the difference for you – if you use single quotes expressions or veriables will not be executed/evaluated, if you use normal double quotes expressions and variables will be executed/evaluated.

  • #57893

    Participant
    Points: 2
    Rank: Member

    Sorry, I re-read and thought that you might be looking for how to extract the values of the first two variables out of $VariantB. One way to do this is with regular expressions:

    [regex]::Match($VariantB,$var).value
    [regex]::Match($VariantB,$var2).value
    
  • #57901

    Participant
    Points: 0
    Rank: Member

    Hey,
    thanks for such fast answer. I might haven't explained it not so good.

    What I am trying to show is:
    Per example I have a Excel or .csv file that contains in one cell text or String: "This is the Computer with $ProcessorName and $GraphicCardName."
    I now want to get what it is in this $ProcessorName and $GraphicCardName String variable. Basically their values from StringName that is written in this line of String.

    It is a Little bit hard to explain. I am basically trying to write a script for WDS Server for Software/Operating System Installation that will be deployed through Powershell script.

    Thank you for taking time for this question.

    Cheers,
    Bernard

  • #57925

    Participant
    Points: 0
    Rank: Member

    Using your "This is the Computer with $ProcessorName and $GraphicCardName" example

    $x = "This is the Computer with Pentium and Nvidia"
    $y = ($x -split 'This is the Computer with ')[1] -split ' and '

    PS> $y[0]
    Pentium
    PS> $y[1]
    Nvidia

    Never did like regex 🙂

  • #57928

    Participant
    Points: 0
    Rank: Member

    Hey Richard,

    Thanks for your post. I will try it out and will let you know.

    Have a nice evening,

    Bernard

  • #57929

    Participant
    Points: 1,374
    Helping Hand
    Rank: Community Hero

    Hmmm ... I guess I have a vage idea what you try to do. Try:

    $ExecutionContext.InvokeCommand.ExpandString($variantA)
  • #57931

    Participant
    Points: 2
    Rank: Member

    here's a version using Regex, just in case you're interested:

    "This is the Computer with Pentium and Nvidia" -match "^This is the Computer with (?'Processor'\w+) and (?'GraphicCard'\w+)$"
    $Matches.Processor
    $Matches.GraphicCard
    
  • #58003

    Participant
    Points: 1
    Rank: Member

    Hi Olaf, this is brilliant. Works like a charm. 🙂 Also a thank-you to all the others.

    Tried it with the following unfancy codesnippet before (also works but is too long and unsexy...):

     
    
    Process 
        {
        Try {
            If (($LanguageFiles = Get-ChildItem ".\" | Where {$_.Name -like "Language_*.csv"} | Select-Object -ExpandProperty Name).count -gt 0)
                    {
                    WRITE-DEBUG "Write-Feedback - `$LanguageFiles: $LanguageFiles"
    
                    # Jede gefundene Datei in Katalog aufnehmen
                    ForEach ($LanguageFile in $LanguageFiles) 
                        {
                        WRITE-DEBUG "Write-Feedback - `$LanguageFile: $LanguageFile"
    
    #----------------------------------------------------------#
    # Note: Get-Delimiter is no build in CMDlet
    #----------------------------------------------------------#
    
                        # Delimiter ermitteln 
                        $Delimiter = Get-Delimiter ".\$LanguageFile" $Output $Log
    
                        WRITE-DEBUG "Write-Feedback - `$Delimiter: $Delimiter"
    
                        # Sprachdateien importieren
                        $LanguageCatalog = Import-CSV -Encoding Default -Delimiter $Delimiter ".\$LanguageFile" -ErrorAction Stop
    
                        WRITE-DEBUG "Write-Feedback - `$LanguageCatalog: $LanguageCatalog"
    
                        # Inhalt der Sprachdateien auflisten
                        $LanguageCatalogs += $LanguageCatalog
    
                        WRITE-DEBUG "Write-Feedback - `$LanguageCatalogs: $LanguageCatalogs"
                        }
                    }   
    
            WRITE-DEBUG "Write-Feedback - `$PSUICulture: $PSUICulture"
    
            Try {
            # Programm:
             
            # Gibt die einzelnen Sätze aus für das gewählte Ereignis
            ForEach ($languageset in ($LanguageCatalogs.GetEnumerator() | Where {$_.MessageIDs -eq $Text}))
                {
                WRITE-DEBUG "Write-Feedback - `$languageset: $languageset"
    
                # Gibt die einzelnen Wörter aus welche pro gewählte Sprache ($PSUICulture) anfallen
                $singlewords = $languageset.$PSUICulture.Trim().Split(" ")
    
                WRITE-DEBUG "Write-Feedback - `$singlewords: $singlewords"
    
    #----------------------------------------------------------#
    # Note: Standardinfo-Error-Field-Empty is no build in CMDlet
    #----------------------------------------------------------#
    
                # Prüfen ob die Variable einen Wert enthält
                If (!$singlewords) {Standardinfo-Error-Field-Empty}
    
                # Array definieren
                $var = @()
    
                # Die einzelnen vorhandenen Variablen erfassen
                ForEach ($containedvar in ($singlewords | Where {$_ -like '$*'}))
                    {
                    WRITE-DEBUG "Write-Feedback - `$containedvar: $containedvar"
    
                    # Einzelne Variablen sammeln
                    $var += $containedvar
    
                    WRITE-DEBUG "Write-Feedback - `$var: $var"
                    } # End-ForEach
    
                # Jedes einzelne Wort prüfen
                ForEach ($word in $singlewords)
                    {
                    WRITE-DEBUG "Write-Feedback - `$word: $word"
    
                    # Durchlaufendes Wort ersetzen
                    # Nur Wörter wählen, die mit "$*" beginnen
                    If ($word -like '$*')
                        {
                        # Jede einzelne Variable auf einen Treffer prüfen
                        ForEach ($subvar in $var) 
                            {
                            WRITE-DEBUG "Write-Feedback - `$subvar: $subvar"
    
                            # Pattern anwenden um angehängte Symbole zu entfernen
                            $wordtrim = $word -replace '(\$[a-zA-Z0-9]+).*','$1'
                            $subvartrim = $subvar -replace '(\$[a-zA-Z0-9]+).*','$1'
                            $subvartrimvar = $subvartrim.trim('$')
    
                            WRITE-DEBUG "Write-Feedback - `$wordtrim: $wordtrim"
                            WRITE-DEBUG "Write-Feedback - `$subvartrim: $subvartrim"
                            WRITE-DEBUG "Write-Feedback - `$subvartrimvar: $subvartrimvar"
    
                            # Variable erschaffen / abrufen
                            $replace = $subvartrim
    
                            WRITE-DEBUG "Write-Feedback - `$replace: $replace"
                            
                            $with = (Get-Variable $subvartrimvar -Valueonly -ErrorAction Stop) 
                            
                            WRITE-DEBUG "Write-Feedback - `$with: $with"
    
                            # Prüfen ob der Name der Variable mit der generierten 100%tig übereinstimmt
                            If ($replace -eq $wordtrim)
                                {
                                WRITE-DEBUG "Write-Feedback - `$replace: $replace"
                                WRITE-DEBUG "Write-Feedback - `$wordtrim: $wordtrim"
    
                                # Text-Variable mit gewünschtem Wort (variablen Inhalt) ersetzen
                                $newword = $word.Replace("$replace","$with")
    
                                WRITE-DEBUG "Write-Feedback - `$newword: $newword"
                        
                                # Nur die ersetzten Variablen übernehmen, welche nicht den gleichen Namen aufweisen z.B.: nicht $testen -eq $testen
                                If ($newword -ne $word)
                                    {
                                    # Neues Wort dem Satz hinzufügen
                                    $newset = $newset + $newword + " "
    
                                    WRITE-DEBUG "Write-Feedback - `$newset: $newset"
                                    } # End-If
                                } # End-If
                            } # End-ForEach
                        } # End-If
                    Else
                        {
                        # Wörter zu einem Satz zusammenfügen die kein "$" beinhalten
                        $newset = $newset + $word + " "
    
                        WRITE-DEBUG "Write-Feedback - `$newset: $newset"
                        } # End-Else
                    } # End-ForEach
                } # End-ForEach  
    
    

    Cheers
    Rob & Bernard

    • #58006

      Participant
      Points: 1,374
      Helping Hand
      Rank: Community Hero

      You're welcome. Glad it was helpful. 😉

The topic ‘Question about Get-Variable’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort