Powershell convert number to date and save it - powershell

I am curious if it's possible to convert numbers to data. In below example the script would ask sth like this :
How long user will be active: <add number 30 - meaning for 30 days>
So there must be a variable that holds a current date and add days based on numbers from the input nd, for example, save these data to file. I would create a second script that reads this file and remove users if current days are equal do the current date.
I am not sure about conversion from adding days like this :
current-date + 30 days = date_in_thefuture :)
any example or where i should look ?

$numberofdays = 30
$Temp = (Get-date).AddDays($numberofdays)
Powershell has functions like Get-Date that gives methods such as AddDays() that allows you to do what you are looking for.
Microsoft documentation on Get-Date

Related

Dynamic variable based on previous month in powershell

I want to change dynamically for below URL.
e.g let's say if I run the script in february, the url would be like below such as m-1-2023.
or if I run the script in march, the url would be like m-2-2023.
if I run the script in january 2023, the url would be like m-12-2022.
and so on.
As summary , it will be the previous month.
My URL:
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023
Thanks,
Use Get-Date to get a DateTime object, which has support for date arithmetics and formatting. Get last month by adding -1 months and format with .Net format strings Like so,
$d = (get-date).AddMonths(-1).ToString("M-yyyy")
$url = "https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-" + $d
# output
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023

Sending automated email from SAS with attachments that changes name

I have files that should be send out every week. These files changes names e.g. "filename_1" next week it will be "filename_2".
But it only takes the filename that I manually wrote which is filename_1. Is there a way to say that it should take the latest file with this name everyweek,instead of me doing it manually every week?
This is my code for the email (I manually wrote the filename):
filename outbox email from="test#test.dk" to="test#test.dk"
type='text/html' subject='test' attach=("F:\filename_1.png" ct='png')
ods html body=outbox rs=none style=Htmlblue;run; ods html close;
The SAS macro facility will help you solve this very problem. If your filenames always have a consistent pattern, you can assign a macro variable to automatically change it for you. For simplicity's sake, let's say your filename always ends with today's date. You can assign a macro variable to hold this value.
%let filename = filename_&sysdate9..png;
This will resolve to filename_14DEC2020.png. You can confirm it with %put &filename.
If your file is sent out weekly and increments in a pattern, some quick math will help us figure out the correct suffix. Let's set a base week to start. We can count the number of weeks from this base week to identify the suffix. In this case, let's say it's today: December 14th, 2020. intck() can count the number of weeks from then until today. Our logic is:
suffix = (Number of weeks from Dec. 14th 2020 to Today) + 1.
In data step language, this is:
suffix = intck('week', '14DEC2020'd, today() ) + 1;
Translated to SAS macro language:
%let suffix = %sysevalf(%sysfunc(intck(week, %sysfunc(inputn(14DEC2020, date9.)), %sysfunc(today()) )) + 1);
%let filename = filename_&suffix..png;
Because we're pulling from data step functions, we need to enclose nearly everything in %sysfunc() to call them. This is one of the functions available that connect the SAS macro facility with the data step language.
Note that we also cannot use date literals directly in the SAS macro facility. We must use inputn() or putn() to convert a human-readable date into a SAS date format.
Simply call this macro variable within your code and it will resolve automatically (except within single quotes).
filename outbox email
from="test#test.dk" to="test#test.dk"
type='text/html'
subject='test'
attach=("F:\&filename" ct='png')
;

Comparing creation dates of files in VBScript

