Solved

how to configure a schedule task to launch a function which is in a module ?

Posted on 2015-01-16
12
90 Views
Last Modified: 2015-01-21
Hello,

i 'd like to launch with scheduletask a function that is in one of my modules and so far i only found examples for launching a Module.

for the module i'm supposed to add an action :
Program/script:
Powershell -command modulename

how can i point to a specific function inside the module ?


thank you in advance for your help.

Toshi
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40553863
That is not correct. You import your module, either by Import-Module (if properly built in a DLL or the like), or by importing the PS1 script defining it.
I assume you are talking about a PS1 script. Then you can use this as action:
Powershell -command  '. your-script.ps1; your-function'

Open in new window

0
 

Author Comment

by:toshi_
ID: 40557582
Hello Qlemo,

thanks a lot for your reply.  

my file that contains the function is a module, the extension is psm1.  Does it work also with modules files ?
on history tab in scheduler panel, i can find the executions of the ScheduleTask that i cron on friday. every hour the ST has launch something......i don't exactly because it did not produce files as my function is supposed to do.

As i dont have any output, how can I check that the function run ?

thank you in advance for further help.

Toshi
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40558307
If the module file is installed in the default module folder $env:PSModulePath, a simple Import-Module YourModuleName is sufficient prior to execution your function. With PS3 and above you do not need even that.

If your module file is in a non-standard location, you'll definitely have to import it with Import-Module C:\Path\To\YourModule\YourModule.psm1.

The complete command is e.g.
Powershell -command  'Import-Module YourModule; your-function'

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:toshi_
ID: 40559294
Hello Qlemo,

Thank you for your reply.
My module is in the default folder $env:PSModulePath.
I have updated my schedule task as suggested, with the Import-module

I.
Now when i launch my function from Powershell ISE the output filie is created every thing runs as expected.
II.
But if launch my schedule task with : Task Scheduler panel , "run".  I can find logs on eventviewer under :
Application and Services Logs/Microsoft/Windows/Powershell/Operation.
but logs only has information and no error.

For info: the user that launches the schedule task is the same that i use to launch powershell ISE:

What should i look after, how can i find more information about this schedule task ; Why doesn't it write the file ?

other info : on my schedule task , i have enter the Edit Action as follow :

Program/script : Powershell
Add argument : -command  'Import-Module YourModule; your-function'



thank you in advance for further help.

toshi
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40559828
Try with
PowerShell -command 'Start-Transcript C:\Temp\logfile.txt; Import-Module YourModule; your-function'

Open in new window

That will generate a script log file.
Another way (additional or instead) is to add -NoExit to the PowerShell call (best as first parameter). That will allow the console window to stay open after execution.
You might also try to tick "Run with highest privileges" in the task. That often helps.
0
 

Author Comment

by:toshi_
ID: 40559835
i try now ;)
0
 

Author Comment

by:toshi_
ID: 40559862
Qlemo,

ai !

no logfile.txt in my C:\temp

i also try to do that on powershellISE, here's what it answers.

PS M:\> Start-Transcript C:\temp\logfile.txt ; Get_Tshr_WhatTestingMachineHasTester
Start-Transcript : This host does not support transcription.
At line:1 char:1
+ Start-Transcript C:\temp\logfile.txt ; Get_Tshr_WhatTestingMachineHasTester
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscriptCommand


any idea ?
0
 

Author Comment

by:toshi_
ID: 40559869
transcript works on my console powershell but not on ISE.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40559893
That is to be expected. ISE implements its own input/output, and hence does not allow for some operations like transcripting.

With the task command line, I'm making the same mistake all the time :/
The correct usage is
PowerShell -command "& {Import-Module YourModule; your-function}"

Open in new window

You can add the transcripting etc. as you like.
0
 

Author Comment

by:toshi_
ID: 40560006
amazing...
i've tried all possible combinations but without succès.

do you think i should go for a ScheduleTask on a batch that will launch the powershell ?

what would be other possibilities.

sorry for the time this question is taking.

toshi
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40560434
If you use above cmdline exactly as provided e.g. from the Command Prompt, does it perform any action?
0
 

Author Comment

by:toshi_
ID: 40562131
Hello Qlemo,

launching the powershell on the "cmd" was the solution :

the function i was launching has as a parmeter a function that stand in a different module.
my principal module would not load the dependent module.

Once i have added the import in the command,  things now run on cmd.
and it's work also on the schedule task ! !!!!!!

the file is created and has a basic header on it.....

thank you so much for helping me.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question