7 Replies Latest reply on Sep 1, 2015 8:46 AM by Yanick Girouard

    NSH (type 1) Powershell Script returns bad characters

    James Donohue

      Hello,

       

      I have an NSH script in my Console and it finds the Windows Registry value that I'm looking for but returns it with some weird characters appended to it.

       

      Command:

      RESULT= `nexec -i -e cmd /c "echo . | powershell (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\ML\AutoInstall').Version"`

       

      It should return: 607 but it returns 607^M as shown below:

       

      Error    Aug 28, 2015 1:18:17 AM    //@/bladelogic/nsh/NSH/tmp/lkcma60490a/scripts/job__b057f7a2-dac7-4df9-84d6-f7049293301a/script_DBKey-SJobKeyImpl-8521663-1__65d2ff81-82fa-4439-aa97-6b89f30200a1.3401235.1_Test_Band_Version.nsh:76: command not found: 607^M

       

      The script is supposed to update a Server property but it doesn't and my guess is because the returned data contains these extra garbage characters. Is there anyway I can obtain the data exactly as it is listed in the Registry? Just need 607.

        • 1. Re: NSH (type 1) Powershell Script returns bad characters
          Yanick Girouard

          You need to escape the backslashes, and there's an extra space after the variable's equal sign... You also have to save your scripts in Unix format if it's NSH or else you need to plan for the extra \r (^M) character for the end of lines...

           

          Try this way instead (mind the quotes and double-quotes):

           

          RESULT=$(nexec -e cmd /c powershell -noprofile -noninteractive -command "(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\ML\\AutoInstall').Version" | tr -d "\r")

          • 2. Re: NSH (type 1) Powershell Script returns bad characters
            James Donohue

            Thanks Yanick, that worked! Another question, if you don't mind:

             

            The wmic query is returning 2 lines and there's seems to be no way for me to remove the 'Version' text using parameters. I can't figure it in Windows anyway. After getting this result back to the script and before I echo $RESULT, is there a way for me to just return 4.5.50709 only. In powershell, you can use a 'skip 1' command. Is there a parsing command in NSH to skip the first line? 

             

            RESULT=$(nexec -e cmd /c powershell -noprofile -noninteractive -command "(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\ML\\AutoInstall').Version"  | tr -d "\r")


            Info    Aug 31, 2015 10:47:07 AM    Version

            Info    Aug 31, 2015 10:47:07 AM    4.5.50709 (<-- this is all I want)

            • 3. Re: NSH (type 1) Powershell Script returns bad characters
              Yanick Girouard

              Hi James, that's easy, just use `tail -n +2` to skip the first line:

               

              RESULT=$(nexec -e cmd /c powershell -noprofile -noninteractive -command "(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\ML\\AutoInstall').Version"  | tr -d "\r" | tail -n +2)

              1 of 1 people found this helpful
              • 4. Re: NSH (type 1) Powershell Script returns bad characters
                James Donohue

                Hi Yanick,

                 

                I have another WMI query and used your solution and it returns the data I need, however, in this query I'm only interested in the second line that gets returned in the multi-line variable. I'm looking for the latest version of the .Net Framework and never know how many lines it will return.

                 

                (There's other methods I can use but this seems to be the most precise on getting exactly what I need.) 

                 

                Command:

                RESULT3=$(nexec -e cmd /c wmic path win32_product where "name like '%%.NET%%'" get version | tail -n +2)

                echo $RESULT3

                 

                Returns:

                Info    Aug 31, 2015 3:58:49 PM    4.5.50709

                Info    Aug 31, 2015 3:58:46 PM    4.0.30319

                 

                So, using tail, I'm able to skip the Heading (first line) but it's giving me multiple entries. Is there a way I could format it to only give me the latest version? (i.e. 4.5.50709) , which is the first line (after tail command)

                Thanks!

                 

                (P.S. Im learning NSH / BLCLI / Bash but teaching myself takes a lot longer than I was hoping)

                • 5. Re: NSH (type 1) Powershell Script returns bad characters
                  Bill Robinson

                  Pipe it through sort and tail the last line ?

                  • 6. Re: NSH (type 1) Powershell Script returns bad characters
                    James Donohue

                    The solution I came up with, with the Help of the good people in this Post is:

                     

                    RESULT3=$(nexec -e cmd /c wmic path win32_product where "name like '%%.NET%%'" get version | tail -n +2) | sort

                    RESULT3=(${RESULT3[@]})

                    echo "${RESULT3[0]}"

                     

                    I found this snippet on: bash: Assigning the first line of a variable to a variable - Unix & Linux Stack Exchange

                    by scouring Google. I find I get more answers on BASH Websites than BMC, I guess since its essentially the BASH scripting language as per a conversation I had with Bill R. a long time ago. I seem to be taking the multi-line string variable and creating an array and grabbing the first line (0). Anyway, it works. Also, I could NOT get the WMIC query to work due to syntax issues so I looked at it carefully and stripped out the Namespace path and came up with a simpler line:

                     

                    Final Query - stripped (works in BSA):

                    wmic /namespace:\\root\cimv2 path win32_product where "name like '%%.NET F%%'" get version

                     

                    I'm not sure if stripping out the Namespace will work for all queries, but it worked for getting Product info.

                     

                    Thanks Yanick and Bill!

                    • 7. Re: NSH (type 1) Powershell Script returns bad characters
                      Yanick Girouard

                      Make sure you put the "| sort" inside the $() that is assigned to he variable. Not sure if it's a typo but the code you wrote shows it outside.

                       

                      And you're welcome For future reference, NSH is based on ZSH (ZShell), and pretty much everything you can do in zsh, you can do in NSH (except the newer features that came out with the latest version of zsh). Zsh has some nice stuff that bash doesn't support, such as variable transformations like : ${var:l} to convert to lowercase, or ${var:u} for uppercase.

                       

                      So whenever you search Google for code snippets to help with NSH scripting, start with zsh, and if you find nothing then try bash.