Need to self sign a powershell script to use in the task manager - powershell

I have a powershell script that if I open Powershell (right click> run as administrator) and post the code it works just fine. However when I try to create a task in task scheduler, the script will not work. I am assuming I need to sign the powershell script. I searched for a fix and came up with a Command that uses "makecert" however when I try to run it I get" 'makecert' is not recognized as an internal or external command.

you can just use "-ExecutionPolicy Bypass" in the program task arguments.

Related

Task Scheduler - Powershell script not firing?

I've created numerous scripts in PowerShell that are working as intended if I execute them directly, however, when I try and setup a schedule to run these in Task Scheduler (to run with highest privileges) it doesn't seem to be running anything at all.
I'm running the following in my actions:
powershell.exe -ExecutionPolicy Bypass -File C:\PS\Mailboxes\CheckForwardingList.ps1
I'm getting a "Last Run Result" of 0x0 and the particular purpose of the above script is to generate a TXT file from EXO which it then mails out via SMTP and I've yet to receive any emails and I also don't see any TXT being generated in the folder where the script is located.
I do have two additional scripts setup which aren't running but once I've addressed the issue on the above this should quickly rectify the problems.
I like to test my PowerShell scripts from a command prompt first.
For example a script called C:\Tests\Test-PowerShellScriptsRunning.ps1 that only contains the following one liner helps me to test if scripts can run successfully on a machine
Write-Host -ForegroundColor Yellow "If you see this, then your script is running"
Next, I run this script from a command prompt, to get the syntax right in my scheduled task:
powershell.exe -nologo -file c:\Tests\Test-PowerShellScriptsRunning.ps1
Of course, you can add the -Executionpolicy bypass parameter, but I prefer to test the execution policy first.
However, as you are running a script that connects to ExchangeOnline, I suspect it has to do with the user you are running this task under. Does the script run if you run this task under your credentials or are the credentials stored on the system or in the script?
You might want to check this article to see how you can register an app and authenticate without storing your credentials on the machine to run the script unattended: App-only authentication for unattended scripts in the EXO V2 module

Command prompt not executing powershell scripts

I tried running a build today and found that my command prompt is not able to execute powershell scripts. To confirm this, I created a test powershell script which simply writes to console. The script works fine when run from powershell, but is not recognizable by the command prompt. Screenshots below.
test script working from powershell
test script not working from command prompt
Any suggestions/explantion as to why this is happening?
Resolved it. I created a profile.ps1 script under ..\Documents\WindowsPowerShell folder to load custom settings for powershell. Removing profile.ps1 from the folder or adding a -NoProfile switch parameter solves this.

Unable to load .ps1 powershell script from Powershell command (console)

I am stuck in this weird problem where I am trying to execute a powershell script from the powershell command prompt. But neither do I get any errors nor the script is loaded.
I have script in C:\temp\myFunction.ps1 (which has a method getMyName() )
I open the powershell command and navigate to this directory and execute
./myFunction.ps1
then there are no errors and return back to the next line in the prompt. But when I try to call the function getMyName - I get error getMyName is not recognised.
I have set the Execution-Policy to Unrestricted, I am running the powershell as Administrator
Try dot sourcing your script:
. .\myFunction.ps1
It's basic problem of Powershell script. Set the Path where you physically saved your file and then execute the Powershell script. One more thing
1. start your command window run as admin.
2. set the Powershell script policy for execution.

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.

How to Execute a PowerShell Script from SSIS

Does anyone know how to execute a PowerShell script from SSIS? I have created the script and it works from from the command line. The script takes a couple of command line parameters, which work fine when called from cmd.exe.
I'm using an Execute Process Task in SSIS and cannot get the script file to execute. I'm using expressions to pass in the name of the script and the command line arguments. The task is returning an incomplete string token error.
From VS to launch PSH with an extra script (for a Cmdlet project) I use the following commandline:
powershell -noexit -command ". ./Startup.ps1"
The -noexit will keep the instance around (so you wouldn't want that), putting all the real commands in a script to be dot-sourced avoids a really long command line.
Go to Execute Process Task Editor -> Process tab and set the following options:
Executable - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments - -File ".\PowershellScript/ps1" "arg_1_value" "arg_2_value" ... "arg_n_value"
Refer to the below screenshot: