I'm new to using powershell, I'm trying to use the PSGSuite module to get a report for all users for the past 30 days. What I've got so far is the following
$REQUESTEDDATE = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$REPORTDATE = (Get-Date -Month ($REQUESTEDDATE-(-1)) -Hour 0 -Minute 0 -Second 0)
$MonthAgo = $REPORTDATE.AddMonths(-1)
$FIRSTDAYOFMONTH=GET-DATE $MonthAgo -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
$Times = $FIRSTDAYOFMONTH..$LASTDAYOFMONTH.day | Foreach-Object {
$currentdate = Get-Date -Day $_ -Month $LASTDAYOFMONTH.Month -Year $LASTDAYOFMONTH.Year
$GMAIL = Get-GSUsageReport -Date $currentdate -UserKey xxx -flat
}
This is now throwing a "Invalid cast from 'DateTime' to 'Int32' error. There's probably a much easier way to do this, but I'm more of the hardware/networking side thrown onto this while the dev team is working on different projects, so any help is appreciated.
First thing is that I hate all those CAPITALS in your code, so if you don't mind I have changed that.
Next, you can simply run through the dates for as long as the running date is less than the final date (day 1 of the requested date) using a while loop:
$requestedDate = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$reportDate = (Get-Date -Month $requestedDate -Day 1).Date # end date set at midnight on day 1
$runningDate = $reportDate.AddMonths(-1) # start at day 1, one month ago
$result = while ($runningDate -lt $reportDate) {
# perform your command and capture the output in variable $result
Get-GSUsageReport -Date $runningDate -UserKey xxx -flat
# increment the running date
$runningDate = $runningDate.AddDays(1)
}
# show the result
$result
Related
I'm trying to find in Powershell if today's date is greater than the first Tuesday of the quarter or if it is before the first Tuesday of the quarter.
The reason is my script should only make a change if today is past the first Tuesday of the quarter!
I know how to do it for the first Tuesday of a month, but really struggling to get it to work with quarters, whats the best way to do this?
# Get today's date (without a time-of-day component).
$today = (Get-Date).Date
$afterFirstTueInQuarter =
if ($today.Day -ge 7 -or $today.Month -notin 1, 4, 7, 10) { $true }
else { # in the first 6 days of the start of a quarter
# Find the first day of the month...
$firstDayOfMonth = Get-Date -Year $today.Year -Month $today.Month -Day 1
# ... and the month's first Tuesday.
$firstTue = $firstDayOfMonth.AddDays(([DayOfWeek] 'Tuesday' - $firstDayOfMonth.DayOfWeek + 7) % 7)
# Compare to today.
$today -gt $firstTue
}
A quick question, apparently today (January 06, 2020) week number should be 2, because there are 53 weeks in 2020.
However, the following PowerShell snippet returns 1:
(Get-Date -UFormat %V)
What is the good approach getting the week number properly?
To translate this Get the correct week number of a given date C# answer from #il_guru into PowerShell:
Function GetIso8601WeekOfYear([DateTime]$Date) {
$Day = (Get-Culture).Calendar.GetDayOfWeek($Date)
if ($Day -ge [DayOfWeek]::Monday -and $Day -le [DayOfWeek]::Wednesday) {$Date = $Date.AddDays(3)}
(Get-Culture).Calendar.GetWeekOfYear($Date, 'FirstFourDayWeek', 'Monday')
}
GetIso8601WeekOfYear (Get-Date)
2
GetIso8601WeekOfYear (Get-Date('2016-01-01'))
53
You could detect a leap year and then adjust the week number based off the result.
if(((Get-Date).year)%4 -eq 0){
$week = (Get-Date -UFormat %V) -as [int]
$week++
}else{
$week = (Get-Date -UFormat %V)
}
Write-Host $week
I am new in Powershell and I have write a small utility in PS in which I provide values in $firstDate and $lastDate but when I run this program my these two value is not printed in $value.. Can anyone please look and help me on this.
$firstDate=Get-Date -Year.2018 -Month.12 -Day.1
$lastDate=Get-Date -Year.2020 -Month.1 -Day.12
$value = $firstDate -eq $lastDate
when you use -year or -month or -day to provide the values, you have to define the value for it after a space. You are using dot notation which is used mostly to access property or method of the variable. (Note from #mklement0: PowerShell also supports : as the separator between parameter name and argument)
$firstDate=Get-Date -Year 2018 -Month 12 -Day 1
$lastDate=Get-Date -Year 2020 -Month 1 -Day 12
$value = $firstDate -eq $lastDate
# value = False.
This will give you the value of false because the dates dont match up.
UPDATE
The above code will never equal true even if you set the year, month and day to be the same. This is because time factor of the DateTime will be off. You can do the following to make sure you compare only Dates
$value = $firstDate.Date -eq $lastDate.Date
This will only compare the Date and ignore the Time.
trying to calculate\determine 6th Business from today in powershell.
is there a script\function available for same?
example :
if today's date is 4th Feb 2018, 6th business day from today is 12th feb 2018.
how do i get that date using powershell?
I think this would work for your situation.
It adds 2 days if the current iteration lands on a Saturday and 1 day if it lands on a Sunday. There may be a better way but I know this works.
Edit: I didn't think about it if you need to account for holidays let me know and I may be able to edit this script to include it.
$daystoadd = 7
$CurrentDate = Get-date
For ($i = 1; $i -le $daystoadd; $i++) {
$CurrentDate = $CurrentDate.adddays(1)
if ($CurrentDate.DayOfWeek -eq 'Saturday') {
$CurrentDate = $CurrentDate.adddays(2)
}
if ($CurrentDate.DayOfWeek -eq 'Sunday') {
$CurrentDate = $CurrentDate.adddays(1)
}
}
$CurrentDate
#Sumanth specifically asks for the sixth business day, so this should do the trick:
function SixthBusinessDay($date)
{
$daysToAdd = 8,8,8,8,8,10,9
$weekDay = $date.DayOfWeek.value__
$sixtBusinessDay = $date.AddDays($daysToAdd[$weekDay])
return $sixtBusinessDay
}
It's based on that all days require at least six days and one weekend to make it the sixth business day, but Friday requires two extra days and Saturday one extra day.
Running this:
(SixthBusinessDay (Get-date "2018-1-29")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-30")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-31")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-1")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-2")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-3")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-4")).ToString("dddd dd/MM")
gives the following result:
Tuesday 06-02
Wednesday 07-02
Thursday 08-02
Friday 09-02
Monday 12-02
Monday 12-02
Monday 12-02
In Powershell, converting a date into a day of the year value is easy:
$DayOfYear = (Get-Date).DayofYear
Write-Host $DayOfYear
140
Is there a similar way to convert the day of year back into a date (i.e. 140 = 05/20/2013)?
Try this:
([datetime]"01/01/$((Get-Date).Year)").AddDays(140-1)
20. mai 2013 00:00:00
mai = may in norwegian :-)
You could create a DateTime object representing the start of the year, then add the number of days to it, e.g.:
(New-Object System.DateTime 2013,1,1).AddDays(140-1)
This might be helpful for you:
$startdate_year = ([datetime]"01/01/$((Get-Date).Year)")
$a = (Get-Date $startdate_year).AddDays(139)
"Date: " + $a.ToShortDateString()
Now you will get the result like this:
Date: 5/20/2013
Use this method to avoid [datetime] objects and use Powershell syntax only:
$DayOfYear = (Get-Date).DayOfYear
$Today = (Get-Date -Month 1 -Day 1).AddDays($DayOfYear-1)
"$($Today.ToString("MM/dd/yyyy")) is day $DayOfYear of 365"
Output:
10/03/2017 is day 276 of 365