How to get rid of security warning in Batch/Powershell - windows

I am trying to execute a powershell script via batch file and constantly getting the following warning;
-> the execution policy is set to remote signed ; i also tested with Unrestricted, the same error shows up; how to get rid of this?

Is your ps1 script reside in a network share ? You can skip all the security process by typing set-executionpolicy bypass in a admin powershell console
or you can invoke your script by powershell.exe -executionpolicy bypass -file \\servername\share\script.ps1

If the same error shows up, make sure you're setting the execution policy in the right place. On a 64 bit system there will be an execution policy for both 64 and 32 bit versions of Powershell. Normally, it loads the 64 bit version for the console, but uses the 32 bit version if called from a bat file. You need to launch the 32 bit version, running eleveated, and set the execution policy for it also.

Related

Heroku won't load within Visual Studio Code

heroku : File C:\Users\mar\AppData\Roaming\npm\heroku.ps1 cannot be loaded. The file C:\Users\mar\AppData\Roaming\npm\heroku.ps1 is not digitally
signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
This is the error I'm getting. I've tried running "heroku -v" within the cmd and it works there so I know it was installed correctly. However, when I run the same command in visual studio I get the above error. I'm running visual studio code in admin mode and I installed heroku from admin mode as well.
I also enabled developer mode in my computer settings.
You need to set the execution policy regardless of whether you are running in administrator mode or not for PowerShell.
You can read more about this here
You can also see that it's throwing an error with regards to execution policy. The error basically means that windows could not verify the identity of the user that created the script and is blocking you from running it because it may be harmful (based on the fact that it could not verify the identity of the file creator).
This is just a counter-measure to prevent malicious scripts from running automatically.
If you want to run the script I would suggest running
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
and then running the Heroku commands. The above script removes restrictions on script executions on the current logged in user.

Powershell Closes Instantly

I've looked all of the the internet and I cannot find any information that applies to this situation.
Powershell closes immediately upon starting. When I run it in Command Prompt I get the following:
Windows PowerShell terminated with the following error:
Unable to cast object of type 'System.String' to type 'System.String[]'.
I have checked the following folders and no profiles exist:
c:\users\me\appdata\microsoft\windows\powershell
c:\windows\system32\windowspowershell\v1.0\
c:\windows\systwow64\windowspowershell\v1.0\
I have tried to run with the following commands and have no luck:
powershell -noexit
powershell -noprofile
I have run the following commands and have no luck:
sfc.exe /scannow
DISM.exe /Online /Cleanup-image /Restorehealth
I also have gone to Control Panel -> Uninstall a Program -> Turn Windows features on or off then,
Removed Powershell, rebooted, then re-installed it.
After doing all of these steps I still am not able to run Powershell. ISE does not work either.
This may be related to Powershell logging settings. I had the exact same issue after implementing Powershell Module logging using the wrong path for ModuleNames.
Check the values set in HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging
In my case, I made the mistake of adding a value of ModuleNames set to * - not realizing it should be a KEY named ModuleNames - which resulted in an error
Windows PowerShell terminated with the following error:
Unable to cast object of type 'System.String' to type 'System.String[]'.
By adding the right path HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\ with a REG_SZ value named * and with a value of * I got Powershell working again.
Note that if this setting comes from a GPO (Group Policy Object) in the domain, it will need to be fixed there, rather than in the registry. Otherwise, the GPO will just overwrite the local settings the next time it applies.
Whenever you run powershell, it loads the default modules present in the Modules directory. One of these modules (most likely a custom one you wrote) is causing errors and not allowing you to start.
Without knowing anything about the modules you have present in the directory located at: C:\windows\system32\windowspowershell\v1.0\Modules, it would be hard for anyone to tell you the solution.
Recommendation
Remove any custom modules you have in there and add each module you need one at a time to see which one breaks your powershell.exe. You will need to check each path you have defined for custom modules to be loaded as well.
Other way would be to clear out the PSModulePath from Environment variables and add one location at a time until you see which Modules directory is causing error.
NOTE: Write down the paths on a notepad somewhere before you clear it.
From the error it seems like a .net library class (dll) that is not correctly written.

Package Manager Console Not Working

