Solved

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

Posted on 2015-01-16
12
86 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

840 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