Powershell Scripts for users - powershell

I am trying to distribute a Powershell script for our users to update some files in their roaming profile. The script works fine but I'm hitting the issue where some machines have Powershell scripts disabled.
I am having them download this from a FTP server and run it from their desktop.
Is there a way to allow them to run the script and not have it blocked by Windows?
Thank you!

As an Administrator, you can set the execution policy by typing this into your PowerShell window:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
When you are done updating everything, you can set the policy back to its default value with:
Set-ExecutionPolicy Restricted
You can bypass this policy for a single script by adding -ExecutionPolicy Bypass when running powershell
powershell -ExecutionPolicy Bypass -File script.ps1

Related

PowerShell bug “execution of scripts is disabled on this system.”

I have a power shell script that runs to stop services, 'stop / terminate process' , delete 2 files and then restart.
I can run this script perfect on my Windows 10 64 Bit Host Machine - with ZERO issues.
I try to run it in my Virtual Machines and I get the error
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
SO just for giggles I went to see my group policies and they are not configured on either machine.
Administrative Templates > Windows Components > Windows PowerShell
Not Configured.
So why the issue on the virtual machine and not in my host ?
EDIT
Ran Get-ExecutionPolicy and also Get-ExecutionPolicy-List on VM
Restricted
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Undefined
Ran it on my Host
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Unrestricted
I do not know how my local machine was changed - software installation ??
The following will allow all local scripts to execute on the VM, irrespective of whether they're signed or not:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
I am going to go out on a limb here and just rehash a portion of About Execution Policies.
The default execution policy for Windows client OSes is Restricted. This means that a script will not run automatically. If your VM has a Windows client OS and you have never changed the execution policy, then your issue is expected. If the one Windows 10 machine works without issues, then someone changed the execution policy.
On the problematic VMs, you will need to determine the scope (or account) that is running your script. Then you will need to set the execution policy accordingly.
If you are testing running a script while logged into the server as yourself, then you can just open a PowerShell console and run the following:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Then run the script in that same console.
The following command will list the execution policy for all scopes on that machine:
Get-ExecutionPolicy -List
You should compare the command above on the working system and the non-working system. Your issue likely be the execution policy setting for the particular scope that is running the script. If you read the link in my post, it should help you determine what you need to change specifically.
If you are on Windows here is what you have to follow:
Press the [windows] button and then type PowerShell.
Run as Adiministrator
Copy and Paste the following command and hit [Enter]
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Type Y and hit [Enter]
Rerun the command and type A hit [Enter]
Close the powershell and try again
Good luck.
Open your PowerShell and enter the following command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
I had the same problem with VS Code then I check the cmd with Administrator run. There is no problem so better to use CMD easy way to pass this problem
Run Powershell as an administrator and run the following command:
set-executionpolicy remotesigned
The least problematic approach is to use the command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
This will get around Admin user authority issues.
After running powershell as administrator, run the following commands:
Get-ExecutionPolicy -List
Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy Unrestricted -Force
May be you need to restart the machine.
Open your powershell as an administrator and then paste those commands:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Then choose A
I had the same issue in my PC. Open the windows PowerShell ISE in administrator mode and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
This command solved my issue.
I had same issue when trying to create a vue application by running vue create my-project
To fix this I have followed below steps
Open powershell as an administrator on Windows
Run this command - Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Now open a new session on your terminal and run your application specific script. It worked.
user powerShell as admin and execute the following commends:
1-Set-ExecutionPolicy RemoteSigned
2-Get-ExecutionPolicy -List
3-Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine*
and rerun the scrips again and it will work
I m just farwarding all the changes that implies with setting the ExcecutionPolicy to RemoteSigned with the command : Set-ExecutionPolicy -ExecutionPolicy RemoteSigned ( I really recommend to first give a look at the other policies offered : Windows Execution Policies )
RemoteSigned :
The default execution policy for Windows server computers.
Scripts can run.
Requires a digital signature from a trusted publisher on scripts and
configuration files that are downloaded from the internet which
includes email and instant messaging programs.
Doesn't require digital signatures on scripts that are written on the
local computer and not downloaded from the internet.
Runs scripts that are downloaded from the internet and not signed.
Risks running unsigned scripts from sources other than the internet
and signed scripts that could be malicious.
Note that on Windows, browsers sign downloaded files and mark them as 'coming from the Internet'. If u want to unblock such a script, u can use the cmd Unblock-File.
user powerShell as admin and execute the following commends:
PS C:\WINDOWS\system32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Unrestricted
CurrentUser Unrestricted
LocalMachine Unrestricted
To unrestrict the execution policy:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
Under your normal user. The following requires to open an administrator instance:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine
as an administrator.
You might need to restart the computer afterwards.
You will see peerjs server is working
Use the syntax below
Set-ExecutionPolicy -Scope CurrentUser
Enter the "Unrestricted" as your value in the ExecutionPolicy parameter syntax below:
ExecutionPolicy:Unrestricted
Then run your command; you can check the node version or any version of the software you installed after implementing the commands above
I have tried these two ways that you can tackle this problem succesfully:
By enabling PowerShell execution policies:
PS C:\Users\usr1>Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
For example, I encountered this problem when i wanted to run yarn command at my project's root folder. Then by running the command above, I was abled to run the comand yarn without a problem.
Use bash terminal instead of powershell terminal
You can use this method when you get error from the powershell terminal by running the Set-ExecutionPolicy command. Like the error below:
In such case, by switching your terminal to bash you can have your scripts run:

