I got sidetracked from this issue, but I’m back to it now and I hope olaf and kvparson haven’t lost interest. BTW I tried “Bypass” instead of “Unrestricted” as kvparson suggested but I got the same error. Perhaps this is an “XY problem” as olaf suggests so I will give you the wider scope if the issue:
I’m using a public domain scientific programming environment called GNU Octave (basically a clone of the proprietary Matlab language). From Octave, one can find the screen resolution but not the screen working area (i.e. where you can put stuff avoiding the task bar), so I wrote a function to return the screen working area. The function first looks for a power shell script called workingArea.ps1 and if it doesn’t find it, it creates the workingArea.ps1 file with the following contents:
Add-Type -AssemblyName System.Windows.Forms
$Scr = [System.Windows.Forms.Screen]::AllScreens
$Di = $Scr | Where-Object -FilterScript {$_.DeviceName -match 'DISPLAY1'}
$Di.WorkingArea.X, $Di.WorkingArea.Y,
$Di.WorkingArea.Width, $Di.WorkingArea.Height, $Di.Bounds.Height |
Out-File -FilePath e:\Octave\workingArea.txt -Encoding ASCII
(Actually the file path of the workingArea.txt will change depending on the location of the Octave script).
Then it executes the powershell script, reads the results from the workingArea.txt file and returns that result. The full Octave code (workingArea.m) follows:
function p = workingArea()
% returns [x y width height] of the primary display useable are not including the taskbar
% where: x is the distance in pixels from the left edge
% y is the distance in pixels from the bottom edge
f2 = which(mfilename); % path and file name of this function
f2(end)=''; % remove .m extension
f1 = [f2 'ps1']; % name of powershell script
f2 = [f2 'txt']; % name of powershell script results file
if ~exist(f1) % create powershell script file if it doesn't already exist
f = fopen(f1,'w');
fprintf(f,'Add-Type -AssemblyName System.Windows.Forms\n');
fprintf(f,'$Scr = [System.Windows.Forms.Screen]::AllScreens\n');
fprintf(f,'$Di = $Scr | Where-Object -FilterScript {$_.DeviceName -match ''DISPLAY1''}\n');
fprintf(f,'$Di.WorkingArea.X, $Di.WorkingArea.Y,\n');
fprintf(f,'$Di.WorkingArea.Width, $Di.WorkingArea.Height, $Di.Bounds.Height |\n');
fprintf(f,'Out-File -FilePath %s -Encoding ASCII\n',f2);
fclose(f);
end;
system(['powershell -inputformat none -file ' f1 ' -ExecutionPolicy Unrestricted']); % execute powershell script
p = textread(f2,'%n'); % read script results
p(2) = p(5) - p(2) - p(4); % convert top edge coordinates to bottom edge
p = p(1:4)'; % return first four elements as row vector
When I run this script from Octave, it returns the following error message:
File E:\Octave\workingArea.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
Although the error message seems to suggest that windows is not allowing the powershell script to run, I also thought it possible that somehow Octave was not allowing it. So I tried running the script in Matlab and I found that it worked! I know I am not imagining this because I tested it with several different taskbar sizes and positions and the script always returned the correct working area. But the frustrating thing is I tried it the next day and I found that it no longer worked, and in fact returned the exact same error message I received from Octave. (I’m not sure if I had done a reboot between those sessions.) Despite the initial success, I never got it to work again. I also tried it on another computer and also got the same error message that running scripts is disabled.
So to summarize … I know this script has potential because I have seen it work, but I have no idea what is usually preventing the powershell script from executing.
Do you think this is a windows issue or merely an Octave/Matlab issue? If it’s the later, perhaps I could get help on the Matlab or Octave forums, but I doubt I would find someone there with this specialized knowledge.
Thanks for your help and patience.
~Paul