This may be very obvious to someone out there but I'm having a lot of trouble trying to solve a bug in VBScript. Within the script, I am running through a bunch of .zip files in a directory and processing ones whose creation date is within a specified range.
For instance, if the user enters two arguments 9/3/2014 and 9/5/2014, I only want to process zip files within that date range.
Here is the if statement I am using:
If Mid(file.NAME,len(file.NAME)-3,4) = ".zip" AND
FormatDateTime(file.DateCreated, 2) >= Wscript.Arguments(1) AND
FormatDateTime(file.DateCreated, 2) <= Wscript.Arguments(2) then
I am using the FormatDateTime function to remove the times from the file creation date. That way I should just be left with a short date (mm/dd/yyyy).
The problem I am having is that I am processing dates outside of the given range. For example if the given range is 9/3/2014 to 9/5/2014 then I also end up processing 9/30/2014 for some reason. Can anyone help solve this?
Both the return value of FormatDateTime() and the items of .Argments are Strings. A string comparison of (stringyfied) numbers will give inconvenient results:
>> WScript.Echo CStr(5 < 30)
>> WScript.Echo CStr("5" < "30")
>>
True
False
Use CDate() to convert the .Arguments to Dates and DateDiff() to compare them against the .DateCreated.
Found the source of my problem. FormatDateTime returns a string. Furthermore, the arguments I was being passed were strings also. This means I was actually doing a string comparison instead of a date comparison. The if statement should be:
If Mid(file.NAME,len(file.NAME)-3,4) = ".zip" AND
CDate(FormatDateTime(file.DateCreated, 2)) >= CDate(Wscript.Arguments(1)) AND
CDate(FormatDateTime(file.DateCreated, 2)) <= CDate(Wscript.Arguments(2)) then

Comparing Current Date and Time Against a File

I am looking to compare the last updated date and time of a file against the current date.
To date the date/time of the file I am splitting up the time and date tag like this:
find . -maxdepth 1 -type f -name \*.dat -printf "%Tm,%Td,%TH,%TM\n"
And I am looking to match this against the current date:
date '+%m,%d,%H,%M'
Both are in the same format - DD MM HH MM - and I would like to compare if the time is more than two minutes out.
What would be my best option for coding this - can I use the values I have obtained?
Or even - would there be an easier method?
Yes, there is an easier way.
You can use
find -mmin -2
for finding all the entries which have been changed less than two minutes ago.
Or, of course, use
find -mmin +2
to find the entries which are older than two minutes (have been changed more than two minutes ago).

Powershell HowTo get last day of 2 month ago

I've a script scheduled every 4th and 14th day of month.
when script starts on 4th, I need to get the last day of the previous month (easy, $a.AddDays(-5))
when script starts on 14th, I need to get the last day of 2 month before.
for example:
14 april.
I want to get:28 february 2013
how is it possible?
also I want to get the date in format yyyymmdd
UDPDATE:
Is it possible that your solution doesn't work well with the change of the year?
if I'm in january, $(get-date).month -1 results 0. so I did this:
$datenow = Get-date
if $datenow.day -eq 14
$datenow.AddDays(-14)
then I calculate
$LastDayInMonth = [System.DateTime]::DaysInMonth($(Get-date).Year, $(Get-date.Month))
$datenow.AddDays(-$LastDayInMonth)
$datestring = $datenow.ToString("yyyyMMdd")
To get the date in a string:
$(get-date).Date.ToString("yyyyMMdd")
For getting the last day of two months prior:
if($(get-date).Day -eq 14){
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
}
Update
The line
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
Uses the static method DaysInMonth in the System.DateTime class to get the days in the month of the month that is passed to it. It's documentation is here. It takes as input two parameters, the month as an integer, and the year as an integer as well.
In our case, we want 2 months before this month, so for the month parameter we pass in
$(get-date).month - 2
And that is surrounded in a parenthesis to make sure powershell does the calculation and then pass the result of the calculation. get-date is a powershell cmdlet that gives the current date and time as a .NET dateTime object, so we have all the properties and methods at our disposal.
The whole
[System.DateTime]::DaysInMonth()
is just the way of calling static methods in powershell.
Update 2
Once you get the last day in the month, you can concatenate the string by:
$LastDayInMonthString = "$($(get-date).AddMonths(-2).ToString("yyyyMM"))$LastDayInMonth"
$(Get-Date).addDays(-$(Get-Date).Day).addMonths(-1).ToString("yyyyMMdd")