I have checked extensively for a solution to the below error to no avail.
When opening Package Manager Console in Visual Studio, I receive an error stating that "There was an error in loading the format data file.. Path --> to file" cannot be loaded because its execution is blocked by software restriction policies. For more information contact your administrator.
These are all of the things i've done to correct but failed.
Running Powershell as an Administrator.. I Set-ExecutionPolicy to RemoteSigned and Unrestricted in powershell(x86), powershell ISE (x86) and also both 64bit versions..
Running Powershell as an Administrator.. I Set ExecutionPolicy -scope for process, currentuser and localmachine to RemoteSigned and Unrestricted.
Reinstalled Visual Studio.
Set the ExecutionPolicy in Registry Editor to RemoteSigned and Unrestricted.
But I am still getting the error.. Can someone please help..
Thanks
Let me know if you tried the following:
PowerShell says "execution of scripts is disabled on this system."
http://sqlish.com/file-ps1-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-please-see-get-help-about_signing-for-more-details/
Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies
if all fails do the below:
It's possible that you changed the execution policy for 64-bit
powershell and the package manager is running 32-bit (or vice versa).
I'd try opening 32-bit console (PowerShell (x86)) and setting the
execution policy there, as the error is definitely pointing to that
kind of resolution.
Make sure you restart visual studio after changing execution policy so
that changes can take effect. Also make sure you changed execution
policy globally with administrator username and password.
Source:file cannot be loaded because the execution of scripts is disabled on this system
As a workaround, I found this solution (more details at stackoverflow.com/a/53433786/8358565)
Execute the following commands in the Package Manager Console
Set-ExecutionPolicy -Scope Process Bypass
Import-Module "your-solution-directory/packages/EntityFramework<your EF version>/EntityFramework.psd1"

Windows RunOnce - Running twice :/

I created an exe with NSIS that installs my application, but if the application is already running, it gets updated at startup.
It seems this simplest way to achieve this is by added a registry entry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
My entry looks like this:
Value name: InstallRUS
Value data: cmd /C "C:\Location\of\my\program.exe /S"
This executable simply copies files to a directory and runs a script to register the app as a windows service.
If I restart windows, it runs twice, the first time in silent mode, and the second time it seems to ignore the silent flag.
If I log out and log back in, it's correctly ran once in silent mode.
This leads to the conclusion that the task is ran at windows startup and login, yet the documentation states :
Run and RunOnce registry keys cause programs to run each time that a
user logs on...By default, the value of a RunOnce key is deleted before the command line is run.
Does windows starting up counts as "user login"?
Why is the entry not deleted after the first run?
How can I fix this?
Why is windows lying to me?
Note:
I'm using Windows 7 Professional x32
Seems to be related to this: Windows Registry RunOnce Loop but I am not writing to the registry

VS error when creating new WebAPI project

When I create a new WebAPI project (MVC4) I get the following error.
EntityFramework.5.0.0: Failed to initialize the Powershell host. If your powershell execution policy setting is set to AllSigned, open the package manager console to initialize the host first.
jQuery.1.7.1.1: Failed to initialize the Powershell host. If your powershell execution policy setting is set to AllSigned, open the package manager console to initialize the host first.
After Googling I have found a few answers but nothing that works yet.
Error creating new MVC project - EF and JQuery
This answer seems like it should work for me as my last project was a 7z Command Line app and I might have done something daft with 7zip. But I copy pasted the 7-Zip directory from Program Files to Program Files (86) with no luck.
http://social.msdn.microsoft.com/Forums/en-US/vssetup/thread/c934fed4-e44e-4a06-9e3b-eccb9c8aa8d6
There is an answer here that might work (I haven't tried it) but even if it does work I wouldnt want to do this every time I create a new project.
Is anyone able to help me with this one?
I got around a similar error by running PowerShell as administrator with the command Set-ExecutionPolicy Unrestricted, restarting Visual Studio, and opening the Package Manager Console before what I wanted to do.
Make sure you understand the security implications of doing this first.
http://technet.microsoft.com/en-us/library/ee176961.aspx
Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned - Only scripts signed by a trusted publisher can be run.
RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
I encountered this issue recently, after re-install VS and install the latest VS update 2, things go well. This works for me at least.

Resources