Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
wobble_wobble

Avatar / Picture

Associate Troublemaker Apprentice
Registered:
Posts: 934
Reply with quote  #1 
I have inherited a solution that has a wonky screw to put it simply.

First off, what does this do in a scheduled task?
-noprofile -command ". """C:\ApplicationDirectory\Process-Emails.ps1"""

-noprofile -command ". """C:\ApplicationDirectory\Process-Emails.ps1"""
 

I've edited the file path, but the -noprofile and the """ has me a bit confused.

What does it mean/ do.

Next, the code is to run a script, that checks for emails in a mailbox, queries the subject and then sends that email to an email address based on the subject.

The mail queue is random, sometimes tiny (zero mails) sometimes a lot larger (600 mails) within the time period.
When the script has issues it hangs and processes/ sends no mail.

The script for the email processing is scheduled to run every 15 minutes from a scheduled task. When this finishes it generates an event 102.
I've changed this to three tasks, 20 min apart at 1 hour intervals.

I want to make the script run after it finishes, so I want to do the following:
1. Schedule the script to run at 00.01.
2. Watch for Event 102 in the scheduled tasks logs.
3. Event 102 in the scheduled tasks logs kicks off the script to process the email
4. At 23.30 I kill the task (no idea how, i could search for the schtask.exe PID and kill it, but that could have other side effects)
5. At 00.01 number 1 starts.

[biggrin]

 


__________________
Have you tried turning it off and walking away? The next person can fix it!

New to the forum? Read this
0
jsclmedave

Administrator
Registered:
Posts: 495
Reply with quote  #2 
-NoProfile iss for running scripts from a scheduled task.  This prevents any profile settings from being loaded that could have unexpected results.

Here is an example

http://www.powertheshell.com/bp_noprofile/

the -Command is just declaring the command which is dot sourced  ". ""C:\

Now the multiple " . "" ................... """   To me looks like they are having to escape the \ in the script path..?   I have seen that with some of the Linux based apps we use.

I posted on Twitter a good example of how to enter it into a scheduled task, I will find it and post here.



__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
wobble_wobble

Avatar / Picture

Associate Troublemaker Apprentice
Registered:
Posts: 934
Reply with quote  #3 
Grand I understand the no profile.

Explain further the 

Quote:
 the -Command is just declaring the command which is dot sourced  ". ""C:\


I'd normally not use the -noprofile, but would use this
-command E:\Scripts\WorkingScripts\Email-V1.ps1 



__________________
Have you tried turning it off and walking away? The next person can fix it!

New to the forum? Read this
0
Infradeploy

Avatar / Picture

Senior Member
Registered:
Posts: 171
Reply with quote  #4 
The noprofile is to skip profile scripts: $profile|select * to see them
I usually use -file.

With dot source you would state in powershell that what you are doing is a command in stead of just declaring something. I normally use the & for that.

__________________
Have SpaceSuit, Will Travel

0
jsclmedave

Administrator
Registered:
Posts: 495
Reply with quote  #5 
For running a Function via a Scheduled Task I used this - 


PowerShell.exe -NoProfile -Command ". D:\Temp\Scripts\FunctionName.ps1;FunctionName;"


Notice that I have to name the Function Name twice..?  That because it would load the Function into the session, but not actually run it.

FunctionName.ps1;FunctionName;"


Another option could be - Not sure if this is best practice but am sure someone here will be able to guide us - is to add the function name inside of the saved Function Script which is outside of the Closing bracket of the Function itself - 


Function RVTOOLS{
Param (
        Stuff,
        Stuff
       )
. "C:\Program Files (x86)\RobWare\RVTools\RVTools.exe" -u $UserName -p $Password -s "$Server" -c ExportAll2xls -d "$ExportPath" -f "$Server.xls" 
More Stuff
More Stuff

Exit
} # End Function RVTOOLS

RVTOOLS -UserName $($Creds01.UserName) -Password $($CredS01.GetNetworkCredential().Password)





__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.