简体   繁体   English

Powershell - 每天分配一个变量

[英]Powershell - Assign Each Day a Variable

I have a simple script that assigns a variable to 'yesterday', uses a RestAPI to grab my locations' sunrise and sunset times based on yesterday's date, and then formats the results.我有一个简单的脚本,它为“昨天”分配一个变量,使用 RestAPI 根据昨天的日期获取我所在位置的日出和日落时间,然后格式化结果。

$yesDate = (Get-Date).AddDays(-1) | Get-Date -Format "yyyy-MM-dd"
$daylight = (Invoke-RestMethod "https://api.sunrise-sunset.org/json?lat=35.608237&lng=-78.647497&formatted=0&date=$yesDate").results
$sunrise  = ($daylight.Sunrise | Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$sunset   = ($daylight.Sunset | Get-Date -Format "yyyy-MM-dd HH:mm:ss")

What I now want to do is update the script to take each day of the month (for instance, Sept 1 through 27 (yesterday)), grab the sunrise and sunset values, and then assign them a variable (for instance $sunrise1 and $sunset1 ).我现在想要做的是更新脚本以获取每月的每一天(例如,9 月 1 日到 27 日(昨天)),获取日出和日落值,然后为它们分配一个变量(例如$sunrise1$sunset1 )。

Any idea on how I could even do this?关于我如何做到这一点的任何想法?

UPDATE:更新:

I have an input file with a line for each minute of the day, with a few sample lines below:我有一个输入文件,每天的每一分钟都有一行,下面有几个示例行:

2020-09-28 00:00:00 0.0 2020-09-28 00:00:00 0.0

2020-09-28 00:01:00 0.0 2020-09-28 00:01:00 0.0

2020-09-28 00:02:00 0.0 2020-09-28 00:02:00 0.0

2020-09-28 00:03:00 0.0 2020-09-28 00:03:00 0.0

2020-09-28 00:04:00 0.0 2020-09-28 00:04:00 0.0

2020-09-28 00:05:00 0.0 2020-09-28 00:05:00 0.0

I used my original script above to grab yesterdays' sunrise/sunset times, and then used the below script to remove all rows before sunrise and after sunset.我使用上面的原始脚本来获取昨天的日出/日落时间,然后使用下面的脚本删除日出之前和日落之后的所有行。 It then, removed the timestamp altogether, then took an average of the remaining values, and finally formatted the average.然后,完全删除时间戳,然后取剩余值的平均值,最后格式化平均值。

Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_unedited.txt | 
    Where-Object { $_ -ge $sunrise -and $_ -le $sunset } | 
        Out-File C:\CumulusMX\summaries\Txt_Files\daily_values_uv_consolidated.txt
(Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_consolidated.txt) -Replace '\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\t', '' | 
    Out-File C:\CumulusMX\summaries\Txt_Files\daily_values_uv_no_timestamp.txt
Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_no_timestamp.txt | 
    Measure-Object -Average | 
        Select-Object -ExpandProperty Average |
            Out-File C:\CumulusMX\summaries\Txt_Files\daily_values_uv_raw_avg.txt
$result = Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_raw_avg.txt
[MATH]::Round($result,1) |
    Out-File C:\CumulusMX\summaries\Txt_Files\daily_values_uv.txt

This may not be the cleanest way to accomplish the goal, but for the purposes of grabbing an environmental variable on a single day, filtering out the non-daylight hours, and getting the average, it works just fine.这可能不是实现目标的最干净的方法,但是为了在一天中获取环境变量,过滤掉非白天时间并获得平均值,它工作得很好。 However, the next goal is to perform this same function over a month (and at some point, a year), but taking into account that the sunrise and sunset times change daily...然而,下一个目标是在一个月内(有时甚至一年)执行相同的功能,但考虑到日出和日落时间每天都在变化……

You can iterate through the dates you want (for all days of a specific month you can use [DateTime]::DaysInMonth() . And then use New-Variable with the -Name paramter to create a dedicated variable for each days - if that's what's needed.您可以遍历您想要的日期(对于特定月份的所有日期,您可以使用[DateTime]::DaysInMonth() 。然后使用带有-Name参数的New-Variable为每一天创建一个专用变量 - 如果是这样的话需要什么。

for ($i = 1; $i -lt [DateTime]::DaysInMonth(2020, 9); $i++) { 
    # $sunrise = your stuff here
    New-Variable -Name sunrise$i -Value $sunrise
}

You could take a different approach and output an array of custom objects with Sunrise and Sunset properties.您可以采用不同的方法并输出一组具有SunriseSunset属性的自定义对象。

# Yesterday
$yesdate = (Get-Date).AddDays(-1)

# Loop through day 1 to yesterday
$Times = 1..$yesdate.Day | Foreach-Object {
    $currentDate = Get-Date -Day $_ -Month $yesdate.Month -Year $yesdate.Year -Format 'yyyy-MM-dd'
    $daylight = (Invoke-RestMethod "https://api.sunrise-sunset.org/json?lat=35.608237&lng=-78.647497&formatted=0&date=$currentDate").results
    [pscustomobject]@{
        Sunrise = ([datetime]$daylight.Sunrise).ToString('yyyy-MM-dd HH:mm:ss')
        Sunset = ([datetime]$daylight.Sunset).ToString('yyyy-MM-dd HH:mm:ss')
    }
}
# Outputs sample section

# $Times is an array of objects with sunrise and sunset properties
# $Times one array element for each day starting from day 1 to yesterday of the given month
$Times 
# Outputs day 1 sunrise
$Times[0].Sunrise
# Outputs day 10 sunset
$Times[9].Sunset
# Outputs day 15 sunrise and sunset as an object
$Times[14]
# Outputs day 15 sunrise and sunset as comma-separated values
"{0},{1}" -f $Times[14].Sunrise,$Times[14].Sunset

EDIT: Using your examples编辑:使用你的例子

If you want to check a specific day against your file, you can do the following:如果要根据文件检查特定日期,可以执行以下操作:

# Checking day 28

Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_unedited.txt | Where {
    [datetime]($_ -replace '\d+\.\d+') -ge [datetime]$Times[27].Sunrise -and [datetime]($_ -replace '\d+\.\d+') -le [datetime]$Times[27].Sunset
}

You could also loop through all the days of the given month:您还可以遍历给定月份的所有天数:

$daily = Get-Content C:\CumulusMX\summaries\Txt_Files\daily_values_uv_unedited.txt
foreach ($Time in $Times) {
    $Sunset = [datetime]$Time.Sunset
    $Sunrise = [datetime]$Time.Sunrise
    $daily | Where {
        [datetime]($_ -replace '\d+\.\d+') -ge $Sunrise -and [datetime]($_ -replace '\d+\.\d+') -le $Sunset
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM