Hi everyone.. Please I have been trying to run scripts on powershell administrator [duplicate] - react-native

I am trying to run a cmd file that calls a PowerShell script from cmd.exe, but I am getting this error:
Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.
I ran this command:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
When I run Get-ExecutionPolicy from PowerShell, it returns Unrestricted.
Get-ExecutionPolicy
Output:
Unrestricted
cd "C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts"
powershell .\Management_Install.ps1 1
WARNING: Running x86 PowerShell...
File C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:25
.\Management_Install.ps1 <<<< 1
CategoryInfo : NotSpecified: (:) [], PSSecurityException
FullyQualifiedErrorId : RuntimeException
C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> PAUSE
Press any key to continue . . .
The system is Windows Server 2008 R2.
What am I doing wrong?

If you're using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?
As an Administrator, you can set the execution policy by typing this into your PowerShell window:
Set-ExecutionPolicy RemoteSigned
For more information, see Using the Set-ExecutionPolicy Cmdlet.
When you are done, you can set the policy back to its default value with:
Set-ExecutionPolicy Restricted
You may see an error:
Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
To change the execution policy for the default (LocalMachine) scope,
start Windows PowerShell with the "Run as administrator" option.
To change the execution policy for the current user,
run "Set-ExecutionPolicy -Scope CurrentUser".
So you may need to run the command like this (as seen in comments):
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

You can bypass this policy for a single file by adding -ExecutionPolicy Bypass when running PowerShell
powershell -ExecutionPolicy Bypass -File script.ps1

I had a similar issue and noted that the default cmd on Windows Server 2012, was running the x64 one.
For Windows 11, Windows 10, Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator:
x86 (32 bit)
Open C:\Windows\SysWOW64\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned
x64 (64 bit)
Open C:\Windows\system32\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned
You can check mode using
In CMD: echo %PROCESSOR_ARCHITECTURE%
In Powershell: [Environment]::Is64BitProcess
References:
MSDN - Windows PowerShell execution policies
Windows - 32bit vs 64bit directory explanation

Most of the existing answers explain the How, but very few explain the Why. And before you go around executing code from strangers on the Internet, especially code that disables security measures, you should understand exactly what you're doing. So here's a little more detail on this problem.
From the TechNet About Execution Policies Page:
Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.
The benefits of which, as enumerated by PowerShell Basics - Execution Policy and Code Signing, are:
Control of Execution - Control the level of trust for executing scripts.
Command Highjack - Prevent injection of commands in my path.
Identity - Is the script created and signed by a developer I trust and/or a signed with a certificate from a Certificate Authority I trust.
Integrity - Scripts cannot be modified by malware or malicious user.
To check your current execution policy, you can run Get-ExecutionPolicy. But you're probably here because you want to change it.
To do so you'll run the Set-ExecutionPolicy cmdlet.
You'll have two major decisions to make when updating the execution policy.
Execution Policy Type:
Restricted† - No Script either local, remote or downloaded can be executed on the system.
AllSigned - All script that are ran require to be digitally signed.
RemoteSigned - All remote scripts (UNC) or downloaded need to be signed.
Unrestricted - No signature for any type of script is required.
Scope of new Change
LocalMachine† - The execution policy affects all users of the computer.
CurrentUser - The execution policy affects only the current user.
Process - The execution policy affects only the current Windows PowerShell process.
† = Default
For example: if you wanted to change the policy to RemoteSigned for just the CurrentUser, you'd run the following command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Note: In order to change the Execution policy, you must be running PowerShell As Administrator.
If you are in regular mode and try to change the execution policy, you'll get the following error:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option.
If you want to tighten up the internal restrictions on your own scripts that have not been downloaded from the Internet (or at least don't contain the UNC metadata), you can force the policy to only run signed scripts. To sign your own scripts, you can follow the instructions on Scott Hanselman's article on Signing PowerShell Scripts.
Note: Most people are likely to get this error whenever they open PowerShell because the first thing PowerShell tries to do when it launches is execute your user profile script that sets up your environment however you like it.
The file is typically located in:
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
You can find the exact location by running the PowerShell variable
$profile
If there's nothing that you care about in the profile, and don't want to fuss with your security settings, you can just delete it and PowerShell won't find anything that it cannot execute.

We can get the status of current ExecutionPolicy by the command below:
Get-ExecutionPolicy
By default it is Restricted. To allow the execution of PowerShell scripts we need to set this ExecutionPolicy either as Unrestricted or Bypass.
We can set the policy for Current User as Bypass by using any of the below PowerShell commands:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
Unrestricted policy loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
Whereas in Bypass policy, nothing is blocked and there are no warnings or prompts during script execution. Bypass ExecutionPolicy is more relaxed than Unrestricted.

Also running this command before the script also solves the issue:
Set-ExecutionPolicy Unrestricted

If you are in an environment where you are not an administrator, you can set the Execution Policy just for you (CurrentUser), and it will not require administrator.
You can set it to RemoteSigned:
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
or Unrestricted:
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
You can read all about Getting and Setting Execution policy in the help entries:
Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full

In Windows 7:
Go to Start Menu and search for "Windows PowerShell ISE".
Right click the x86 version and choose "Run as administrator".
In the top part, paste Set-ExecutionPolicy RemoteSigned; run the script. Choose "Yes".
Repeat these steps for the 64-bit version of Powershell ISE too (the non x86 version).
I'm just clarifying the steps that #Chad Miller hinted at. Thanks Chad!

RemoteSigned: all scripts you created yourself will be run, and all scripts downloaded from the Internet will need to be signed by a trusted publisher.
OK, change the policy by simply typing:
Set-ExecutionPolicy RemoteSigned

I'm using Windows 10 and was unable to run any command. The only command that gave me some clues was this:
[x64]
Open C:\Windows\SysWOW64\cmd.exe [as administrator]
Run the command> powershell Set-ExecutionPolicy Unrestricted
But this didn't work. It was limited. Probably new security policies for Windows10. I had this error:
Set-ExecutionPolicy: Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of...
So I found another way (solution):
Open Run Command/Console (Win + R)
Type: gpedit.msc (Group Policy Editor)
Browse to Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows Powershell.
Enable "Turn on Script Execution"
Set the policy as needed. I set mine to "Allow all scripts".
Now open PowerShell and enjoy ;)

First, you need to open the PowerShell window and run this command.
set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Then it will ask you to confirm. Type Y and press Enter.
When you run this command, you can see that your system has set all policies for the current user as remotely. It will take a few seconds to complete this process.
The image will be shown like below:
To check if the execution policy has set. Type:
Get-ExecutionPolicy
If it was set, the output would be like this:

Open a Windows PowerShell command window and run the below query to change ExecutionPolicy:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
If it asks for confirming changes, press Y and hit Enter.

You should run this command:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

Win + R and type copy paste command and press OK:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
And execute your script.
Then revert changes like:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"

Open the command prompt in Windows.
If the problem is only with PowerShell, use the following command:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

Setting the execution policy is environment-specific. If you are trying to execute a script from the running x86 ISE you have to use the x86 PowerShell to set the execution policy. Likewise, if you are running the 64-bit ISE you have to set the policy with the 64-bit PowerShell.

you may try this and select "All" Option
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Open Run Command/Console ( Win + R )
Type: gpedit. msc (Group Policy Editor)
Browse to Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows Powershell.
Enable "Turn on Script Execution"
Set the policy as needed. I set mine to "Allow all scripts".
Now run the run command what ever you are using.. Trust this the app will runs.. Enjoy :)

You can also bypass this by using the following command:
powershell Get-Content .\test.ps1 | Invoke-Expression
You can also read this article by Scott Sutherland that explains 15 different ways to bypass the PowerShell Set-ExecutionPolicy if you don't have administrator privileges:
15 Ways to Bypass the PowerShell Execution Policy

I have also faced a similar issue. Try this.
As I'm using Windows, I followed the steps as given below.
Open a command prompt as an administrator and then go to this path:
C:\Users\%username%\AppData\Roaming\npm\
Look for the file ng.ps1 in this folder (directory)
and then delete it (del ng.ps1).
You can also clear npm cache after this though it should work without this step as well.

If you have Git installed, just use Git Bash.

Set-ExecutionPolicy RemoteSigned
Executing this command in administrator mode in PowerShell will solve the problem.

