英文:
How to Get Server Last Updates Details along with Last Boot Time Rebooted Using Powershell
问题
我需要获取每日日志,其格式如下:
Source Description HotFixID InstalledOn LastRebooted
------ ----------- -------- ----------- -----------
SRV1... Update KB4049065 2/2/2018 12:00:00 AM 1/2/2019 10:00:00 AM
SRV2... Security Update KB4048953 2/2/2018 12:00:00 AM 2/2/2019 09:00:00 AM
我已经尝试了以下这些PowerShell代码:
这个用于按日期显示更新:
get-wmiobject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending
而这个用于显示上次服务器重启时间:
Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime
我尝试将两个代码合并以在同一表格中显示它们,但没有成功。
我甚至尝试了这个:
get-wmiobject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
但我得到的结果看起来像这样:
csname LastBootUpTime
------ --------------
SRV1...
SRV2...
我漏掉了什么?
英文:
I need to get daily log that will look like that:
Source Description HotFixID InstalledOn LastRebooted
------ ----------- -------- ----------- -----------
SRV1... Update KB4049065 2/2/2018 12:00:00 AM 1/2/2019 10:00:00 AM
SRV2... Security Update KB4048953 2/2/2018 12:00:00 AM 2/2/2019 09:00:00 AM
All achived is those Powershell codes:
This one Display Updates by Date:
get-wmiobject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending
And that one is Show Last Time Server Rebooted:
Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime
I try to combine both codes so it will display them in The same table but without success.
I even try this:
get-wmiobject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
But all i got is Result that looks like that:
csname LastBootUpTime
------ --------------
SRV1...
SRV2...
What am i missing?
答案1
得分: 1
以下是代码的翻译部分:
我相信这是你要找的内容。
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | ForEach-Object -Begin {} -Process {
Write-Output ([PSCustomObject]@{
Source = $_.CSName
Description = $_.Description
HotFixID = $_.HotFixID
InstalledOn = $_.InstalledOn
LastRebooted = (Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime
})
} -End {} | Format-Table
如果你想保持你之前尝试的样式,这个也可以用于那个目的。
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | Select @{LABEL='SOURCE';EXPRESSION={$_.CSName}},Description,HotFixID,InstalledOn,@{LABEL='LastBootUpTime';EXPRESSION={(Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime}} | Format-Table
如果你在Get-WmiObject cmdlet上运行Get-Member,你会看到你试图选择一个在这些对象上不存在的属性(lastbootuptime)。这就是你得到null值的原因。
运行Get-Member:
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | gm
Get-Member的输出:
TypeName: System.Management.ManagementObject#root\cimv2\Win32_QuickFixEngineering
Name MemberType Definition
---- ---------- ----------
PSComputerName AliasProperty PSComputerName = __SERVER
Caption Property string Caption {get;set;}
CSName Property string CSName {get;set;}
Description Property string Description {get;set;}
FixComments Property string FixComments {get;set;}
HotFixID Property string HotFixID {get;set;}
InstallDate Property string InstallDate {get;set;}
InstalledBy Property string InstalledBy {get;set;}
Name Property string Name {get;set;}
ServicePackInEffect Property string ServicePackInEffect {get;set;}
Status Property string Status {get;set;}
__CLASS Property string __CLASS {get;set;}
__DERIVATION Property string[] __DERIVATION {get;set;}
__DYNASTY Property string __DYNASTY {get;set;}
__GENUS Property int __GENUS {get;set;}
__NAMESPACE Property string __NAMESPACE {get;set;}
__PATH Property string __PATH {get;set;}
__PROPERTY_COUNT Property int __PROPERTY_COUNT {get;set;}
__RELPATH Property string __RELPATH {get;set;}
__SERVER Property string __SERVER {get;set;}
__SUPERCLASS Property string __SUPERCLASS {get;set;}
PSStatus PropertySet PSStatus {__PATH, Status}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
InstalledOn ScriptProperty System.Object InstalledOn {get=if ([environment]::osversion.version.build -ge 7000)...
希望这对你有帮助。
英文:
I believe this is what you are looking for.
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | ForEach-Object -Begin {} -Process {
Write-Output ([PSCustomObject]@{
Source = $_.CSName
Description = $_.Description
HotFixID = $_.HotFixID
InstalledOn = $_.InstalledOn
LastRebooted = (Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime
})
} -End {} | Format-Table
And if you want to keep the same style that you were trying to do this works for that.
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | Select @{LABEL='SOURCE';EXPRESSION={$_.CSName}},Description,HotFixID,InstalledOn,@{LABEL='LastBootUpTime';EXPRESSION={(Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime}} | Format-Table
If you run Get-Member on the Get-WmiObject cmdlet you will see that you are trying to select a property that doesn't exist on those objects (lastbootuptime). Hence the null value you are getting.
Running Get-Member:
Get-WmiObject -class win32_quickfixengineering | Sort-Object -Property {$_.InstalledOn} -Descending | gm
Output of Get-Member:
TypeName: System.Management.ManagementObject#root\cimv2\Win32_QuickFixEngineering
Name MemberType Definition
---- ---------- ----------
PSComputerName AliasProperty PSComputerName = __SERVER
Caption Property string Caption {get;set;}
CSName Property string CSName {get;set;}
Description Property string Description {get;set;}
FixComments Property string FixComments {get;set;}
HotFixID Property string HotFixID {get;set;}
InstallDate Property string InstallDate {get;set;}
InstalledBy Property string InstalledBy {get;set;}
Name Property string Name {get;set;}
ServicePackInEffect Property string ServicePackInEffect {get;set;}
Status Property string Status {get;set;}
__CLASS Property string __CLASS {get;set;}
__DERIVATION Property string[] __DERIVATION {get;set;}
__DYNASTY Property string __DYNASTY {get;set;}
__GENUS Property int __GENUS {get;set;}
__NAMESPACE Property string __NAMESPACE {get;set;}
__PATH Property string __PATH {get;set;}
__PROPERTY_COUNT Property int __PROPERTY_COUNT {get;set;}
__RELPATH Property string __RELPATH {get;set;}
__SERVER Property string __SERVER {get;set;}
__SUPERCLASS Property string __SUPERCLASS {get;set;}
PSStatus PropertySet PSStatus {__PATH, Status}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
InstalledOn ScriptProperty System.Object InstalledOn {get=if ([environment]::osversion.version.build -ge 7000)...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论