Old 29th August 2016, 07:20   #1
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Hello world program with PowerShell

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.
Chaitanyansis is offline   Reply With Quote
Old 29th August 2016, 11:23   #2
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
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, ....

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Old 29th August 2016, 13:23   #3
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Quote:
Originally Posted by th_mi View Post
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, ....
Hi thanks for the post I have done as following which is working fine

PHP Code:
!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 that

This is my PowerShell module script

PHP Code:
# Filename: TestParameter.psm1
function TestParameter([string$TestParam)
{
    
Write-Host
    Write
-Host '$TestParam'
    
Write-Host "Good-bye $TestParam! `n"
}
# end of script 
Chaitanyansis is offline   Reply With Quote
Old 29th August 2016, 14:50   #4
jpderuiter
Major Dude
 
Join Date: Feb 2007
Posts: 672
https://devcentral.f5.com/articles/p...for-parameters
jpderuiter is offline   Reply With Quote
Old 29th August 2016, 15:36   #5
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Quote:
Originally Posted by jpderuiter View Post
I know how to pass the variables if it is a direct script but how can I achieve it using NSIS nsExec::ExecToStack
Chaitanyansis is offline   Reply With Quote
Old 30th August 2016, 13:10   #6
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
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

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Old 31st August 2016, 05:36   #7
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
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
Chaitanyansis is offline   Reply With Quote
Old 31st August 2016, 09:15   #8
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
exec "powershell.exe -file test.ps1 -param1 1 -param2 2"

Maybe you need the path to the ps1 file....!?

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Old 31st August 2016, 11:28   #9
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Didn't worked

EXEC 'powershell.exe "& "C:\PowerShell\TestParameter.ps1 -param1 1"'
Chaitanyansis is offline   Reply With Quote
Old 2nd September 2016, 05:23   #10
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
Quote:
Originally Posted by Chaitanyansis View Post
Didn't worked

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.

It worked as expected!

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Old 2nd September 2016, 05:45   #11
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Quote:
Originally Posted by th_mi View Post
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.

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 me

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"
Chaitanyansis is offline   Reply With Quote
Old 2nd September 2016, 07:18   #12
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
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.

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Old 2nd September 2016, 08:36   #13
Chaitanyansis
Junior Member
 
Join Date: Aug 2016
Posts: 14
Quote:
Originally Posted by th_mi View Post
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 requirement
Chaitanyansis is offline   Reply With Quote
Old 2nd September 2016, 09:59   #14
th_mi
Member
 
Join Date: Sep 2005
Location: Sitting next to my desk
Posts: 56
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.

This signature is currently on vacation!
th_mi is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Tags
powershell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump