Solved

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

Posted on 2015-01-16
12
74 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 68

Expert Comment

by:Qlemo
Comment Utility
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_
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
 

Author Comment

by:toshi_
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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_
Comment Utility
i try now ;)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:toshi_
Comment Utility
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_
Comment Utility
transcript works on my console powershell but not on ISE.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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_
Comment Utility
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 68

Expert Comment

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

Author Comment

by:toshi_
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now