Powershell Command in Windows Server 2019 Task Scheduler? - powershell

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

Related

Script startup is slow when started with "Run with Powershell"

When I start a script with "Run with Powershell" its starting slow.
First it is searching for avaiable modules and after this its asking for a Execution Policy Change. Is there a way to suspend them?
Thanks for the help!
Starting with version 3, Powershell does module autoloading, which you can disable by adding this line to your Powershell script (or profile):
$PSModuleAutoloadingPreference = “none”
Be careful, though, as this means that any cmdlet/function that's not part of the default modules Powershell loads at startup will not be available.
As an indication, I launched (get-command).count both with and without that line:
Module autoloading on: 1753
Module autoloading off: 213
If you need to load more modules, you can simply add Import-module instructions in your script.
As for the execution policy, you can also force it when launching powershell:
powershell -executionpolicy unrestricted <path to your script>
As was mentioned earlier, be careful with this parameter, as it will enable launching any Powershell script on your system. As long as you're in control of what scripts you're launching, there's nothing to be worried about.
Sure but understand the consequences
Set-ExecutionPolicy Unrestricted –Forc

Running a batch file in powershell

I am using the application LabTech to write scripts for Leo backup. I have a batch file on my local C drive (backup.bat). I need that file to run when a backup fails. How would I do this in powershell with commands? I looked on Google and could not find anything concrete.
Any help is appreciated. Please let me know if you need any more information.
Try this, used the -wait switch so that your powershell script pauses until the backup.bat is complete and the hidden switch so that it runs invisibly.
Start-Process -FilePath 'C:\Backup.bat' -Wait -WindowStyle Hidden
You can try replacing email line (or add under it)
& "path\to\your\file.bat"
if there are spaces. If not:
path\to\your\file.bat

How to open Powershell Console Window from Powershell

I am writing a script to use multiple plink (PuTTY) sessions as a Windows version of clusterssh. I am stuck however because I want to open multiple Powershell windows from powershell. When I type the command for powershell, it opens a new session. This is similar to typing bash in bash. I want multiple physical windows opening.
I tried -windowstyle as well as the other args to no avail. I was wondering if there is a way you know of. I really appreciate your help. I looked and didn't find anything already here. Thanks for your time.
This will open a new window.
Either:
start-process powershell
Or:
start powershell
if you are trying to open a new window and launch a new script:
start powershell {.\scriptInNewPSWindow.ps1}
This will do it:
Invoke-Item C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
This works for me:
$argList = "-file `"$Location\script.ps1`""
Start-Process powershell -argumentlist $argList
(The backticks are necessary. This can be copied outright.) Variables can be used in the "-file" parameter (such as one set at the beginning of the script to reflect the location of the file) and spaces can appear in the variable due to the backticks.
Edited to use a two-line solution (the "$argList" variable) because PowerShell can mangle things otherwise.
To start Powershell 6 from a PS console start pwsh might do the trick.
It starts in the same folder.
(I haven't delved into it but I guess PS6's pwsh.exe has to be in the path for it to work.)

While running a powershell script using installshield, Enable-ADFSEndpoint can not succeed

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.

Terminate PowerShell process after script executing

I have a TFS Build where I run PowerShell script. The problem is PowerShell.exe never stops after runnig and do nothing.
Script is signed by trusted sertificate and successfully runs on my BuildAgent from PowerShell and writes logs. But it don't to anything from build or from cmd.exe. PowerShell.exe just starts and do nothing.
P.S. PS script have Exit commands but it not help.
Thanks,
Roman
You can use, Stop-Process -Id $PID from within the script to end the current PowerShell process.
The issue was resolved.
Problem was security settings on BuildAgent. When I run script manually from BuildAgent user's account and choose "Run always" build starts working correctly.