I'm new to scripting in Powershell, and I've run into an issue where whenever I run a script as administrator it automatically fails. Running the script normally succeeds, but as administrator it opens for a split second and immediately closes again ( * after suggestions from @iRon, this is no longer the case). I'm honestly not sure if this is a programming issue or a Windows issue, so I'm putting it here - please let me know if this is better fit for the superuser stack.
The end goal is to be able to call the script from a batch script so that I can remotely re-build trusted root certificates on PCs where Windows Update screwed them up because of faulty firewall settings. But I can't do that unless I get it working to run as administrator.
My current code is as follows (from this tutorial) (They are both named the same aside from extension, hence the %~dpn0):
BATCH:
@ECHO OFF
PowerShell.exe -NoExit -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -noexit -wait -File ""%~dpn0.ps1""' -Verb RunAs}"
pause
POWERSHELL:
echo "Hello World!"
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{Write-Output 'Running as Administrator!'}
else
{Write-Output 'Running Limited!'}
Pause
Additional information: I'm running this on Windows 10 21H2 with powershell verion 10.0.19041.1023. I am able to run the commands individually in an elevated powershell window.
After @iRon's help, I was able to get the actual error message, although the script still will not work:
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:31
+ -wait -File [path]\Program Files (x86)\Lansweeper\PackageShare\Scripts\ ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException