March 16, 2014 at 8:37 pm #13852
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:
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.
March 17, 2014 at 5:10 am #13854
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.
March 17, 2014 at 5:14 am #13855
In your case it would probably be like this. . .
The open that folder in explorer. . .
In that empty folder copy your get-freespace.ps1 file and change the extension to psm1
March 17, 2014 at 6:11 am #13856
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 firstname.lastname@example.org -From email@example.com -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?
March 18, 2014 at 4:11 am #13875
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. . .
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.
You must be logged in to reply to this topic.