Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run powershell script that doesn't have ps1 extension

I am using ops tool Rundeck that allows for execution of arbitrary scripts. I enter the script text in the web application widget and upon execution, Rundeck saves it to temporary file and calls interpreter. The problem is that the temporary file doesn't have ps1 extension and Powershell refuses to execute it.

Is there any way to set up Powershell to ignore extension ?

=== EDIT 2018 ===

Rundeck now has an option for this within a job definition.

like image 395
majkinetor Avatar asked Aug 20 '14 08:08

majkinetor


People also ask

How do I run a ps1 file extension?

Run Your PowerShell Scripts. After configuring the execution policy, you can run PowerShell scripts. To run a script, open a PowerShell window, type the script's name (with or without the . ps1 extension) followed by the script's parameters (if any), and press Enter.

How do I run a ps1 script from the command line?

ps1 files are interpreted by PowerShell, the Command Prompt (CMD) cannot work with PowerShell scripts directly. If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1.

Can you use a different file extension like .txt for the run ps1 file?

By default a file of type script ( . ps1 ) is created, but it can be saved with a new name and extension.


1 Answers

I know I'm not strictly answering your questions regarding setting up PowerShell to ignore extensions, but here is one way of executing code from a textfile:

Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content .\file.txt)))

This reads the contents of file.txt and converts it into a scriptblock, before executing it using Invoke-Command.

like image 75
ojk Avatar answered Oct 27 '22 12:10

ojk