PowerShell issue

Post Reply
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

PowerShell issue

Post by Wim Gielis »

Hello all,

I'm trying to achieve the following. In several load processes I have a line of AsciiOutput in case an error occurs.
In the TM1 logging directory I get files with a certain filename pattern, involving the name of the offending process and a timestamp. The file itself contains 1 line, a description of the error.

Now, when all loads are finished, I want to have a chore that sends an email containing all files and their contents - as a summary for the TM1 administrator to look at, process by process.

I wrote a PowerShell script as follows. When I use TI to create a text file containing the script (Prolog tab) and I execute it with ExecuteCommand and the -File option (Epilog tab), it works perfectly. A csv file contains all "filename: error description" combinations.

Code: Select all

vPS = 'Get-ChildItem "D:\Logs\a.txt" | ';
vPS = vPS | 'foreach-object { $fname=[System.IO.Path]::GetFileNameWithoutExtension($_); ';
vPS = vPS | 'get-content $_ | foreach-object { echo $fname": "$_ >> "D:\Logs\output.csv"}}';

DataSourceAsciiQuoteCharacter = '';
AsciiOutput( 'D:\Logs\test.ps1', vPS );
ExecuteCommand( 'powershell.exe -ExecutionPolicy ByPass -file "D:\Logs\test.ps1"', 0 );
However, when I use the same PowerShell script in TI, and I execute it with ExecuteCommand and the -Command option, it does not work. A csv file is not created:

Code: Select all

vPS = 'Get-ChildItem "D:\Logs\a.txt" | ';
vPS = vPS | 'foreach-object { $fname=[System.IO.Path]::GetFileNameWithoutExtension($_); ';
vPS = vPS | 'get-content $_ | foreach-object { echo $fname": "$_ >> "D:\Logs\output.csv"}}';

ExecuteCommand( 'powershell.exe -Executionpolicy ByPass -command ' | vPS, 0 );
Now, if I take out the variable $fname in the output (last line), I get a file. So it seems related to that variable. But here I am stuck !

I am aware that a DOS command can also work, but I am just eager to know what is at stake here.

What is the difference between TI running from a ps1 file (that it creates first by himself), and passing a command ?

Note; in the PS script above I use only 1 file (a.txt) as the source but in reality I loop over the files obeying a filename pattern, files only produced today, etc.

Thanks a lot !

Wim
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: PowerShell issue

Post by Wim Gielis »

Allright, I got it. Again, double quotes, single quotes, spaces, backticks misery...

Here is the full code you need in TI. Individual text files are called: 'ERROR - nameoftheprocess_20190716 012345.txt' (for today)

Code: Select all

cLog_Folder = GetProcessErrorFileDirectory;
cAll_Errors = 'output.csv';
cFullname = cLog_Folder | cAll_Errors;

vPS = 'Get-ChildItem "' | cLog_Folder | 'ERROR` -` *_????????` ??????_?????.txt" | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | foreach-object { $fname=[System.IO.Path]::GetFileNameWithoutExtension($_); $fname_short=$fname.substring(8, $fname.Length - 14); get-content $_ | foreach-object { echo $fname_short'': ''$_`n >> "' | cFullname | '"}}';

ExecuteCommand( 'powershell.exe -command ' | vPS, 0 );
The output.csv file then contains x lines, with this structure for each line: 'name of the process: error description'
Last step will be to send an email with the output.csv file as contents. Or, have PowerShell send the email and skip the intermediate output.csv file alltogether :-)

If someone can simplify all this, please do so.

Code: Select all

# alternatively, with a temporary file, but that's not so neat:

cLog_Folder = GetProcessErrorFileDirectory;
cAll_Errors = 'output.csv';
cFullname = cLog_Folder | cAll_Errors;

AsciiDelete( cFullname );
DataSourceAsciiQuoteCharacter = '';

vPS = 'Get-ChildItem "' | cLog_Folder | 'ERROR` -` *_????????` ??????_?????.txt" | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | foreach-object { $fname=[System.IO.Path]::GetFileNameWithoutExtension($_); $fname_short=$fname.substring(8, $fname.Length - 14); get-content $_ | foreach-object { echo $fname_short'': ''$_`n >> "' | cFullname | '"}}';

AsciiOutput( GetProcessErrorFileDirectory | 'temp.ps1', vPS );

# if needed, bring the next line of code to the Advanced > Epilog tab
ExecuteCommand( 'powershell.exe -file "' | GetProcessErrorFileDirectory | 'temp.ps1"', 0 );

AsciiDelete( cFullname );
Last edited by Wim Gielis on Tue Jul 16, 2019 1:52 pm, edited 1 time in total.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Drg
Regular Participant
Posts: 159
Joined: Fri Aug 12, 2016 10:02 am
OLAP Product: tm1
Version: 10.2.0 - 10.3.0
Excel Version: 2010

Re: PowerShell issue

Post by Drg »

Hi all i use next construction tu run powershell comands.

powershell -command "& { <write your script here> }

Code: Select all

PSscript='powershell -command "& {set-content -path '|Filepath|' -value '|exportstring|' }"';
EXECUTECOMMAND( PSscript , 1 );
this saves us situations when the script needs to be given permission, but it also brings a little pain with quotes and double quotes
Last edited by Drg on Wed Jul 17, 2019 7:22 am, edited 1 time in total.
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: PowerShell issue

Post by Wim Gielis »

Drg wrote: Tue Jul 16, 2019 6:50 ambut it also brings a little pain with quotes and double quotes
Indeed, don't you close the double quotes in the code ?
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Drg
Regular Participant
Posts: 159
Joined: Fri Aug 12, 2016 10:02 am
OLAP Product: tm1
Version: 10.2.0 - 10.3.0
Excel Version: 2010

Re: PowerShell issue

Post by Drg »

Wim Gielis wrote: Tue Jul 16, 2019 1:53 pm
Drg wrote: Tue Jul 16, 2019 6:50 ambut it also brings a little pain with quotes and double quotes
Indeed, don't you close the double quotes in the code ?
Oh Yes, I hurried with the answer so I missed them [corrected] But I thought the essence of the question is that you don’t know how to execute the scripts.
However, I was talking about the situation with quotes when you need to use double and single quotes inside a block of something like this (yes, these are simply dumb examples of wrapping different quotes that are revealed at the Turbointegrator level then at the cmd level and after at the powershell level)

Code: Select all

vFlagSet='powershell -command "& {';
vFlagSet=vFlagSet | '$SomePSvariable=get-content -path ''c:\temp\bla.txt'' ';
vFlagSet=vFlagSet | 'sc -path '''|vAvailableProdInstanceFlagPath|''' -Value \"$SomePSvariable\" -Force -ErrorAction SilentlyContinue;'; 
vFlagSet=vFlagSet |'(Invoke-RestMethod -Uri \"http://'| AdminHost |':5895/api/v1/Servers(''' | Server | ''')\").AcceptingClients;'
vFlagSet=vFlagSet |'$HttpPort=8001;Invoke-RestMethod -Uri \"http://'| AdminHost |':$HttpPort/api/v1/Cubes?$filter=Name eq'''| CubeName |'''&$count=true'\"; }"';
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: PowerShell issue

Post by Wim Gielis »

By now it works, but I will play around with your examples.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Post Reply