powershell get-date计算指定日期及格式化

get-date -format "yyyyMMdd"  # 获取当天日期并格式化为20200107的格式

get-date -UFormat %V  # 获取当天是本年度的第几周(这里有一个bug,就是每周一获取到的还是上周)

(get-date).adddays(-1)| get-date -format "yyyyMMdd"  # 获取前一天的日期并格式化

(get-date).adddays(-1) | get-date -UFormat %V  # 获取前一天是本年度的第几周

 

# Windows Server Baseline Scanner.ps1 # 需要管理员权限执行 # 初始化日志文件 $reportPath = "$env:USERPROFILE\Desktop\BaselineReport_$(Get-Date -Format 'yyyyMMdd-HHmmss').html" $currentDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # HTML报告模板 $htmlHeader = @" <style> body {font-family: Consolas; margin: 20px} table {border-collapse: collapse; width: 100%} th {background: #004684; color: white; padding: 8px} td {padding: 6px; border: 1px solid #ddd} .warn {background: #FFF3CD} .critical {background: #F8D7DA} </style> <h2>Windows Server 基线扫描报告</h2> 扫描时间:$currentDate<br> 服务器名称:$($env:COMPUTERNAME) <hr> "@ # 1. 系统基础信息 $systemInfo = Get-WmiObject Win32_OperatingSystem | Select-Object Caption, Version, BuildNumber, OSArchitecture, @{Name="LastBootTime"; Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} # 2. 补丁状态检测 (当前/最新) $hotfixes = Get-HotFix | Select-Object HotFixID, InstalledOn, InstalledBy | Sort-Object InstalledOn -Descending $latestPatch = Get-WmiObject -Query "SELECT * FROM Win32_QuickFixEngineering" | Sort-Object InstalledOn -Descending | Select-Object -First 1 # 3. 组件版本检测函数 function Get-ComponentVersions { param($componentName) $output = switch($componentName) { ".NET Framework" { Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version, Release -ErrorAction SilentlyContinue | Where-Object { $_.Version -ne $null } | Select-Object @{n="组件";e={$_.PSChildName}}, @{n="当前版本";e={$_.Version}}, @{n="最新版本";e={[system.version]::new(4,8,0)}} } "PowerShell" { [PSCustomObject]@{ 组件 = "PowerShell" 当前版本 = $PSVersionTable.PSVersion.ToString() 最新版本 = "7.4.1" } } "IIS" { if (Test-Path "HKLM:\SOFTWARE\Microsoft\InetStp") { $iisVersion = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\InetStp" | Select-Object VersionString [PSCustomObject]@{ 组件 = "IIS" 当前版本 = $iisVersion.VersionString 最新版本 = "10.0.20348.1" # Windows Server 2022 IIS版本 } } } } return $output } # 4. 安全基线检查 $securityChecks = @( # 密码策略 @{ 检查项 = "密码最小长度" 当前值 = (Get-LocalSecurityPolicy "MinimumPasswordLength").Value 合规要求 = ">= 8" 状态 = if ((Get-LocalSecurityPolicy "MinimumPasswordLength").Value -ge 8) {"合规"} else {"不合规"} }, # 账户锁定策略 @{ 检查项 = "账户锁定阈值" 当前值 = (Get-LocalSecurityPolicy "LockoutBadCount").Value 合规要求 = "<= 5" 状态 = if ((Get-LocalSecurityPolicy "LockoutBadCount").Value -le 5) {"合规"} else {"不合规"} } ) # 生成HTML报告 ConvertTo-Html -Body $htmlHeader ` -PostContent "<h3>系统信息</h3>", ($systemInfo | ConvertTo-Html -Fragment), "<h3>已安装补丁(最近5个)</h3>", ($hotfixes | Select-Object -First 5 | ConvertTo-Html -Fragment), "<h3>组件版本状态</h3>", (Get-ComponentVersions ".NET Framework" | ConvertTo-Html -Fragment), (Get-ComponentVersions "PowerShell" | ConvertTo-Html -Fragment), (Get-ComponentVersions "IIS" | ConvertTo-Html -Fragment), "<h3>安全基线检查</h3>", ($securityChecks | ForEach-Object { [PSCustomObject]$_ } | ConvertTo-Html -Fragment) ` | Set-Content $reportPath Write-Host "扫描报告已生成:$reportPath" -ForegroundColor Green
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值