Solved

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

Posted on 2015-01-16
12
83 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_
  • 7
  • 5
12 Comments
 
LVL 69

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 69

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 69

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 69

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 69

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

786 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