Page 1 of 1

Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Tue Apr 03, 2018 7:52 am
by vino1493
Hi,

I have a ps1 code that could copy files from one folder to another, I'm trying to trigger this via TI process for which I used:

Code: Select all

 ExecuteCommand ( 'PowerShell E:\powershell.ps1' , 1);
What I'm trying to achieve now is can we pass the source and target folder names as a parameter via TI?

something like below:

Code: Select all

 vDirectory = '\\*****\tm1servers$';
 ExecuteCommand ( 'PowerShell E:\tm1servers\powershell.ps1', $name, vDirectory , 1);
I know the above syntax is wrong and doesn't work, but is there any other way?

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Tue Apr 03, 2018 11:54 am
by orlando
Hi,

ExecuteCommand has only the two paramaters. One for the CommandLine and one for Wait. So you have to do all your wishes in the CommandLine.

I don't know much about PowerShell, but i would try the following:

vDirectory = '\\*****\tm1servers$';
$Name = WHATEVER_YOU_NEED_HERE'
sCmdPath = 'PowerShell E:\tm1servers\HQ_PS_Performance\Scripts\powershell.ps1';
sCommand = sCmdPath |' ' | $Name |' '| vDirectory;

ExecuteCommand ( sCommand, 1);

Best regards,

orlando

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Wed Apr 04, 2018 3:13 am
by vino1493
@Orlando Thanks for your reply, this can be done for .exe files not .ps1, I guess I will have to hard code.

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Wed Apr 04, 2018 7:12 am
by orlando
@vino1493:
sorry for that. i made a short search yesterday before the reply an found this http://blog.simonw.se/powershell-functi ... eter-sets/ and thought powershell is able to handle parameters.

best regards,
orlando

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Wed Apr 04, 2018 10:30 am
by Wim Gielis
vino1493 wrote: Wed Apr 04, 2018 3:13 am @Orlando Thanks for your reply, this can be done for .exe files not .ps1, I guess I will have to hard code.
Where did you read that ?

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 7:01 am
by vino1493
@Wim Gielis I didn't read it anywhere, tried many trial and errors. Moreover executecommand doesn't accept parameters.

If you have any other thoughts or ideas to it. Please share it here, I would be glad to hear it. :)

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 8:29 am
by lotsaram
vino1493 wrote: Thu Apr 05, 2018 7:01 am @Wim Gielis I didn't read it anywhere, tried many trial and errors. Moreover executecommand doesn't accept parameters.

If you have any other thoughts or ideas to it. Please share it here, I would be glad to hear it. :)
I don't really know what I am reading here, you have me scratching my head to be honest.
But the clear answer is of course you can. Yes the ExecuteCommand function only accepts 2 arguments of i) the command line to call and ii) whether to wait for it. But there is nothing to stop you from bundling whatever runtime parameters you want into the command line string. Obviously in whatever format the relevant command line program requires them.

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 9:19 am
by vino1493
@lotsaram thanks for your comment, could you please share me a sample command line string for power shell scripts!

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 9:25 am
by orlando
vino1493 wrote: Thu Apr 05, 2018 9:19 am @lotsaram thanks for your comment, could you please share me a sample command line string for power shell scripts!
got a sleeples night because of these parameter and tried this.

My PowerShellscipt:

######################
Param(
[string]$text,
[string]$FilePath

)

$text > $FilePath
######################

My TI Script

#######################

sParam1 = 'WhateverNeededHere';
sPath = 'd:\test\Myfile.txt';
sCommand = 'Powershell D:\TEST\file.ps1 -text '| sParam1 |' -FilePath '| sPath;

ExecuteCommand(sCommand, 0);

#######################

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 11:01 am
by vino1493
I hope 'text' and 'Filepath' are mere variables and not functions. I altered my code as follows, but it doesn't work. Could you please have a look at it:

TI process

Code: Select all

sServer = 'testd';
sInstance = 'salee';
sCommand = 'PowerShell E:\tm1servers\Scripts\Final.ps1 -Server '| sServer |' -Instance '| sInstance;

ExecuteCommand ( sCommand, 0);
PS1 file:

Code: Select all

Param(
  [string]$Server,
  [string]$Instance
  )
  $Server > $Instance
  Get-ChildItem "\\$Server\tm1servers$\$Instance\LogFiles\" | where {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Move -Destination "TARGET:\$Server\$Instance\LogFiles\"

And what is the use of '$text > $Filepath' in the version you shared?

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 12:05 pm
by orlando
vino1493 wrote: Thu Apr 05, 2018 11:01 am
And what is the use of '$text > $Filepath' in the version you shared?
This just writes the value of $text to the file specified in $Filepath

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 12:08 pm
by orlando
vino1493 wrote: Thu Apr 05, 2018 11:01 am

Code: Select all

  Get-ChildItem "\\$Server\tm1servers$\$Instance\LogFiles\" | where {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Move -Destination "TARGET:\$Server\$Instance\LogFiles\"
does it copy the files, when you try the values of your variables in the UNC Path?

Get-ChildItem "\\wa01095d\tm1servers$\HQ_PS_Performance\LogFiles\" | where {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Move -Destination "TARGET:\wa01095d\HQ_PS_Performance\LogFiles\

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 12:46 pm
by vino1493
I would like to get the server and instance from control cube and pass them as a parameter to the power shell script. This script will move the log files from that server\instance\logfiles folder to a share point.

the PS script I use is

Code: Select all

Get-ChildItem "\\$Instance\tm1servers$\HQ_PS_Performance\LogFiles\" | where {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Move -Destination "TARGET:\$Server\$Instance\LogFiles\"
Here variables are $Server and $Instance, I want to create a TI process that could pass the servername and instance name to this script.
Please help me out.

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Apr 05, 2018 9:28 pm
by macsir
| Move -Destination "TARGET:\$Server\$Instance\LogFiles\"
Off the top of my head, MOVE is dos command rather than PS command. Use Move-Item instead. https://docs.microsoft.com/en-us/powers ... wershell-6
And better to show powershell log when debugging it.

Re: Is it possible to pass parameters to a powershell (.ps1) file?

Posted: Thu Feb 21, 2019 5:15 pm
by vino1493
Would like to answer my own question:

Below is the code I used to pass parameters to a PowerShell script. I have not mentioned the variable declaration part.
spath is the variable where I declared the Script's path\filename something like 'E:\scripts\logs.ps1'
Thanks for all your support!

Code: Select all

sPsCmd = '"& {
' |spath | '
-From ' | sFrom | ' 
-To ' | sTo | ' 
-cc ' | sCC | '
-Subject ' | '  \"  ' | sSubject | '  \"
-Attachments ' | sAttachment | '
-Body ' | '  \"  ' | sBody | '  \"
-Priority ' | sPriority | '	
-SmtpServer ' | sSmtpServer | ' 
}"';
# execute command
ExecuteCommand ( 'powershell.exe -command ' | sPsCmd, 1 );