I've been strugling on a little thing for few hours now, and I wanted to know if one of you have the solution (maybe i'm missing a little thing)
I got a switch for setting the condition in a IF but seems it doesn't interprete it as a if condiction
switch ( $CampaignStatus )
{
"Complete" { $CampainCondition = ($DateStr -eq (get-date -Format "MM/dd/yyyy")) }
"Incomplete" { $CampainCondition = ($DateStr -eq (get-date -Format "MM/dd/yyyy")) }
"InProgress" { $CampainCondition = ($DateStr -gt (get-date -Format "MM/dd/yyyy")) }
}
foreach($c in $CampaignList) {
$index = $index +1
$DateStr = ConvertToFriendlyDate -Date $c.deadline
if ($CampainCondition) { blablabla}
Any idea ?
I tried with quotes, without etc
You're looking for Script Blocks to store those expressions and later evaluate them:
$CampainCondition = switch ( $CampaignStatus ) {
"Complete" { { $DateStr -eq (Get-Date -Format "MM/dd/yyyy") } }
"Incomplete" { { $DateStr -eq (Get-Date -Format "MM/dd/yyyy") } }
"InProgress" { { $DateStr -gt (Get-Date -Format "MM/dd/yyyy") } }
}
Then for evaluation, you can use the call operator &:
foreach($c in $CampaignList) {
$index = $index++
$DateStr = ConvertToFriendlyDate -Date $c.deadline
if(& $CampainCondition) {
<# blablabla #>
}
<# blablabla #>
}
As for the conditions themselves, instead of converting the date into a specific format (Get-Date -Format "MM/dd/yyyy") and then comparing that string with $DateStr, it would a much better idea to parse $DateStr as a DateTime instance and compare that against today's date.
Related
I'm trying to write a function to calculate either a full ISO date (yyyy-W-d, where W is the ISO week number), or just the ISO week number by itself. My function is based on this.
I have 2 problems with my code. One is that I do not know how extract either of the results I am after as written above. The other problem is that the second parameter doesn't show when I try to call the function.
This is my current code.
Function Get-ISODate {
Param(
[datetime]$Date = (Get-Date),
[bool]$LongFormat
)
$DayOfWeek = $Date.DayOfWeek
If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}
$Year = $Date.Year
$Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
$Day = $Date.Day
If ($LongFormat)
{
Get-ISODate = $Year + '-W' + $Week + '-' + $Day
}
Else
{
Get-ISODate = 'W' + $Week
}
}
My expected results would be,
Get-ISOWeek returns W16, which is the current ISO week at this time of writing.
Get-ISOWeek -LongFormat $true returns 2021-W16-1
Get-ISOWeek -Date '2000-1-1' returns W52
Get-ISOWeek -Date '2000-1-1' -LongFormat $true returns 1999-W52-6
I'm using this link to verifiy my results with regards to the week calculation.
You can read about switch paramenter here.
What Mathias meant is to literally remove Get-ISODate =
The function should look like this:
Function Get-ISODate {
Param(
[datetime]$Date = (Get-Date),
[switch]$LongFormat
)
$DayOfWeek = $Date.DayOfWeek
If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}
$Year = $Date.Year
$Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
$Day = $Date.Day
If ($LongFormat.IsPresent)
{
return "$Year-W$Week-$Day"
}
'W' + $Week
}
# Testing the function
PS /~> Get-IsoDate
W16
PS /~> Get-IsoDate -LongFormat
2021-W16-19
Got this working. Please note that the main issue was that I was trying to call the function incorrectly with "Get-ISOWeek". Monday morning.
I also added .value__ to Day for $Day to get the number instead of the day as "Monday" etc.
Function Get-ISODate {
Param(
[datetime]$Date = (Get-Date),
[bool]$LongFormat
)
$DayOfWeek = $Date.DayOfWeek
If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}
$Year = $Date.Year
$Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
$Day = $Date.DayOfWeek.value__
If ($LongFormat -eq $true)
{
"$Year-W$Week-$Day"
}
Else
{
"W$Week"
}
}
Edit: I realised after posting this, the year was sometimes wrong according to the week number and day of the week. So here is my new version. I have checked the dates here.
Function Get-ISODate {
Param(
[datetime]$Date = (Get-Date),
[switch]$LongFormat
)
[int]$Year = $Date.Year
[int]$Month = $Date.Month
[int]$Day = $Date.DayOfWeek.value__
If ($Day -eq 0) {$Day = 7}
If ($Day -ge 1 -and $Day -le 3) {$Date = $Date.AddDays(3)}
$Week = (Get-Culture).Calendar.GetWeekOfYear($Date, 'FirstFourDayWeek', 'Monday')
Switch ($Week)
{
52 {If ($Day -ge 5 -and $Day -le 7 -and $Month -ne 12) {$Year = $Year - 1} ; Break}
53 {If ($Day -ge 5 -and $Day -le 7 -and $Month -ne 12) {$Year = $Year - 1} ; Break}
1 {If ($Month -eq 12) {$Year = $Year + 1} ; Break}
}
$Week = $Week.ToString('00')
If ($LongFormat.IsPresent)
{
"$Year-W$Week-$Day"
}
Else
{
"W$Week"
}
}
Apparently my code is like this and it is now working. I think the logic is already there. the $openTime and $closeTime is read from csv using import-csv in "HH:mm" form.
$openTime = $ip.openTime
$closeTime = $ip.closeTime
$time = Get-Date -UFormat "%R"
if (($time -ge $openTime) -and ($time -le $closeTime)) {
Write-Host "Store is Open!" -ForegroundColor Green
}else{
Write-Host "Store is outside open hours!" -ForegroundColor Red
}
powershell 7
$csv = #"
store, openTime, closeTime
Wallmart, 08:00, 18:00
Ikea, 10:00, 20:30
"# | ConvertFrom-Csv
Get-Date
$csv | ForEach-Object{
[int]([datetime]::Now - [datetime]::Today).TotalMinutes -in (.{[int]$args[0][0]*60 + [int]$args[0][1]} $_.openTime.split(":"))..(.{[int]$args[0][0]*60 + [int]$args[0][1]} $_.closeTime.split(":")) ? "Store {0} is Open! " -f $_.store : "Store {0} is outside open hours!" -f $_.store
}
16 февраля 2021 г. 8:52:42
Store Wallmart is Open!
Store Ikea is outside open hours!
powershell 5
$csv = #"
store, openTime, closeTime
Wallmart, 08:00, 18:00
Ikea, 10:00, 20:30
"# | ConvertFrom-Csv
Get-Date
$csv | ForEach-Object{
("Store {0} is outside open hours!", "Store {0} is Open! ")[[int]([datetime]::Now - [datetime]::Today).TotalMinutes -in (.{[int]$args[0][0]*60 + [int]$args[0][1]} $_.openTime.split(":"))..(.{[int]$args[0][0]*60 + [int]$args[0][1]} $_.closeTime.split(":"))] -f $_.store
}
Try it online!
I find it's much easier to work with date and times if I convert them to [DateTime] objects. We can use the DateTime class method ParseExact to convert the time into [DateTime] objects for us. This object will actually contain today's date as well as the time we supply, but for our purposes this is fine since the $time object also will be today's date. For the current time ($time) just let Get-Date return to us a [DateTime] object that will represent the current date and time (now). After that the rest of your code works as expected. Hurray!
# this $ip hashtable object just represents data similar to your csv import
$ip = #{
openTime = "09:00"
closeTime = "21:00"
}
$openTime = [datetime]::ParseExact($ip.openTime, 'HH:mm', $null)
$closeTime = [datetime]::ParseExact($ip.closeTime, 'HH:mm', $null)
$time = Get-Date
if (($time -ge $openTime) -and ($time -le $closeTime)) {
Write-Host "Store is Open!" -ForegroundColor Green
}
else {
Write-Host "Store is outside open hours!" -ForegroundColor Red
}
If you are working with the time are imported from csv, make sure the time format in the csv file is in "HH:mm"
I am trying to use PowerShell to get the dates from a Sharepoint list and compare today's date to see if there is a match.
I have some code so far but it doesn't seem to be working. I am guessing I have some data types wrong when I am doing a comparison or something of that nature. This is the code I have so far below:
$unusableDatesArray is set with a CAML query to a list in SharePoint
The column name of the date field in SharePoint is called 'UnusableDate' and of type date/time
$todaysDate = Get-Date
$foundUnusableDate = $false
ForEach($item in $unusableDatesArray) {
if($item['UnusableDate'] -eq $todaysDate) {
$foundUnusableDate = $true
}
}
Even though both values appear to have 8/10/2017 12:00:00 AM as the value when I find a matching date, $foundUnusableDate never ends up being true. If anyone knows what I am doing wrong please chime in.
-eq is a "hard" comparison operator to get a $true out of it, both variables would need to be at the exact same datetime. This would return $true:
$date1 = [datetime]'8/11/2017 12:00:00'
$date2 = [datetime]'8/11/2017 12:00:00'
if ($date1 -eq $date2) {$true
} else {$false}
while a simple difference of one second would cause it to return $false
$date1 = [datetime]'8/11/2017 12:00:00'
$date2 = [datetime]'8/11/2017 12:00:01'
if ($date1 -eq $date2) {$true} else {$false}
To work around that you can use different approaches
$date1 = [datetime]'8/11/2017 12:00:00'
$date2 = Get-Date
if (('{0:yyyyMMdd}' -f $date1) -eq ('{0:yyyyMMdd}' -f $date2)) {$true} else {$false}
if ($date1.Date -eq $date2.Date) {$true
} else {$false}
$date1 = ([datetime]'8/11/2017 12:00:00').Date
$date2 = (Get-Date).Date
if ($date1 -eq $date2) {$true} else {$false}
$dateToCheck = Get-Date
ForEach($item in $unusableDatesArray) {
[DateTime]$sharePointDate = $item['UnusableDate']
if(Get-Date $sharePointDate -Format 'MM/dd/yyyy') -eq (Get-Date $dateToCheck -Format 'MM/dd/yyyy')) {
$foundUnusableDate = true
}
}
The above is my final solution
I want to convert datetime to Epoch format in csv file using PowerShell. In the csv file I have only time data, and I want to use current date and time specified in csv to convert it to Epoch format .
in.csv
"192.168.1.2","01-any1TEST ","Ping","Down at least 3 min","17:25:14",":Windows 2012 Server"
"192.168.1.2","02-any2TEST ","Ping","Down at least 4 min","17:25:40",":Unix Server"
"192.168.1.2","03-any3TEST ","Ping","Down at least 3 min","17:26:21",":windows host "
My findings
This should be doable using a combination of the below two. The main issue I am facing is that I am unable to combine the current date with the time in csv file.
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = [datetime]::Parse($_.Date).ToString('MM/dd/yyyy HH:mm')
}
Get-Date -Date "12/31/2015 23:59:59" -UFormat %s
When using Get-Date, you have the option to override values manually or using another datetime
For example:
Import-Csv ".\out.csv" |
ForEach-Object {
$tempDate = [datetime]::Parse($_.Date).ToString('MM/dd/yyyy HH:mm')
Get-Date -Hour $tempDate.Hour -Minute $tempDate.Minute -UFormat %s
}
I'd do it like this:
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = Get-Date -Date $_.Date -UFormat %s
}
If you want to be a bit more explicit about what it's doing, you can convert the time to a timespan, which can be added to a date. Then you can pipe it to Get-Date to format it:
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = [DateTime]::Today + [Timespan]::Parse($_.Date) | Get-Date -UFormat %s
}
[DateTime]::Today is today's date at midnight (time 00:00:00).
Ok, try the code below. It will write a warning message to the console when it finds a date that it can't parse. It won't fix the problem, but it will tell you where the problem is.
Import-Csv ".\out.csv" |
ForEach-Object {
$t = [timespan]::Zero
if ([Timespan]::TryParse($_.Date,[ref]$t)) {
$_.Date = [DateTime]::Today + $t | Get-Date -UFormat %s
}
else {
Write-Warning "Unable to parse timespan '$($_.Date)' for record $($_)"
}
}
This is working perfectly for me in 2.0 and not on 4.0 version . If possible please let me know why is is not working on powershell 4.0 .
$EventTime = $($s.EventTime)
$value = get-date -format d
$Imported = Import-Csv 'C:\PathToFIle\out.csv'
$Output = foreach ($i in $Imported) {
foreach ($c in $EventTime) {
$time=$i.eventtime
$fulltime =$value+' '+$time
$i.EventTime = Get-Date -Date $fulltime -UFormat %s
}
$i
}
$Output
$Output | Export-Csv 'C:\PathToFIle\TestComputers.csv' -NoTypeInformation
I have a text file as shown below. I need to preserve the files which are within 6 months old (from the previous month) and write every other content to other file which are more than 6 months old.
What I've done so far is like:
$array3 =
do {
foreach($monthl in $monthlookup)
{
$mnthl = $monthl -split '-'
$m_day = $mnthl[1]
$m_month = $mnthl[2]
$m_year = $mnthl[3]
$m_temp = 0
$prevmonth = (Get-Date).AddMonths(-1).ToString("dd-MM-yy")
while(($m_temp -eq "0"))
{
if(($m_month -ge $startmonth) -and ($m_month -le $prevmonth))
{
$monthl | Add-Content "C:\each_month_latest.txt"
break;
}else
{
$monthl | Add-Content "C:\exclusions.txt"
}
}
}
} until ($m_month -ge $m)
What the issue I identified here is like: If the current month is 1, then it wont go back to the last 6 months and check, as I use just strings here.
Any suggestions or code improvements, anyone of you can think of..? Would be really appreciated.
Edit
monthlookup will look like:
testdatabase-30-11-14-23-00
testdatabase-31-12-14-23-00
testdatabase-30-01-15-23-00
testdatabase-31-01-15-23-00
testdatabase-27-05-15-23-00
testdatabase-28-02-15-23-00
testdatabase-31-03-15-23-00
testdatabase-30-04-15-23-00
testdatabase-31-05-15-23-00
$m is $m = Get-Date -Format "MM"
Well, I don't completly understand what you want to do but here some tipps:
$prevmonth = (Get-Date).AddMonths(-1).ToString("dd-MM-yy") shouldn't be done each time within the foreach loop
You could extract the date using a simple regex: [regex]::Match('testdatabase-30-11-14-23-00', '^.*?-(\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$)').Groups[1].Value
Parse the date using [datetime]::ParseExact and a format string
So it could look like this:
# this doesn't have to be done each time in the foreach loop
$sixMonthBefore = (Get-Date).AddMonths(-6)
foreach($monthl in $monthlookup)
{
# extract the date
$dateString = [regex]::Match($monthl, '^.*?-(\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$)').Groups[1].Value
# create a datetime using format string
$date = [datetime]::ParseExact($dateString, 'dd-MM-yy-HH-mm', $null)
if ($date.Month -eq (Get-Date).Month -and $date.Year -eq (Get-Date).Year)
{
Write-Host "$monthl is from this month"
}
elseif ($date -gt $sixMonthBefore)
{
$monthl | Add-Content "C:\each_month_latest.txt"
}
else
{
$monthl | Add-Content "C:\exclusions.txt"
}
}