In Window 10:
If you are not administrator, you can use this:
powershell Set-ExecutionPolicy -Scope CurrentUser
cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`
It solved my problem like a charm!

In the PowerShell ISE editor I found running the following line first allowed scripts.
Set-ExecutionPolicy RemoteSigned -Scope Process

For Windows 11...
It is indeed very easy. Just open the settings application.
Navigate to Privacy and Security:
Click on For Developers and scroll to the bottom and find the PowerShell option under which check the checkbox stating "Change the execution policy ... remote scripts".

Open PowerShell as Administrator and run Set-ExecutionPolicy -Scope CurrentUser
Provide RemoteSigned and press Enter
Run Set-ExecutionPolicy -Scope CurrentUser
Provide Unrestricted and press Enter

Open PowerShell as a administrator. Run the following command
Set-ExecutionPolicy RemoteSigned
Type Y when asked!

In Windows 10, enable the option under the name: 'Install apps from any source, including loose files.'
It fixed the issue for me.

To fix this issue, we have to set the execution policy, so that the PowerShell script runs on the particular machine. Here is how:
Open PowerShell Console by selecting “Run as Administrator” and set the execution Policy with the command: Set-ExecutionPolicy RemoteSigned
Type “Y” when prompted to proceed
credits:
https://www.sharepointdiary.com/2014/03/fix-for-powershell-script-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system.html

In PowerShell 2.0, the execution policy was set to disabled by default.
From then on, the PowerShell team has made a lot of improvements, and they are confident that users will not break things much while running scripts. So from PowerShell 4.0 onward, it is enabled by default.
In your case, type Set-ExecutionPolicy RemoteSigned from the PowerShell console and say yes.

Related

"react - native link error File C:\Users\pc\AppData\Roaming\npm\react-native.ps1 cannot be loaded"

Error message:
react-native : File C:\Users\pc\AppData\Roaming\npm\react-native.ps1 cannot be loaded. The
file C:\Users\pc\AppData\Roaming\npm\react-native.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.
At line:1 char:1
+ react-native link
+ ~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
How can I solve this? I have installed npm, Node.js and JDK. I have done the tutorial for Okta authentication and in the step of react-native link I get this error.
Try setting your execution policy in PowerShell to remote
signed.
Open a new PowerShell administrator window and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser.
There’s also a way to allow running PowerShell scripts on Windows in Settings somewhere, but I don’t remember where. Probably in Developer Settings.
Nothing to worry about this just run PowerShell as admin and run command below in that
steps
Go to Start Menu
Type PowerShell
Right Click PowerShell and run as
administrator
And Run set-executionpolicy remotesigne in
PowerShell
Right Click PowerShell and run as administrator
And Run set-executionpolicy remotesigne in PowerShell
it worked for me.

Different behavior when launching with -file parameter

This all feels like a bug, but I hope someone can prove me wrong.
PS version: 5.1.17763.592
I'm launching a powershell script from a UNC path with the "-file" and "-NoExit" parameter.
This is done from a VB.net application. The VB.Net .exe is run as admin, the Powershell session also then starts as admin.
Dim script_path As String = "-NoExit -ExecutionPolicy RemoteSigned -file \\PATH\Test.ps1"
Process.Start("Powershell", script_path)
This all launches fine, but for some unknown reason this Powershell instance do not behave the same way as if I were to run powershell as admin directly.
For example:
Some registry information cant be found or is returned differently.
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Measure-Object
Returns 116 in "-file" instance, 36 in standard instance.
cd "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\"
Returns not found in -file instance, but it do exist. I have found a few registry keys like this one.
This instance has 13 fewer modules avaliable (Get-Module -ListAvailable | Measure-Object), some cmdlets cant be run, for example Get-LocalUser.
It kind of feels like im remoting to a completely different computer when I'm not.
Same computer, same user, both powershell run as admin.
I've tried:
Launch from a local directory (Not UNC)
Remove -NoExit param.
Launching cmd as admin manually then powershell.exe -NoExit -ExecutionPolicy RemoteSigned -file \\UNC\Test.ps1 (This do not have the same limited instance)
I would understand if this mode was somewhat limited and modules need to be imported manually, but it doesnt explain why registry information is returned differently.

SSH into Linux Subsystem, not Windows CMD

I assume I messed up something since nobody has a similar question here. When I SSH into a windows laptop it does not give me the UNIX shell by default, instead it lands me directly at the Windows prompt. I then have to enter bash, followed by cd and then I can run the commands I need.
Is this a windows setting problem or an openssh issue and does anyone have any suggestions on how I can avoid needing to enter these commands every time that I log in?
The Windows SSH daemon is openssh, and by default it is setup to use cmd as the shell, but you can edit the configuration to use powershell, bash, or any other third party shell. Follow the information in this link.
Its given in MS PoweShell documentation should go through.
To set the default command shell, first confirm that the OpenSSH installation folder is on the system path. For Windows, the default installation folder is SystemDrive:WindowsDirectory\System32\openssh.
Configuring the default ssh shell is done in the Windows registry by adding the full path to the shell executable to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH in the string value DefaultShell.
Here is the example, the following Powershell command sets the default shell to be PowerShell.exe:
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
For further explanation refre this link.

Running PowerShell with different user

Can anyone help me with run command to open PowerShell as different user?
Currently I am trying to run below command
Runas /noprofile /user:domain\username powershell
It works but I have to provide password once powershell window opens, is there any way where I can directly put password in run command?
Or even if I get to know how can I run any commands from PowerShell using different user without opening new ps window. e.g need to run below command using different user.
Restart-Server -Name testservice
You could try schtasks.exe. You can get the help using schtasks.exe /?
If you are getting any errors after using this command referring the examples, update the post with the error, we are here to help you.
Update:
There is an in-built cmdlet available in PowerShell Register-ScheduledTask, but its depended on OS and is available only from Windows Server 2012 onwards.

How to run a CruiseControl task as an administrator in Windows Server 2008

I'm currently using CruiseControl.NET to automate my build. We have two different ways to build a solution in my build environment - one is through the Keil uVision4 IDE, the other is through Visual Studio 2008.
I've successfully gotten the Visual Studio solution to build properly using CruiseControl.NET and have created a batch file which properly uses Keil's uVision command line interface to compile my uvproj Project (compilation details here).
Problem Description
1) I can successfully execute the build script on my Windows 2008 server and build the project if I create a command prompt with administrator privileges (I'm doing this manually - start -> run -> cmd with ctrl-shift-enter to run as admin).
2) However, if I create a command prompt without administrator privileges, and attempt to execute the batch file, the batch file won't work unless I accept the prompt asking me to confirm admin rights are required to run the batch script.
How do I automatically execute a batch file as an administrator through CruiseControl?
Is this something that could be automated using the RunAs command?
Technical details
1) The batch file being executed is pretty simple - it deletes the old output and re-makes the output, creating a build log file in the location below.
set BuildLogLocation=BuildLog\BuildLog.txt
echo on
cd ../..
cd PTM
rmdir /s /q output
mkdir output
mkdir BuildLog
C:\Keil\UV4\UV4.exe -r myProj.uvproj -o %BuildLogLocation%
echo ErrorLevel of build is %ERRORLEVEL%
echo build complete, see %BuildLogLocation%
2) Currently I'm looking to use the Exec functionality to run the Keil build script above:
<Exec>
<Command>C:\myProject\Build\KeilBuild\BuildScript.bat<Command/>
<buildTimeoutSeconds>600<buildTimeoutSeconds/>
<!-- Details about error codes can be found here:
http://www.keil.com/support/man/docs/uv4/uv4_commandline.htm -->
<successExitCodes>0,1</successExitCodes>
<Exec/>
Related questions:
How can I use a build server with Keil uVision4 (MDK-ARM), script a build, use a makefile? (Electrical Engineering)
Execute a command-line command from CruiseControl.NET (Stack Overflow)
Can you run CCService, the CruiseControl.NET Windows Service, as a user who has administrative permissions? I'd try that first.
If that doesn't work, I would use runas to run your script. You'll have to embed the administrative user's password in the script calling runas.
I know this is old but, Did you get an offical way to do it Via Cruise Control?
Normally I create this and call it to call other processes "As Admin".
Make a ".VBS" script with This in the contents:
Dim strBatchPath
strBatchPath = "PATH-TO-FILE.EXE"
Set runBatch = CreateObject("shell.application")
runBatch.shellexecute strBatchPath,,,"runas",1
That could be an option to people that can't find an official way
You could try psExec from sysinternals. If you don't need to run as a nt-authority account you should be able to use this in the same way as runas.
It allows you to pass in the username/password as a switch (if memory serves)
I have Discovered that when using PSEXEC and using the -h switch, it then "runs as admin" on destination
e.g.
psexec -h \ServerToRunOn /accepteula -u DOMAIN\USER -p PASSWORD "PATH-TO-FILE"
I am Using CC.Net to call a batch file with the above in. This will run that file as Admin