Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Running Powershell scripts in C# fails/no effect

Running Powershell scripts in C# fails/no effect

Scheduled Pinned Locked Moved C#
questioncsharpwindows-admintools
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    me dagsunde com
    wrote on last edited by
    #1

    I know this isn't a Powershell forum, but the question is about PS in C#.. I have the following Powershell, that clears my languagebar, Setting Norwegian language. Adds Norwegian Apple Keyboard, removes, then Adds back to the end normal Norwegian keyboard:

    $li = New-WinUserLanguageList "nb-NO";
    $li[0].InputMethodTips.Add("0414:A0000414");
    $li[0].InputMethodTips.Remove("0414:00000414");
    Set-WinUserLanguageList $li -Force
    $li = Get-WinUserLanguageList
    $li[0].InputMethodTips.Add("0414:00000414");
    Set-WinUserLanguageList $li -Force

    This works Peachy when run from the powershell commandline. It updates the Language bar, and set the Apple keyboard as default. But when embedding it in C# like this:

    string script = "";
    script += "$li = New-WinUserLanguageList nb-NO;";
    script += "$li[0].InputMethodTips.Add(\"0414:A0000414\");";
    script += "$li[0].InputMethodTips.Remove(\"0414:00000414\");";
    script += "Set-WinUserLanguageList $li -Force;";
    script += "$li[0].InputMethodTips.Add(\"0414:00000414\");";
    script += "Set-WinUserLanguageList $li -Force;";

    using ( PowerShell ps = PowerShell.Create() ) {

    ps.AddScript( script );
    var result = ps.Invoke();
    
    foreach ( var item in result ) {
        //Nothing.
    }
    

    }

    The language bar does not get updated, although the language IS switched to Norwegian... Is there any limitations on what kind of ps-scripts you can embed in C#? (Both the script and the c# code runs as me/current user)... TIA. -- Dag.

    P 1 Reply Last reply
    0
    • M me dagsunde com

      I know this isn't a Powershell forum, but the question is about PS in C#.. I have the following Powershell, that clears my languagebar, Setting Norwegian language. Adds Norwegian Apple Keyboard, removes, then Adds back to the end normal Norwegian keyboard:

      $li = New-WinUserLanguageList "nb-NO";
      $li[0].InputMethodTips.Add("0414:A0000414");
      $li[0].InputMethodTips.Remove("0414:00000414");
      Set-WinUserLanguageList $li -Force
      $li = Get-WinUserLanguageList
      $li[0].InputMethodTips.Add("0414:00000414");
      Set-WinUserLanguageList $li -Force

      This works Peachy when run from the powershell commandline. It updates the Language bar, and set the Apple keyboard as default. But when embedding it in C# like this:

      string script = "";
      script += "$li = New-WinUserLanguageList nb-NO;";
      script += "$li[0].InputMethodTips.Add(\"0414:A0000414\");";
      script += "$li[0].InputMethodTips.Remove(\"0414:00000414\");";
      script += "Set-WinUserLanguageList $li -Force;";
      script += "$li[0].InputMethodTips.Add(\"0414:00000414\");";
      script += "Set-WinUserLanguageList $li -Force;";

      using ( PowerShell ps = PowerShell.Create() ) {

      ps.AddScript( script );
      var result = ps.Invoke();
      
      foreach ( var item in result ) {
          //Nothing.
      }
      

      }

      The language bar does not get updated, although the language IS switched to Norwegian... Is there any limitations on what kind of ps-scripts you can embed in C#? (Both the script and the c# code runs as me/current user)... TIA. -- Dag.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Your two scripts aren't identical. The first line doesn't have nb-NO quoted, so try surrounding this with \"nb-NO\". Also, you are missing $li = Get-WinUserLanguageList from your C#.

      This space for rent

      M 2 Replies Last reply
      0
      • P Pete OHanlon

        Your two scripts aren't identical. The first line doesn't have nb-NO quoted, so try surrounding this with \"nb-NO\". Also, you are missing $li = Get-WinUserLanguageList from your C#.

        This space for rent

        M Offline
        M Offline
        me dagsunde com
        wrote on last edited by
        #3

        Thanks for commenting... The differences are just me trying small differences. They where the same. Didn't work in c# when they were identical either... (But I should of course have posted them equal, to avoid confusion). -- Dag.

        P 1 Reply Last reply
        0
        • P Pete OHanlon

          Your two scripts aren't identical. The first line doesn't have nb-NO quoted, so try surrounding this with \"nb-NO\". Also, you are missing $li = Get-WinUserLanguageList from your C#.

          This space for rent

          M Offline
          M Offline
          me dagsunde com
          wrote on last edited by
          #4

          Thanks for commenting... Thats not it, thou... Just me trying small adjustments. With or without quotes, and with or without re-getting the newly set list. Results are the same. -- Dag.

          1 Reply Last reply
          0
          • M me dagsunde com

            Thanks for commenting... The differences are just me trying small differences. They where the same. Didn't work in c# when they were identical either... (But I should of course have posted them equal, to avoid confusion). -- Dag.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            You might need to set the execution policy:

            Set-ExecutionPolicy Unrestricted -Scope Process

            This space for rent

            M 1 Reply Last reply
            0
            • P Pete OHanlon

              You might need to set the execution policy:

              Set-ExecutionPolicy Unrestricted -Scope Process

              This space for rent

              M Offline
              M Offline
              me dagsunde com
              wrote on last edited by
              #6

              Yeah... It looks like that is where the dog is buried... ;P Thanks...

              P 1 Reply Last reply
              0
              • M me dagsunde com

                Yeah... It looks like that is where the dog is buried... ;P Thanks...

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                No problem. Glad you got it working (and I like the analogy).

                This space for rent

                M 1 Reply Last reply
                0
                • P Pete OHanlon

                  No problem. Glad you got it working (and I like the analogy).

                  This space for rent

                  M Offline
                  M Offline
                  me dagsunde com
                  wrote on last edited by
                  #8

                  Its a Norwegian proverb... Don't know where/why... But it is a good one! :) Still got some small problems, thou... I turned on ExecutionPolicy so it looks like this:

                  $li = New-WinUserLanguageList "nb-NO";
                  $li[0].InputMethodTips.Add("0414:A0000414");
                  $li[0].InputMethodTips.Remove("0414:00000414");
                  Set-WinUserLanguageList $li -Force;
                  $li = Get-WinUserLanguageList;
                  $li[0].InputMethodTips.Add("0414:00000414");
                  Set-WinUserLanguageList $li -Force;

                  - This creates a new languagelist with Norwegian standard culture, - Adds the Norwegian Apple input (keyboard). - Removes the Standard Norwegian keyboard. - Set the list to be the current one in windows. - Add back the Norwegian standard keyboard. - Reapply the list to windows. The sole purpose of the operation is to set the language bar to "Norwegian, with Apple Keyboard" with Norwegian with std. keyboard as a second option. And it *ALWAYS* does, when run from Powershell/Command prompt. When run embedded in c#, it sets Input to Norwegian Standard, and does NOT show Apple keyboard as a first (or second) alternative... This drives me NUTS! -- Dag.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups