以下代码中使用的InstallDate属性 -
$InstalledProgram | Select-Object DisplayName, DisplayVersion, Publisher,
InstallDate | Format-Table –AutoSize
$logDisplayName = $InstalledProgram.DisplayName
$logPublisher = $InstalledProgram.Publisher
$logVersion = $InstalledProgram.DisplayVersion
$logInstallDate= $InstalledProgram.InstallDate
它以20170921
格式显示已安装应用程序的日期,但我希望它以21/09/2017
格式显示DD/MM/YYYY
。我该怎么格式化呢?
下面是整个代码,用于将输出复制到日志文件: -
Clear-Host
$scriptPath = $PSScriptRoot
$logFilePath= Join-path $scriptPath "POCTestResults.log"
# If log file exists, then clear its contents
if (Test-Path $logFilePath)
{
clear-content -Path $logFilePath
}
# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started."
add-content -Path $logFilePath -Value $log -Force
add-content -Path $logFilePath -Value $logString -Force
add-content -Path $logFilePath -Value "`n" -Force
# Validate ADD/Remove Program list
# FUNCTION DEFINITIONS
function Log-InstalledProgram($InstalledProgram, $LogFilePath)
{
#$InstalledProgram | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
#Format-Table –AutoSize
$InstalledProgram | Select-Object DisplayName, DisplayVersion, Publisher, @{Name="InstallDate"; Expression={([datetime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).toshortdatestring()}} | Format-Table –AutoSize
$logDisplayName = $InstalledProgram.DisplayName
$logPublisher = $InstalledProgram.Publisher
$logVersion = $InstalledProgram.DisplayVersion
$logInstallDate= $InstalledProgram.InstallDate
add-content -Path $LogFilePath -Value "Product Name: $logDisplayName" -Force
add-content -Path $LogFilePath -Value "Publisher: $logPublisher" -Force
add-content -Path $LogFilePath -Value "Version: $logVersion" -Force
add-content -Path $LogFilePath -Value "InstallDate: $logInstallDate" -Force
add-content -Path $LogFilePath -Value "`n" -Force
}
add-content -Path $logFilePath -Value "`n" -Force
add-content -Path $logFilePath -Value "Add/Remove Programs :" -Force
add-content -Path $logFilePath -Value "`n" -Force
$InstalledPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
foreach ($InstalledProgram in $InstalledPrograms )
{
foreach ($displayName in "IntelliMatch Operational Control","intelliSuite Management Studio", "SunGard System Analyzer", "STeP")
{
if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains($displayName)))
{
Log-InstalledProgram $InstalledProgram $logFilePath
}
}
}
这是我用过的脚本。
答案 0 :(得分:1)
使用[datetime]::ParseExact()
方法和calculated properties检索Select-Object
中的日期时间格式,并使用.toshortdatestring()
将其格式化为短日期字符串:
$InstalledProgram | Select-Object DisplayName, DisplayVersion, Publisher, @{Name="InstallDate"; Expression={([datetime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).toshortdatestring()}} | Format-Table –AutoSize
$logDisplayName = $InstalledProgram.DisplayName
$logPublisher = $InstalledProgram.Publisher
$logVersion = $InstalledProgram.DisplayVersion
$logInstallDate= $InstalledProgram.InstallDate