PowerShell from Batch File on Network Folder using Admin Accounts: Cannot Find Drive Specified

I have a batch file that will run a PS script from my floating desktop after prompting for Admin credentials, however when I place the batch and PS file on the network to run, it says
N:\folder\folder2\folder3\Powershell.bat
The System cannot find the drive specified, and lists my mapped N:\ drive and directory.
Strangely, when I log into Windows on the same Admin AD account. Not requiring me to prompt the password because I am already Admin, it fixes this issue. Here is my code:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
write-output "Enter your ADMIN credentials to continue..."
EXIT
I'm fairly new to scripting, so please help me out, I tried to list the exact directory but it didn't work. I believe it MUST be with some issues with using my Admin account. This domain must require my Admin account before I can run the scripts. It was working last week so I'm pretty lost...

Powershell with elevated permissions through RunOnce

I am trying to run a powershell script using the RunOnce registry key. I need it to run as administrator and I can get the script to run but I can't get it to run with elevated permissions. Here is what I have tried but it doesn't do anything.
My code:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -verb runas -File c:\script.ps1
Any Ideas?
Try powershell.exe -executionpolicy unrestricted -file ..... and remove the set-excecution unrestricted line in your script as it won't have any effect. If powershell is able to read that command in your script file, then it means the script is allowed => execution-policy doesn't need to be changed.
Set-ExecutionPolicy is an interactive command, it does not belong in a script(unless running on a remote computer maybe).
Also, if you're trying to execute it with RunOnce, then make sure it's run using the HKLM RunOnce key to make sure it runs in the SYSTEM context(so it has the proper permissions).

Run Powershell script in windows Task Scheduler(Windows 7)

