calling Powershell script from Terraform script - powershell

I am trying to execute a powershell script from a terraform script. The powershell commands in the file is not being recognized while execution. Below is the code:
resource "null_resource" "example2" {
provisioner "local-exec" {
command = "powershell -file ./sample.ps1"
}
}
Below is the error:
null_resource.example2 (local-exec): Add-AzIotHubDevice : The term 'Add-AzIotHubDevice' is not recognized as the name of a cmdlet, function, script file, or operable
enter image description here

I think this is already answered here:
How to run a powershell script in terraform?
Also, I recommend calling the exact powershell .exe you want. Some servers have PowerShell v5 and v7. Execution policy can cause issues too.
Recommend command that you can combine with the other answer:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "./sample1.ps" -ExecutionPolicy "Bypass"
another example:
https://ripon-banik.medium.com/run-powershell-script-in-terraform-54d6586c2827

Related

Azure Function - PowerShell, installing az cmdlet :: The term 'az' is not recognized as the name of a cmdlet

I am attempting to install the az cmdlet onto Kudu for my Azure Function. I am currently following this guide:
How to install a PowerShell module in an Azure Function
... however - I am still getting the following error within my Azure Function:
az : The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at run.ps1: line 1
Steps I have done till now:
Created a module folder under D:\home\site\wwwroot\Communication_with_Azure_Container_Registry\>
Within the module folder I have added the contents of azure-cli/2.0.35/..., (which looks like this):
... Azure Function code is very simple to proof out the ability to install the cmdlet:
if (-not (Get-Module -Name "az"))
{
Write-Output "azure-cli not installed";
}
else
{
Write-Output "azure-cli installed";
}
$test = 'az --help'
Invoke-Expression $test
Write-output `n$test
Question:
Is there something within my configuration that is not allowing for the az cmdlet to install?
Is there an alternative way to gain access to the azure-cli without implementing the node module?
I solved the following part of your problem
az : The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at run.ps1: line 1
If you execute
Test-Path -Path 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin'
You will probably get False. This means that you need to install the Azure CLI eg from https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest
I haven't testing this myself, but according to https://blogs.msdn.microsoft.com/powershell/2017/02/24/using-powershell-modules-in-azure-functions/ you should be able to do an Import-Module. In their example...
Write-Output “Loading Wunderlist Module”
import-module ‘D:\Home\site\wwwroot\HttpTriggerPowerShellDemo\Modules\Wunderlist\1.0.10\Wunderlist.psm1’
$Result = Get-Help Get-WunderlistUser | Out-String
Write-Output $Result
Install Azure CLI
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
This should give you the az command

Alternate for esbimportutil.exe for PowerShell BizTalk

I am working on a PowerShell Deployment scripts for BizTalk. I want to import an itinerary in XML format using PowerShell. Commands available for this task is esbimportutil.exe. But this works only in Command Prompt and not in PowerShell.
The error shows is :
The term 'esbimportutil.exe' is not recognized as the name of a cmdlet, function, script file,
or operable program.
I run the PowerShell as an Administrator and even tried running the command from the source root location but still no use.
I got the solution. The problem was resolved by using a simple command:
Start-Process -FilePath "...\esbimportutil.exe" -ArgumentList $argument
The command "start-process" did the magic.

Running Powershell script from SSIS with OnTap cmdlets throws error, but not when run from PS Cmd Line?

We are trying to run a Poweshell Script that uses the OnTap PS Modules, from SSIS, when we do, an error is issued:
Error: The term 'Connect-NaController' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
But when we run the same script from the Powershell Command Line, then the script runs just fine. So I think the scripts are fine.
So I'm wondering if the security context is different or we have to do something more explicitly in our call from SSIS?
When we call the script from SSIS we use: -ExecutionPolicy ByPass
Thanks!
In SSIS i had to set the FULL PATH of the script instead of ./scriptfile

How Can I Get PowerShell to Execute MpCmdRun.exe

PowerShell runs programs such as IpConfig and WhoAmI just as cmd would. However, I am stumped trying to run MpCmdRun.exe
Clear-Host
Set-Location "C:\Program Files\Windows Defender"
Get-ChildItem
mpcmdrun.exe
Result
Error:
mpcmdrun.exe : The term 'mpcmdrun.exe' is not recognized as the name of a cmdlet, function, script file, or operable
program.
You are doing mpcmdrun.exe. You have to do .\mpcmdrun.exe as the current folder . is not in PATH in Powershell unlike in cmd.
PS:
I wonder if you read the entire message that Powershell would have spit out when you did as you said:
Suggestion [3,General]: The command MpCmdRun.exe was not found, but
does exist in the current location. Windows PowerShell does not load
commands from the current location by default. If you trust this
command, instead type ".\MpCmdRun.exe". See "get-help
about_Command_Precedence" for more details.
PPS:
The other commands ran because they were in PATH.

Set up PowerShell Script for Automatic Execution

I have a few lines of PowerShell code that I would like to use as an automated script. The way I would like it to be able to work is to be able to call it using one of the following options:
One command line that opens PowerShell, executes script and closes PowerShell (this would be used for a global build-routine)
A file that I can double-click to run the above (I would use this method when manually testing components of my build process)
I have been going through PowerShell documentation online, and although I can find lots of scripts, I have been unable to find instructions on how to do what I need. Thanks for the help.
From http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
If you're willing to sully your beautiful PowerShell script with a little CMD, you can use a PowerShell-CMD polyglot trick. Save your PowerShell script as a .CMD file, and put this line at the top:
#PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^#PowerShell.*EOF$')) & goto :EOF
If you need to support quoted arguments, there's a longer version, which also allows comments. (note the unusual CMD commenting trick of double #).
##:: This prolog allows a PowerShell script to be embedded in a .CMD file.
##:: Any non-PowerShell content must be preceeded by "##"
##setlocal
##set POWERSHELL_BAT_ARGS=%*
##if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
##PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^##'))) & goto :EOF
Save your script as a .ps1 file and launch it using powershell.exe, like this:
powershell.exe .\foo.ps1
Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run.
Run Script Automatically From Another Script (e.g. Batch File)
As Matt Hamilton suggested, simply create your PowerShell .ps1 script and call it using:
PowerShell C:\Path\To\YourPowerShellScript.ps1
or if your batch file's working directory is the same directory that the PowerShell script is in, you can use a relative path:
PowerShell .\YourPowerShellScript.ps1
And before this will work you will need to set the PC's Execution Policy, which I show how to do down below.
Run Script Manually Method 1
You can see my blog post for more information, but essentially create your PowerShell .ps1 script file to do what you want, and then create a .cmd batch file in the same directory and use the following for the file's contents:
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"
Replacing MyPowerShellScript.ps1 on the 3rd line with the file name of your PowerShell script.
This will allow you to simply double click the batch file to run your PowerShell script, and will avoid you having to change your PowerShell Execution Policy.
My blog post also shows how to run the PowerShell script as an admin if that is something you need to do.
Run Script Manually Method 2
Alternatively, if you don't want to create a batch file for each of your PowerShell scripts, you can change the default PowerShell script behavior from Edit to Run, allowing you to double-click your .ps1 files to run them.
There is an additional registry setting that you will want to modify so that you can run scripts whose file path contains spaces. I show how to do both of these things on this blog post.
With this method however, you will first need to set your execution policy to allow scripts to be ran. You only need to do this once per PC and it can be done by running this line in a PowerShell command prompt.
Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy RemoteSigned -Force' -Verb RunAs
Set-ExecutionPolicy RemoteSigned -Force is the command that actually changes the execution policy; this sets it to RemoteSigned, so you can change that to something else if you need. Also, this line will automatically run PowerShell as an admin for you, which is required in order to change the execution policy.
Source for Matt's answer.
I can get it to run by double-clicking a file by creating a batch file with the following in it:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
you can use this command :
powershell.exe -argument c:\scriptPath\Script.ps1