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

Post Reply
vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » Tue Apr 03, 2018 7:52 am

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\HQ_PS_Performance\Scripts\powershell.ps1', $name, vDirectory , 1);
I know the above syntax is wrong and doesn't work, but is there any other way?

User avatar
orlando
Posts: 24
Joined: Fri Aug 04, 2017 8:27 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2010

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

Post by orlando » Tue Apr 03, 2018 11:54 am

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

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » 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.

User avatar
orlando
Posts: 24
Joined: Fri Aug 04, 2017 8:27 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2010

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

Post by orlando » Wed Apr 04, 2018 7:12 am

@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

Wim Gielis
MVP
Posts: 1669
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1
Version: PAL 2.0
Excel Version: 2016
Location: Brussels, Belgium
Contact:

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

Post by Wim Gielis » Wed Apr 04, 2018 10:30 am

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 ?
Best regards,

Wim Gielis

Excel Most Valuable Professional, 2011-2014
http://www.wimgielis.com ==> 105 TM1 articles and a lot of custom code
Newest blog article: Looping over input files

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » 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. :)

lotsaram
MVP
Posts: 3054
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TM1, CX
Version: TM1 10.2.2 PA 2.0x
Excel Version: 2010 2013 365
Location: Switzerland

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

Post by lotsaram » Thu Apr 05, 2018 8:29 am

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.
Please place all requests for help in a public thread. I will not answer PMs requesting assistance.

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » 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!

User avatar
orlando
Posts: 24
Joined: Fri Aug 04, 2017 8:27 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2010

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

Post by orlando » Thu Apr 05, 2018 9:25 am

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);

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

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » Thu Apr 05, 2018 11:01 am

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 = 'wa01095d';
sInstance = 'HQ_PS_Performance';
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?

User avatar
orlando
Posts: 24
Joined: Fri Aug 04, 2017 8:27 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2010

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

Post by orlando » Thu Apr 05, 2018 12:05 pm

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
Last edited by orlando on Thu Apr 05, 2018 12:08 pm, edited 1 time in total.

User avatar
orlando
Posts: 24
Joined: Fri Aug 04, 2017 8:27 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2010

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

Post by orlando » Thu Apr 05, 2018 12:08 pm

vino1493 wrote:
Thu Apr 05, 2018 11:01 am

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


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\

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » Thu Apr 05, 2018 12:46 pm

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.

User avatar
macsir
Community Contributor
Posts: 507
Joined: Wed May 30, 2012 6:50 am
OLAP Product: TM1, SSAS
Version: 9.5 - 10.2
Excel Version: 97 - 2013
Contact:

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

Post by macsir » Thu Apr 05, 2018 9:28 pm

| 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.
In TM1,the answer is always yes though sometimes with a but....
http://tm1sir.blogspot.com.au/

vino1493
Posts: 13
Joined: Tue Mar 14, 2017 12:01 pm
OLAP Product: cognos tm1
Version: 10.2.2
Excel Version: excel 2013

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

Post by vino1493 » Fri May 11, 2018 7:54 am

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 );

Post Reply