Hi I've got a Script and are trying to start it in Task Scheduler.
this poses Two problems for me.
is that the script will activate a Application that requires the GUI to be running and shown. so how can I make Task Scheduler to log in to windows automatically at a specific time. The computer will never/rarely be turned off or rebooted.
2.
I have yet to Successfully start a Powershell script from the Task Scheduler. I've tried Following:
powershell.exe -noexit -executionpolicy bypass -command &'Y:\Run Test\StartPowershellScript.ps1'
And
powershell.exe -noexit -ExecutionPolicy Bypass -File 'Y:\Run Test\StartPowershellScript.ps1'
And a variation of the above.
powershell.exe -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
and I've tried some variations with and without &"" &'' & and so on.
now I'm not really sure if it can be done. but I hope I can get some help here.
Edit 1.
I just tried the following.
powershell -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
after I did this in powershell.
set-executionpolicy -scope Process -executionPolicy Unrestricted -force
set-executionpolicy -scope CurrentUser -executionPolicy Unrestricted -force
set-executionpolicy -scope LocalMachine -executionPolicy Unrestricted -force
However How do I make sure that this will persist after the computer has been rebooted ?
Edit 2.
as it seems I can't make Scheduled task log in or unlock the computer, is there Anyway to set up a task to run at a specific time to log in to the computer so the scheduled task can run at this time ?
You can't log into the GUI with a scheduled task. Enable automatic logon if you need an interactive desktop.
Remove -NoExit from the argument list, otherwise PowerShell will keep running after the script completes. Also replace your single quotes with double quotes.
The execution policy is persistent unless it's superseded by a local or group policy (in which case Set-ExecutionPolicy should throw an error) or you're logged in with a temporary profile.
If that doesn't help you need to provide more details. Doesn't the task start at all? Do you get an error? What do the task history and eventlog say? What is the output of Get-ExecutionPolicy?

Why can't I get the Jenkins Powershell plugin to work?

Why can't I get the Jenkins "Powershell plugin" to work?
I can run a powershell script on Jenkins using a "Execute windows batch command" build step with the following command:
powershell -ExecutionPolicy ByPass -File script.ps1
But I am unable to run a powershell script with the Jenkins "Powershell plugin" using the "Windows Powershell" build step and this command, because of a Windows Execution policy not set error disallowing it to run:
script.ps1
Does anyone know the proper arg to give the Jenkins "Powershell Plugin" for it to succesfully run a script? Otherwise, I will just use the batch script work-around.
The correct thing to do is to set an execution policy on your machine (a one-time action), at which point you won't need to bypass it every time, and the Jenkins plugin should "just work". Are you unable to?
A reasonable starting setting would be RemoteSigned, which will allow you to execute local scripts fine but would still disallow scripts downloaded from the internet.
From an elevated PowerShell prompt, you would run:
Set-ExecutionPolicy RemoteSigned
See also: http://technet.microsoft.com/library/hh849812.aspx
UPDATE: excerpt from Help on applying policy and how it's supposed to behave:
If you set the execution policy for the local computer (the default)
or the current user, the change is saved in the registry and remains
effective until you change it again.
Of course, if your machine is on a Domain, then Group Policy could revert this.
For a reboot-proof solution, put this single line
powershell Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
in a batch file in the All Users Startup folder, which on Windows 7 is C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
(or you can get there by clicking Start -> All Programs, right-click Startup and click Open All Users)
This is how I got Jenkins executing PS scripts on a domain machine subject to Group Policy, without having to involve the sys admin guys ;-)
After experimentation, I realized that since Jenkins is running as a service as the System user, then the powershell scope is different than the scope used by my terminal services login session.
This script works for me and seems to properly set the registry keys so that the setting is persistent across reboots and new logins.
# SetExecutionPolicyToRemoteSigned.ps1
# Need to run this after every server reboot.
Write-Output "Setting local Powershell policy to RemoteSigned"
Write-Output ""
Set-ExecutionPolicy -scope CurrentUser Undefined -Force
#Set-ExecutionPolicy -scope Process Undefined -Force
Set-ExecutionPolicy -scope LocalMachine Undefined -Force
Set-ExecutionPolicy -scope CurrentUser RemoteSigned -Force
#Set-ExecutionPolicy -scope Process RemoteSigned -Force
Set-ExecutionPolicy -scope LocalMachine RemoteSigned -Force
Write-Output "Finished."
Get-ExecutionPolicy -list
Start-Sleep -s 10