Can some one help me in writing a hello world program with PowerShell and execute it using NSIS. I have gone through this link but I am not able to understand clearly what needs to be done. http://nsis.sourceforge.net/PowerShell_support
I already have bunch of PowerShell scripts written, I would like to execute them too.
Hello world program with PowerShell
14 posts
You need to call the powershell executable with the scritpname as parameter.
exec "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file d:\mydir\myscript.ps1"
But you should keep in mind on a 64 bit system there is a 32 bit and a 64 bit powershell installed. It may make a difference what powershell you are using depending on what the script is doing and if you are also using snapins, ....
exec "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file d:\mydir\myscript.ps1"
But you should keep in mind on a 64 bit system there is a 32 bit and a 64 bit powershell installed. It may make a difference what powershell you are using depending on what the script is doing and if you are also using snapins, ....
Hi thanks for the post I have done as following which is working fineOriginally Posted by th_mi View PostYou need to call the powershell executable with the scritpname as parameter.
exec "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file d:\mydir\myscript.ps1"
But you should keep in mind on a 64 bit system there is a 32 bit and a 64 bit powershell installed. It may make a difference what powershell you are using depending on what the script is doing and if you are also using snapins, ....
!include "x64.nsh"
Name "nsExec Test"
OutFile "nsExecTest.exe"
#ShowInstDetails show
Section "Output to variable"
nsExec::ExecToStack 'powershell.exe "& "Import-Module C:\Dorababu\PowerShell\Hello.psm1"'
Pop $0
Pop $1
DetailPrint '"ImportModules" printed: $1'
SectionEnd But the same I would like to achieve which is accepting parameters inside PS module script can you help me on thatThis is my PowerShell module script
# Filename: TestParameter.psm1
function TestParameter([string] $TestParam)
{
Write-Host
Write-Host '$TestParam'
Write-Host "Good-bye $TestParam! `n"
}
# end of script I know how to pass the variables if it is a direct script but how can I achieve it using NSIS nsExec::ExecToStackOriginally Posted by jpderuiter View Post
Hello Chaitanyansis
In your case I'd try the followerd. I only explain how it could work, not the NSIS stuff.
Assuming you have the followed ps1 script (do not use in this case psm1 since you need to use import-module. If you have a psm1 you shoudl handle this in the ps1 script you call.
Script test.ps1:
Param([Parameter(Mandatory=$False)][string]$param1="",
[Parameter(Mandatory=$False)][string]$param2="")
Write-host "Parameter: $param1 $param2"
How to call the script from normal command prompt:
powershell -file test.ps1 -param1 1 -param2 2
The result would be:
Parameter: 1 2
In your case I'd try the followerd. I only explain how it could work, not the NSIS stuff.
Assuming you have the followed ps1 script (do not use in this case psm1 since you need to use import-module. If you have a psm1 you shoudl handle this in the ps1 script you call.
Script test.ps1:
Param([Parameter(Mandatory=$False)][string]$param1="",
[Parameter(Mandatory=$False)][string]$param2="")
Write-host "Parameter: $param1 $param2"
How to call the script from normal command prompt:
powershell -file test.ps1 -param1 1 -param2 2
The result would be:
Parameter: 1 2
Hello th_mi,
I know how to execute a PowerShell script from command prompt but the thing is I have a requirement as follows. I will create a nsi dialog with some controls like text-boxes where user will enter value and submit so that on submit I need to execute the PowerShell script
I know how to execute a PowerShell script from command prompt but the thing is I have a requirement as follows. I will create a nsi dialog with some controls like text-boxes where user will enter value and submit so that on submit I need to execute the PowerShell script
exec "powershell.exe -file test.ps1 -param1 1 -param2 2"
Maybe you need the path to the ps1 file....!?
Maybe you need the path to the ps1 file....!?
Didn't worked
EXEC 'powershell.exe "& "C:\PowerShell\TestParameter.ps1 -param1 1"'
EXEC 'powershell.exe "& "C:\PowerShell\TestParameter.ps1 -param1 1"'
I tested it, for me it worked perfectly exactly the way I described. I used the script I posted here and also the exec call. The only thing I did was addeng the path to the powershell.exe and also to the test.ps1.Originally Posted by Chaitanyansis View PostDidn't worked
EXEC 'powershell.exe "& "C:\PowerShell\TestParameter.ps1 -param1 1"'
It worked as expected!
Hi th_mi when we are writing with out a function it is working fine but when I create the script as follows it didn't worked for meOriginally Posted by th_mi View PostI tested it, for me it worked perfectly exactly the way I described. I used the script I posted here and also the exec call. The only thing I did was addeng the path to the powershell.exe and also to the test.ps1.
It worked as expected!
function CallMe
{
Param([Parameter(Mandatory=$False)][string]$param1="")
Write-host "Parameter: $param1"
}
This is working fine
Param([Parameter(Mandatory=$False)][string]$param1="")
Write-host "Parameter: $param1"
You asked for a solution I gave one to you. If you don't like my solution it is up to you find a better one for you. 🙂
Hi I am not saying that your solution didn't worked, but initially in my post I posted the code with function, so I am just checking to work it with like the requirementOriginally Posted by th_mi View PostYou asked for a solution I gave one to you. If you don't like my solution it is up to you find a better one for you. 🙂
And from my understanding you will not get it to work in this specific situation with a script that uses the function keyword. But I may be wrong I'm using powershell only for a short time of 3 years, therefore I'm far away from a powershell professional.