i have a .ps script file job which runs daily(except weekends and holidays). i have block of code which should execute once in a month only. as it is a daily job so it should execute first working day of the month only. Rest of the code executes daily but this my own section should execute once in a month.
Is there any way i can keep the if condition in powershell script, how it knows after month changes????
Need inputs.
$day = (get-date).day
$triggerday = 1 #if you want the script to execute on the 1st day of the month. "1" appears only once a month
if($triggerday -eq $day )
{
#Do something here
}
Related
I am looking for a script that can ignore the timing and utilise just the date to move files after 1 day, so yesterday, to an archive folder.
My knowledge of powershell is not great so any advice on how i can do this would be great.
Everyday i run a script that generates a .txt report which has a filename .....2022 01 02 (The filename ends with the date) so would like to add some extra lines that archives the .txts that were created yesterday to an archive folder.
The [datetime] type has a Date property that gives you the date at midnight, thereby allowing you to compare dates without taking the time component into account:
# Construct datetime value describing yesterday at midnight
$yesterday = (Get-Date).AddDays(-1).Date
# Filter files based on date of the `CreationTime` property
$filesCreatedYesterday = Get-ChildItem -Path .\path\to\folder -File |Where-Object { $_.CreationTime.Date -eq $yesterday }
$filesCreatedYesterday will now contain the files created yesterday
Once I get to a certain point within my script, I would like to only continue at the first available opportunity from a specified list of times. This list is automatically generated when given two pieces of information:
A start time, in 24h format- e.g "05:00"
A given interval, e.g 7 hours.
For example, the code would take two variables:
$StartTime = 05:00
$Interval = 6
It would then, beginning at the start time of 05:00 add 6 hours until a set of 4 times is generated (24/6 = 4)
Which would output:
05:00
11:00
17:00
23:00
The above I have a rough idea on how to do, and have already attempted the second part as follows:
$time1 = 05:00
$time2 = 11:00
$time3 = 17:00
$time4 = 23:00
write-host "Waiting for start time..."
start-sleep ((get-date "$time1" -or "$time2" -or "$time3" -or "time4") - (get-date)).TotalSeconds;
write-host "Time reached, code is continuing..."
Unfortunately, however, the above doesn't seem to work and I cannot use the -or statement using this method. For even just choosing the most viable of 2 times, let alone automatically generating 4 and then choosing the soonest available one. I feel like there must be a simple, few line answer to this but I cannot think of how to do so. Any help appreciated.
We have a batch file that runs an end-of-month process. Right now it's a manual process, but we'd like to automate it based on when EOM falls. If the last work day of the month is a Friday (or other weekday), we run the script on Friday night or Saturday. If it falls on a Saturday or Sunday, the script is run on Monday following the weekend. There may be a few exceptions, but that's the general idea.
We're having trouble figuring out how to automate this based on date. Any options will be considered. Powershell, batch, etc...
Any help would be greatly appreciated.
Edit - The dates it selects to run can be a bit random. If we could have it read in a text file with a list of dates to run that would work too.
So we could have a list of dates like:
04-30-2015,
05-31-2015,
06-29-2015,
Then a script could be run that says if today is equal to any of these dates, run the batch file.
The logic isn't completely clear to me, but as said in the comment above, you could run a PowerShell script using Windows Task Scheduler every day (or only Friday/Monday?) and have that script check if the time is right to do something.
From what I can tell it either has to run on Friday or Monday.
You can get the current date in PowerShell with the Get-Date command.
If you pass this through Get-Member you can see all the methods you have on the date object to figure out if the time is right to do something.
get-date | get-member
You'll probably need some methods or properties like this to implement the check:
$today = get-date
$today.DayOfWeek # prints e.g. "Monday"
$today.DayOfWeek -eq 1 # Returns True on Monday
$today.AddDays(1) # Next day, the number can be negative or positive
$today.Day # Returns 6 right now (april 6th)
There are plenty of resources that discuss calling a PowerShell script in Task Scheduler. If what you currently do is run a batch then configure your task to run at 5:00pm every day checking the date against all the dates in your text file.
$milestones = Get-Content c:\temp\dates.txt
$today = Get-Date -Format "MM-dd-yyyy"
If($milestones -contains $today){
# Do stuff and things.
# cmd.exe /K C:\Path\To\Batch.bat
}
If there was a line in the text file "c:\temp\dates.txt" for "04-06-2015" that would satisfy the If condition. Then you could uncomment the line with cmd and update as required.
If you have issues with these concepts it is expected that you do a little research before you ask. If you are still stuck after that please either edit your question of ask a new question.
I have a small problem with a log file. It must save in log a current date and a expire date which is 10 days. Something like this:
date: 27.08.2014 expire date: 06.09.2014.
I have tried script:
PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyy-MM-dd')
but I cant figure out, how to insert it in a log.txt file.
Have tried >>C:\log.txt
Please help!
This is one way: it just adds the part at the end of the line.
PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyy-MM-dd') >>"c:\log.txt"
I have a script that I converted to EXE so I can run it as a service. I am trying to make the script execute itself again on some condition. I have a for loop in the script and I want to break and re-execute so it would similar to this:
foreach{
if(Get-Date -format "yyyyMMdd" != $CURRENTDATE){
**re-execute script**
}
send_email($_)
}
Wrap your code into function and in if call this function.
There are a few ways to do this.
You can make a function, and call it directly:
function myFunction($myCollection)
foreach{
if(Get-Date -format "yyyyMMdd" != $CURRENTDATE){
myFunction $myCollection)
}
send_email($_)
}
More info for using functions can be found here (They are odd things compared to C syntax)
Would probably be the optimal route.
Another route would be a while loop:
while(Get-Date -format "yyyyMMdd" != $CURRENTDATE)
{
foreach{
#Iterate foreach here, then exits to loop, where it checks condition again
#If still does not match todays date, it goes through foreach loop again.
}
} #loop
send_email($_) #Script will not get here until it meets your date check condition
If you want to run this as a service you need some way to limit the frequency that the script checks the date, otherwise it'll use a lot of CPU!
There are a couple of ways you could do this. You could take other answers here and add the Start-Sleep cmdlet with a suitable amount of time in the for/while loop.
However I'm thinking you might have a reason to rerun your EXE. The other answers wouldn't rerun the EXE, they would just keep the EXE running until the date rolled around.
If you do want to rerun your EXE you can just enter the EXE's command line where you have **re-execute script**. You won't need the for loop, but you do need to invoke Start-Sleep to something reasonable.