Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-16
12
Medium Priority
?
95 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 71

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 71

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

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.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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