$startdate = [DateTime]'2022-06-01' $numberOfWeeks = 52 $result = for ($week = 0; $week-lt$numberOfWeeks; $week ++) { # calculate the real date each week $realdate = $startdate + (New-Timespan-days (7*$week))
# calculate the current month $month = $realdate.Month
# calculate the days in this month $daysInMonth = [DateTime]::DaysInMonth($realdate.Year, $realdate.Month)
# make arbitrary adjustments, i.e. set start time to 12PM by default, but 7PM on the last week of a month
# are we in the last week of a month? if ($realdate.Day -gt ($daysInMonth-7)) { # add 19 hours $realdate = $realdate.AddHours(19) } else { # add 12 hours $realdate = $realdate.AddHours(12) }
# create your Excel sheet layout as a CSV file [PSCustomObject]@{ Start = $realdate IsOnline = $false Title = '' Speaker = '' Notes = '' } }
# properties to exclude (add or remove as needed) $blacklist = 'FileList', 'Tags'
$data = Invoke-RestMethod-Uri$url-UseBasicParsing | ForEach-Object { $hash = [Ordered]@{} $moduleInfo = $_.Properties foreach($_in$moduleInfo.PSObject.Properties) { # name of property $name = $_.Name # if it is in blacklist, skip and continue with next property if ($name-in$blacklist) { continue } # if it is the property "name", then skip # all remaining (xml default properties) if ($name-eq'Name') { break }
# if type is "xmlelement", retrieve underlying text value in #text if ($_.TypeNameOfValue -eq'System.Xml.XmlElement') { $hash[$name] = $moduleInfo.$name.'#text'
# if a datatype is assigned, try and convert to appropriate type if ($moduleInfo.$name.type -like'Edm.*') { $typename = $moduleInfo.$name.type.replace('Edm.','') $hash[$name] = $hash[$name] -as$typename } } else { $hash[$name] = $_.Value } }
# convert a hash table to object and return it [PSCustomObject]$hash }
# determine the primary module location for your PowerShell version $path = if ('Management.Automation.Platform'-as [Type]) { # PowerShell CLR if ([Environment]::OSVersion.Platform -like'Win*'-or$IsWindows) { # on Windows Join-Path-Path$env:ProgramFiles-ChildPath'PowerShell' } else { # on Non-Windows $name = [Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES') Split-Path-Path$name-Parent } } else { # Windows PowerShell Join-Path-Path$env:ProgramFiles-ChildPath"WindowsPowerShell" }
在 Windows 上,PowerShell 7 和 Windows PowerShell 可以共享一个文件夹,因此如果您不想专门为 PowerShell 7 部署模块,则可以进一步简化脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# determine the primary module location for your PowerShell version $path = if ([Environment]::OSVersion.Platform -like'Win*'-or$IsWindows) { # Windows Join-Path-Path$env:ProgramFiles-ChildPath"WindowsPowerShell" } else { # Non-Windows $name = [Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES') Split-Path-Path$name-Parent }