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"
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
We have an automatic scheduled export of Salesforce data into CSV files in specific folder(everyday at 10pm).
In each CSV file there are two datetime columns which are in UTC time format [2018-01-30T05:27:26.000Z].
My requirement is to create a script to read those CSV files and update date time columns into local time zone and format (dd/mm/yyyy hh:mm:ss).
And I need to schedule this script to run everyday (everyday at 10.30pm).
(we know the folder path, file name, column name).
Please help with the script sample.
Thank you
Ditto to what Robert Cotterman has stated.
However, with This well could be considered a duplicate of this discussion.
Convert list of UTC to Current TimeZone
$UTCTime = GC "C:\Scripts\UTC.txt"
$results = '' | Select UTCTime,PST
Foreach ($newtime in $UTCTime){
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($newtime, $TZ)
$results.UTCTime = $newtime
$results.PST = $LocalTime
$results
}
This answer will get you started, and that last part is just use the script with a scheduled task.
So I am trying to get the date 3 months back using the Powershell command Get-Date. However, when I try to add multiple get-dates in one line it errors out or doesn't give me the results I'm looking for.
The End result I'm trying to get is $checkDate = 6-7-2016
I've tried this, but it doesn't really work:
$checkDate = (Get-Date).month -3 "-" (Get-Date).day "-" (Get-Date).year
Any ideas on how to accomplish this? I'm newer to Powershell and not exactly sure how to concatenate properly.
I'm using PS 4.0
What you are trying to do is format a calculated date. So let's start with calculating the date:
(Get-Date).AddMonths(-3)
That gets you today's date minus 3 months. Next you want to format it in a specific manner. That being Month-Day-Year. That can be done as such:
(Get-Date).AddMonths(-3).ToString("M-d-yyyy")
That results in:
6-7-2016
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 create this script
$VarDay = (Get-Date).day
$VarMonth = (Get-Date).month
get-messagetrackinglog -Recipients:haavarot-from#my-domain.co.il -EventID "FAIL" -Start "09/20/14" -End "09/23/14" | export-csv c:\MailboxStatistics-$VarMonth-$VarDay.csv -encoding "utf8"
to create CSV file with the date name for FAIL mails from mail box
its work fine
but the only problem i cant found is to way to make it run daily wit no need to edit the DATES in the Ps code
-i want it to sat auto run at 22:00 every day and make the log for the some day only for 7 days
in the 8 day i want it to delete the old and create a new one
i need to save only the last 7 days
and idea?
-Start and -End accepts [System.DateTime] so you can just use Get-Date and play with the days using AddDays()
Straight from MSDN. You could do something like this
$endDate = Get-Date # This is today
$startDate = (Get-Date).AddDays(-7) # This is 7 days ago
If you would feel more comfortable with just the date and drop the time you can use the .ToString() method to format the time. Note that the datetime object would be lost as this returns a string.
$endDate = (Get-Date).ToString("MM/dd/yy")
$startDate = ((Get-Date).AddDays(-7)).ToString("MM/dd/yy")
More information on formatting dates can be found here