I am trying to schedule a Powershell task to start automatically after Windows starts (to restart a process in case of a crash or maintenance). My knowledge of Powershell is very limited, but when I manually want to start the task, I need to go to the folder in Explorer, and open Powershell from there which defaults me to "PS C:\Users{{username}}\Documents\fso\fso> " where I then type in "./watchdog.bat". In trying to setup Task Scheduler, it seems like it's looking for a .ps1 file which I have no knowledge of. Would someone mind helping me with this?
Much thanks in advance :)
Thanks for Mathias R. Jessen's help,
The solution is add a command or file to run, in the "Add arguments" field, either put -Command './watchdog.bat' or -File "C:\path\to\script.ps1", where script.ps1 would be a powershell script calling watchdog.bat
I want execute following command without opening admin PowerShell prompt.It should be execute on normal prompt. like we are sudo useradd <username> in Linux.
Enable-PSRemoting;
Set-Item wsman:\\localhost\\client\\trustedhosts $hostIP -Force;
Restart-Service WinRM
I am going integrate this code into C#. Please help me to solve this issue.
What you're asking is not possible directly from a PowerShell script, at least as far as I know. What you can do is create a scheduled task running that script with admin privileges, and then invoke the scheduled task as a normal user.
That is not possible.
Allowing that would be a major security issue in Windows.
We need to run a powershell comman in our NSIS installer. Running the command in the powershell is no problem and running the command with
'powershell.exe "& "our command here"'
in the windows command line is no problem either. If we use the PowerShellExec Macro
${PowerShellExec} "our command"
or if we use the NSexec command does not matter. We always get the error message that "The access to the path has been denied". We found out that it might be a problem with the security policy of the powershell. Is there a way to change the security policy in NSIS or is there another way to call the command inside the installer?
Assuming you're testing on systems that only have PowerShell v2 installed, this appears to be a known issue with PowerShell v2.
There are several workarounds mentioned in both the Nullsoft forum and on MS Connect. The most straightforward solution is to add -InputFormat None to the command line, but that is undocumented.
Alternately, if it's the execution policy that's throwing you, you can try adding -ExecutionPolicy Bypass to the command line.
I am trying to invoke a powershell script from installshield, but I am getting errors on the Enable-ADFSEndpoint line.
When I paste this script into powershell itself, it works properly. But when running through install script's LaunchAppAndWait() function, it fails. Here is my installshield code:
Param1=WINDISK +"\\Windows\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe";
Param2="-ExecutionPolicy remotesigned -File \""+ INSTALLDIR +"PSServer\\Support\\adfs_cmdlets.ps1\"";
LaunchAppAndWait(Param1, Param2, LAAW_OPTION_WAIT);
And the script code is as follows:
Add-PSSnapin Microsoft.Adfs.PowerShell
#ENABLE ENDPOINTS
Enable-ADFSEndpoint –TargetAddress "/adfs/services/trust/2005/windows"
Enable-ADFSEndpoint –TargetAddress "/adfs/services/trust/13/windows"
There is of course more script than this, but upon execution through installshield, it isn't getting further.
I am very much so a novice at installshield, so any hints may help and are much appreciated. Again, it is my current thought that the problem lays within installshield, because when my powershell script is invoked through the powershell command prompt, it works properly. Thank you in advance to anyone that may respond.
I also realize it is likely that I have left out important information, and am of course willing to edit it back in upon request.
I'm using Hudson version 1.324 for CI and have a couple of issues:
Environment:
Windows Server 2008
Powershell v1.0
Hudson 1.324 running as a service
Hudson Powershell Plugin installed
Psake (aka. "Powershell Make/Rake" available from Github) 0.23
(All current/latest versions as of this initial post)
I have a Powershell (PS) script that works to compile, run NUnit tests, and if successful, create a 7z file of the output. The PS script works from the command line, on both my local development box as well as the CI server where Hudson is installed.
1) Execution Policy with Powershell.
I initially ran a PS console on the server, ran Set-ExecutionPolicy Unrestricted, which allows any script to be run. (Yes, I realize the security concerns here, I'm trying to get something to work and Unrestricted should remove the security issues so I can focus on other problems.)
[This worked, and allowed me to fire off the PS build script from Hudson yesterday. I then encountered another problem, but we'll discuss that more in item #2.]
Once Hudson could fire off a PS script, it complained with the following error:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell "&
'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'" The term
'OzSystems.Tools\psake\psake.ps1' is not recognized as a cmdlet, funct
ion, operable program, or script file. Verify the term and try again.
At line:1 char:2
+ & <<<< 'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'"
Using the same command line, I am able to successfully execute the PS script from the command line manually. However Hudson is unable to get PS to do the same. After looking at additional PS documentation I also tried this:
"& 'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'"
and got a similar error. There does not appear to be any documentation for the Powershell plugin for Hudson. I've gone through all the Powershell plugin files and don't see anything that's configurable. I can't find a log file for Hudson to get additional information.
Can anyone help me past this?
2) I spent yesterday wrestling with #1. I came in this AM and tried to dig in again, after restarting the Hudson server/service, and now it appears that the ExecutionPolicy has been reset to Restricted. I did what worked yesterday, opened a PS console and Set-ExecutionPolicy to Unrestricted. It shows Unrestricted in the PS console, but Hudson says that it doesn't have rights to execution PS scripts. I reopened a new PS console and confirmed that the ExecutionPolicy is still Unrestriced -- it is. But Hudson evidently is not aware of this change. Restarting Hudson service again does not change Hudson's view of the policy.
Does anyone know what's going on here?
Thanks, Derek
I just ran into the problem of running powershell scripts in hudson. The thing is that you are running a 32-bit process of Java, and you've configured Hudson for 64-bit but not for 32-bit. See the following thread we created at microsoft.
http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/a9c08f7e-c557-46eb-b8a6-a19ba457e26d
If your lazy.
1. Start powershell (x86) from the start menu as administrator
2. Set the execution policy to remotesigned
Run this once and your homefree.
When Running PowerShell from a scheduled task or Hudson you want to:
Specify the -ExecutionPolicy parameter (in your case: -Ex Unrestricted)
Specify that command using either -Command { ... } or -File NOT BOTH and not without specifying which you mean.
Try this (except that I don't recommend using relative paths):
PowerShell.exe -Ex Unrestricted -Command "C:\Path\To\OzSystems.Tools\psake\psake.ps1" ".\oz-build.ps1"
To be clear, this will work too:
PowerShell.exe -Ex Unrestricted -Command "&{&'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'}"
The first string after -Command is interpreted as THE NAME OF A COMMAND, and every parameter after that is just passed to that command as a parameter. The string is NOT a script, it's the name of a command (in this case, a script file)... you cannot put "&'OzSystems.Tools\psake\psake.ps1'" but you can put "OzSystems.Tools\psake\psake.ps1" even if it has spaces.
To quote from the help (run PowerShell -?) emphasis mine:
-Command
Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.
If the value of Command is "-", the command text is read from standard
input.
If the value of Command is a script block, the script block must be enclosed
in braces ({}). You can specify a script block only when running PowerShell.exe
in Windows PowerShell. The results of the script block are returned
to the parent shell as deserialized XML objects, not live objects.
If the value of Command is a string, Command must be the last parameter
in the command , because any characters typed after the command are
interpreted as the command arguments.
I have been having the same problems as you (as you've seen from my comments). I have given up on the powershell launcher and moved to running things using the batch file launcher. Even though I had set the system to unrestricted that setting didn't seem to matter to hudson's launcher. I don't know if it runs in some other context or something, even adding things to the global profile.ps1 didn't seem to help. What I ended up doing was running
powershell " set-executionpolicy Unrestricted; & 'somefile.ps1'"
which does what I need, although it isn't ideal. I've e-mailed the plugin author about this and will update.
For question #1, try this (assuming you are using PowerShell 2.0):
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell -executionPolicy Unrestricted -file OzSystems.Tools\psake\psake.ps1 C:\{path}\oz-build.ps1"
You are using "." for the path to oz-build.ps1. I suspect you will need to provide the full path to your oz-build.ps1 file to make this work. Unless the infrastructure that executes the command above happens to have the current dir set correctly. And even if it is set correctly for the "process", that only matters to .NET/Win32 API calls and not to PowerShell cmdlets. Current dir in PowerShell is tracked differently than the process's current dir because PowerShell can have multiple runspaces running simultaneously. That sort of global, mutable value doesn't work in this concurrent scenario.
As for question #2, what account does the Hudson service run under? Make sure that account has executed Set-ExecutionPolicy RemoteSigned (or unrestricted).
I just got through this exact problem. What a pain!
If you are running a 32-bit JVM on a 64-bit Windows, make sure that you set the execution policy for the 32-bit Powershell interface. I found my 32 bit executable here:
C:\Windows\syswow64\Windowspowershell\v1.0\powerhsell.exe
The 32- and 64-bit Powershell environments are completely distinct so setting the execution policy in one has no effect on the other.