Link to home
Start Free TrialLog in
Avatar of pramod1
pramod1Flag for United States of America

asked on

.ps1 scripting, powershell

i have following commands which i run manually on a server by opening command prompt.

tabadmin stop

tabadmin clean up

tabadmin start

Is there any way i can automate this task by task scheduler .

can i save this in txt format file and save in .ps1 script but i am not sure
how to do it.
Avatar of oBdA
oBdA

Just save it as a regular batch file (.cmd) mostly like it is, then schedule this batch just like an executable.
You might want to define the absolute path to tabadmin (.exe?).
set TabPath=C:\Foo\Bar

"%TabPath%\tabadmin" stop
 
"%TabPath%\tabadmin" cleanup
 
"%TabPath%\tabadmin" start

Open in new window

While I have never run Tableau, I don't see why the commands couldn't be run via the task scheduler.
Yes, you can save PowerShell commands in a .txt file and then change the extension to .ps1.
There should be an indicator that oBdA is in the process of answering the question ... this way I would know not to bother. ;)
Come to think of it, since it's so short (and PowerShell is the main topic, after all), you theoretically don't even need an extra script, it can be golfed into the arguments.
Program to start:
powershell.exe

Open in new window

Arguments (change only C:\Foo\bar to the actual path; leave everything else, especially the quotes, as they are):
-Command "'stop','cleanup','start'|%{& 'C:\Foo\bar\tabadmin.exe' $_}"

Open in new window


Sam,
wouldn't want to discourage anyone from answering ;)
A regular batch has the ".bat" extension. I think you should be able to take all those 3 lines into that like the original obda comment says.

Jacob, don't discourage to answer if obda answer :) we know that we can take for a fact that he will answer correctly and accuratelya but some more information to the question won't hurt :P hahaha
Avatar of pramod1

ASKER

I was told by my admin to do on this script (attached) where should I modify changes, can you incorporate in it  and once done should I save this as .ps1

he expect to see something like… Start-Process "D:\Program Files\Tableau\Tableau Server\2018.1\bin\tabadmin.exe -ArgumentList “stop”

where in this file should I make changes
Template1.txt
That would be lines 49-59, below "#Bulk of code goes here".
The Start-Process is a really bad idea here. When executed as in the template, it will start the tabadmin process in the background, and then continue with the script - that's why the "Start-Sleep" comes after the command. Now, what happens if "stop" or "cleanup" takes longer than 60 seconds? Exactly.
There is no need for Start-Process at all, anyway. PowerShell is, as the name suggests, a shell script, meant to run command line programs without further ado.
So remove all the "Start-Process" and "Start-Sleep" in lines 49-59, and replace them with this:
$tbadmin = "D:\Program Files\Tableau\Tableau Server\2018.1\bin\tabadmin.exe"
& $tbadmin stop
& $tbadmin cleanup
& $tbadmin start

Open in new window

Note: you can put this into a separate .ps1 script (or run it interactively in the console) to test the tbadmin commands first without the rest.
Avatar of pramod1

ASKER

let me put together for your review , give me few mins
No need to hurry.
Avatar of pramod1

ASKER

does everything looks correct?

can I save this in .ps1

or can u suggest any further chnages
Template1.txt
Avatar of pramod1

ASKER

is it tabadmin or tba? is the spelling correct?
Avatar of pramod1

ASKER

What should I put in input file

I don’t see input file from where the ps1 will collect the server name
Avatar of pramod1

ASKER

I have to keep the start stop process.

I am attaching the final script also I have included in the input file

#Server to reboot
$reboot = Get-Content -Path "\\managementservername\C$\admin\scripts\input\tableau.txt"
Template1.txt
is it tabadmin or tba? is the spelling correct?
Sorry, but I can't know what programs you want/need to call.
I don’t see input file from where the ps1 will collect the server name
Which server name? According to https://help.tableau.com/v2018.1/server/en-us/tabadmin_cmd.htm, there's not much choice:
Note: You should only run tabadmin on the primary Tableau Server node, not on worker nodes.
$reboot = Get-Content -Path "\\managementservername\C$\admin\scripts\input\tableau.txt"
The $reboot variable is superfluous; it's never used in the script. This is probably just a part of the template which does not apply to your situation.

Why do you need the "Start-Sleep -Seconds 300" after each command? Are the commands actually designed so that they return immediately, while the action continues in the background?
Avatar of pramod1

ASKER

Hi oBda,

regarding 1st point ,
is it tabadmin or tba? is the spelling correct?- this is ok, it is tabadmin,

regarding 2nd point

i wont put the variable of input file , but directly run the task scheduler on the  tableau server

regarding 3rd point

Why do you need the "Start-Sleep -Seconds 300" after each command? Are the commands actually designed so that they return immediately, while the action continues in the background?

actually this is a template script given by my windows admin.

i need to customize as per my needs. right now i need to run manually tabadmin stop, it takes 2 to 3mins, then command prompt comes

then i type tabadmin cleanup - this cleans temporary files it takes around 3to 4 mins then again command prompt comes then i type tabadmin start , this takes 3 to 4 mins.

so that is the reason i gave sleep command

all i need sir, is the command i have written in between is correct or not

#Check to see if outdirectory exists, is it doesn't it will create it.
if (!(test-path $destdir))
 {
     New-Item -Path $destdir -ItemType Directory
 }

$tabdmin = "D:\Program Files\Tableau\Tableau Server\2018.1\bin\tabadmin.exe"
& $tabdmin stop
Start-Sleep -Seconds 300
& $tabdmin cleanup
Start-Sleep -Seconds 300
& $tabdmin start
Start-Sleep -Seconds 300


or kindly let me know of any changes in the script which is attached.

thanks
Template1.txt
If the command prompt only comes back once everything is done, you do not need the Start-Sleep. This would only be required with Start-Process, which by default starts the program in the background and immediately continues with the script. Why keep the service down unnecessarily for 10 minutes, and have the script wait unnecessarily for another 5 minutes when there's nothing more to do anyway?
So all you need is
$tabadmin = "D:\Program Files\Tableau\Tableau Server\2018.1\bin\tabadmin.exe"

& $tabadmin stop

& $tabadmin cleanup

& $tabadmin start

Open in new window

Avatar of pramod1

ASKER

I have to run three tasks
Stop clean up and start as I mentioned very clearly that when I run stop it takes 2 to 3 mins to stop all services and then command prompt comes then I do clean up which takes roughly another 3 mins and then again command prompts come and then I start which takes another 2 mins and then command prompt comes 3
Can you please check my script Ilooks correct



Command prompt comes 3 times
Since the command prompt does not come back while the respective task is running, you don't need the Start-Sleep.
The same way the command prompt doesn't come back, the script will not continue at this point, either.

I've had a short look at the rest of the template, and it's, well, let's say "not pretty" (variables that are used, but never set, variables that are set, but never used, ...). But fixing that template is outside the scope of this question, and it falls into responsibility of the person who told you to use it.
Avatar of pramod1

ASKER

So in my case start stop is not required

So my last question when do we need start sleep
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial