Running Powershell scripts in C# fails/no effect
-
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 -ForceThis 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.
-
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 -ForceThis 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.
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
-
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
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.
-
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
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.
-
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.
You might need to set the execution policy:
Set-ExecutionPolicy Unrestricted -Scope Process
This space for rent
-
You might need to set the execution policy:
Set-ExecutionPolicy Unrestricted -Scope Process
This space for rent
Yeah... It looks like that is where the dog is buried... ;P Thanks...
-
Yeah... It looks like that is where the dog is buried... ;P Thanks...
No problem. Glad you got it working (and I like the analogy).
This space for rent
-
No problem. Glad you got it working (and I like the analogy).
This space for rent
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.