How can I get my PowerShell script to execute?
When I run my script I get the error:
File cannot be loaded because the execution of scripts is disabled on this system
Can you help me to get my script to run?
set-executionpolicy unrestricted
#Set-ExecutionPolicy RemoteSigned
$target=[IO.File]::ReadAllText(".\usermenuTest1.4d")
$output=[IO.File]::ReadAllText(".\usermenuTest2.4d")
($output -replace $target) | Set-Content "usermenuTest2.4d.new"
Start-Sleep -s 10
This Technet article suggests trying Get-ExecutionPolicy to check your script execution policy, and then set it to AllSigned or Unrestricted. Also, make sure you run the script like this ".\scriptname.ps1"
Related
I am provisioning a Windows Server 2012R2 with terraforms on Vsphere. I have the machine up and running. Now I want to run some .cmd files on the machine via remote_exec of terraforms.
The user is not the Administrator, but in the Administrator group. UAC disabled for the purpose of executing scripts via CLI or powershell.
I have written a runAsAdmin.ps1 which would call my install_software.ps1 which will have all the necessary commands to execute the .cmd files.
runAsAdmin.ps1
powershell -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file C:\scripts\install_software.ps1' -verb RunAs}"
install_software.ps1
cd C:\\gitrepo\\lip-core-devops\\terraform\\system-config\\ims\\scripts
echo "Installing IMS Base from powerscript"
Invoke-Item .\Install_IMS_Base.cmd
I can run execute the runAsAsAdmin.ps1 on a powershell directly on the machine without admin elevation and everything works as expected.
But when I am executing the runAsAdmin.ps1 from the remote-exec of terraforms, the execution is successful, but the contents of the install_software file are not executed. I tried creating a directory before executing the cmd file.
Terraform part
provisioner "remote-exec" {
inline = [
"powershell -File C:\\scripts\\runAsAdmin.ps1"
}
the connection is all set and successful. I get success at the end of the script execution.
What I am I missing in my scripts that it is not executed via terraforms but works on the machine.
This could be caused by powershell execution policy?, try running it using the -ExecutionPolicy setting. I have to do this when using VM extensions in Azure.
start powershell -ExecutionPolicy Unrestricted -File runAsAdmin.ps1
i create server with winrm, than i put some command in powershell
like
provisioner "remote-exec" {
inline = [
"powershell.exe sleep 3",
When I execute the command at power shell Save-Module -Name ACMESharp -Path <path>
It prompts the error as below:
The 'Save-Module' command was found in the module 'PowerShellGet', but the module could not be loaded.
Then I try to execute Import-Module PowerShellGet turn out that it was the execution policy problems that the status is "restricted" the script.
To check whether is about the execution policy , you could just try to execute the ps command Get-ExecutionPolicy
if is return result show is restricted then you have to change the execution policy to RemoteSigned.
PS: hope that it won't cause any security issue. Just In case ,disable it after you done.
To disable the restriction, I just execute the ps Command below.
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
To change /remove the execution policy
Set-ExecutionPolicy Undefined
Or, type:
Set-ExecutionPolicy Undefined -scope LocalMachine
For more information can visit the link below:
https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Execution_Policies
I am trying to have a power shell script resume after a reboot. (I am rebooting to "apply" registry changes I have made") I believe I can accomplish what I want by making a registry edit to the Run Once key. I have looked at this question, but I can't get my similar code to execute at boot. The registry edit is made and at boot something runs because it disappears but it is not finishing the install.
$part2 = Get-ChildItem C:\Windows\ccmcache\ -Recurse -Force -Filter Full_Druva_Reinstall_part2.ps1
$FullPath = $part2.FullName
$KeyPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
new-itemproperty -Path $KeyPath -Name !Install-Druva -propertytype String -value "Powershell -executionPolicy Unrestricted -File $FullPath"
Edit
This scrpit is inside a SCCM Package and any solution needs to automatic and require no user input.
Open task scheduler on general give a name> run wheter user logged in or not> trigger at startup>
action
program/script will be powershell.exe
arguments
-ExecutionPolicy Bypass -File "C:\myscripts.ps1"
I wasn't able to make the Run Once Registry work, plus it wouldn't tun with admin cred if a non admin logged in. I also wasn't able to make a schedule task in power shell because my environment is all Win7 and power shell v4.
The solution i used was making a task sequence in SCCM that ran part 1 of my script, restarted, and then ran part 2.
So i have fairly easy powershell script that contains following:
import-module activedirectory
Get-ADUser -Filter *
remove-module activedirectory
If i run it from powershell it runs OK, but when i try to call it from CMD nothing happens, it just opens powershell and thats it. I am using following command to run it:
powershell.exe -file "D:\test.ps1"
I noticed also following thing, 2 powershell.exe processes run after i execute this. If i exit from CMD from one powershell then i start seeing lists that this PS query should be returning. Is there a way to get this working since i am trying to run ps script as scheduled job. The crucial part here is import module when i run it over cmd which is not happening for some reason.
It's powershell 2.0 running on Windows 2008R2. I tried this script on win 2012r2, works fine from CMD... Looks like ps 2.0 limitation?
Could be a couple of things going on here. Since your windows opens and closes you wont get to see any errors that might be occurring. What is your ExecutionPolicy set to? Get-ExecutionPolicy
When I make scheduled tasks of my scripts I usually set up my action as such
Program/Script = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments = -ExecutionPolicy Unrestricted -NoProfile -File C:\data\script.ps1
Start In = %SystemRoot%\system32\WindowsPowerShell\v1.0
Also, I don't believe it matters in this case but be sure you have the script "Running with highest privilege" if required.
I am trying to execute the following post build event code but I am getting an non-useful error :
"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
I have run the following PS script before I try :
Set-ExecutionPolicy unrestricted
What am I missing?
UPDATE
This is strange, I am not getting an error on VS now. but the script is not working. When I run it with powershell console I get the following error :
Visual Studio writes the post-build event script to a .BAT file and executes that using cmd.exe. So using & "<path-to-powershell>" won't work. Just execute:
Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
And if you think you're likely to run into execution policy issues on other machines that build the solution, consider going this route:
Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1"
You can reproduce the error in Powershell as follows:
"this is a string" -file "my.ps1"
It is taking the first as a string, the -file as the -f format flag and saying it doesn't have a value expression on the right for the format substitution.
Try like this:
& "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
(as Keith notes, this will not work as this is run from a bat file than Powershell.)
Or just:
powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
Before calling power-shell script from visual studio, set the ExecutionPolicy to unrestricted from power-shell window like this...
Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;
the call power-shell script in the following manner...
powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)
then in the script, you can always read the parameter like this...
param([string]$SolutionDir,
[string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");