Posh Function - Schedule it as a Task?


I am trying to schedule a function that checks disk space on servers. To run the function I am likely not doing it the best way as I run:

  1. import-module .\get-freespace.ps1
  2. .\get-freespace.ps1 -computers c:\scripts\computers.txt -To jake@test.local -From schedtask@test.local -smtpserver mail.test.local

I’ve tried changing this to a .psm1 as a module, but running it manually I get an error that it’s “unknown”, and I realize I should change it to a .psm1 and then edit the “myfunctions” file in my profile, is that right?

I’ve tried running it in a batch file and also from within another posh script using the dot source method, but I can’t get this function or any function to work like that.

I’m using powershell 3.0 on Windows 2012.




Have you searched the web for your answer? You basically just have to create the folder structure $ENV:USERPROFILE\Documents\WindowsPowerShell\Modules

or look at the DIR ENV:PSModulePath to see where that is. Then the exact folder name and script name have to be the same and change the extension of the script to .PSM1.

In your case it would probably be like this. . .

md $ENV:USERPROFILE\Documents\WindowsPowerShell\Modules\Get-Freespace

The open that folder in explorer. . .

invoke-item $ENV:USERPROFILE\Documents\WindowsPowerShell\Modules\Get-Freespace

In that empty folder copy your get-freespace.ps1 file and change the extension to psm1


I think you’re probably trying a few random stuff and getting stuck :).

The Import-Module command does expect a .PSM1 file. While Vern is right in that module files can be located in specific locations for auto-loading, you can also manually specify a path when you load them. However, a path like “.\get-freespace.psm1” refers to the current directory. The “current” directory has to be set as the working directory in Task Scheduler.

It might be better to have your task load the module, using Import-Module, but providing a COMPLETE, absolute path (starting with a drive letter) to the module file.

But here’s where you’re missing something:

.\get-freespace.ps1 -computers c:\scripts\computers.txt -To jake@test.local -From schedtask@test.local -smtpserver mail.test.local

That is running a SCRIPT called Get-Freespace.ps1, and passing the parameters TO THAT SCRIPT. In other words, the expectation there is that the script just contains commands, and not a function. It seems like that might be the best way for you to do this. In other words, I’m not sure you need a function (or a module). I think you just need a script. I would expect the top of what script to contain a Param() block, but NOT the “function” keyword.

But again, when you run the script, provide an absolute path to it, not a relative path, because when the task runs, Task Scheduler won’t necessarily set the same working directory.

Does that help?

For stuff like that I just dot source my script. If it’s not a real module and I just need it to stay resident in memory.

a single dot followed by a space followed by a dot backslash and the file name. . .

. .\get-freespace.ps1

Much faster than doing all the work to turn it into a module. However I like what you said better Don. Just pass the parameters it needs from the command line.