任何人都可以修改此脚本,因为我想将结果存储在 excel 中的列中,但它会拆分结果


$servers= Get-Content "Location "
$PatchingTracker="D:tracker.csv"
foreach($server in $servers)
{
if(Test-Connection -ComputerName $server)
{
$update= Get-HotFix -ComputerName $server -Description *  | out-file $PatchingTracker -Append
$uptime= Get-WmiObject win32_operatingsystem -ComputerName $server |     select csname, @{LABEL=’LastBootUpTime’ ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} | out-file $PatchingTracker -Append
}
}

这里有一种方法可以做我认为你想做的事情。[grin]

它的作用。。。

  • 设置常量
  • 创建一个系统列表
    当准备好使用真实数据执行此操作时,用一个获取计算机名称列表的调用替换整个#region/#endregion
  • 循环浏览计算机名称列表
  • 测试以查看系统是否响应
  • 如果是=从目标系统获取所需信息
  • IF no=将值设置为$NoResponse字符串
  • 建立一个具有所需属性的PSCO
    您可能希望更改此属性以更好地适应您的情况
  • 将该PSCO发送到$Results集合
  • 在屏幕上显示结果
  • 将结果保存到CSV以备日后使用[可能导入excel]

请注意,使用Invoke-Command而不是foreach循环会更快,因为它会在目标系统上运行部分代码 我太懒了,没有写出来。[grin]

代码。。。

$PatchReportFile = 'PatchReport_-_{0}.csv' -f (get-date -Format 'yyyy-MM-dd')
$PRDir = $env:TEMP
$FullPRFile = Join-Path -Path $PRDir -ChildPath $PatchReportFile
$NoResponse = '__n/a__'
#region >>> create server name list
# when ready to do this for real ...
#    - remove or comment out the entire "#region/#endregion" block
#    - add a "Get-Content" or other command to grab your computer list
$ServerList = @"
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
$env:COMPUTERNAME
"@ -split [System.Environment]::NewLine
#endregion >>> create server name list

$Results = foreach ($SL_Item in $ServerList)
{
if (Test-Connection -ComputerName $SL_Item -Count 2 -Quiet)
{
$CIM_OS = Get-CimInstance -ComputerName $SL_Item -ClassName CIM_OperatingSystem
$LastBootUpTime = $CIM_OS.LastBootUpTime
$UpTime_Days = [math]::Round(([datetime]::Now - $LastBootUpTime).TotalDays, 2)
$HotFixId_List = (Get-HotFix -ComputerName $SL_Item).HotFixId -join ', '
}
else
{
$LastBootUpTime = $NoResponse
$UpTime_Days = $NoResponse
$HotFixId_List = $NoResponse
}
[PSCustomObject]@{
ComputerName = $SL_Item
LastBootUpTime = $LastBootUpTime
UpTime_Days = $UpTime_Days
HotFixId_List = $HotFixId_List
}
}
# display on screen
$Results
# send to CSV
#    the delimiter is set to ';' so that excel will [hopefully] import it smoothly
$Results |
Export-Csv -LiteralPath $FullPRFile -NoTypeInformation -Delimiter ';'

屏幕上的输出。。。

ComputerName     LastBootUpTime         UpTime_Days HotFixId_List
------------     --------------         ----------- -------------
BetterNotBeThere __n/a__                __n/a__     __n/a__
LocalHost        2021-05-12 10:15:45 PM 1.38        KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB500...
10.0.0.1         __n/a__                __n/a__     __n/a__
127.0.0.1        2021-05-12 10:15:45 PM 1.38        KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB500...
[MySysName]      2021-05-12 10:15:45 PM 1.38        KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB500...

CSV文件内容。。。

"ComputerName";"LastBootUpTime";"UpTime_Days";"HotFixId_List"
"BetterNotBeThere";"__n/a__";"__n/a__";"__n/a__"
"LocalHost";"2021-05-12 10:15:45 PM";"1.38";"KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB5003173, KB5003242"
"10.0.0.1";"__n/a__";"__n/a__";"__n/a__"
"127.0.0.1";"2021-05-12 10:15:45 PM";"1.38";"KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB5003173, KB5003242"
"[MySysName]";"2021-05-12 10:15:45 PM";"1.38";"KB4601554, KB4561600, KB4570334, KB4577266, KB4577586, KB4580325, KB4586864, KB4589212, KB4593175, KB4598481, KB5003173, KB5003242"

您可以参考下面的脚本,因为它按预期工作,但我在excel中只得到了一个KB数字,否则它提供了所有细节。

$servers= Get-Content "#file path of the server list"
foreach($server in $servers)
{
$windowsupdate= Get-WmiObject -ComputerName $servers -ClassName Win32_QuickFixEngineering -Property * | select HotFixID, InstalledBy, InstalledOn
$uptime= Get-WmiObject win32_operatingsystem -ComputerName $servers | select csname, @{LABEL=’LastBootUpTime’ ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} 

foreach($update in $windowsupdate)
{
$CustomObject = New-Object -TypeName psobject
$CustomObject | Add-Member  -MemberType NoteProperty -Name 'Server Name' -Value $servers
$CustomObject | Add-Member  -MemberType NoteProperty -Name 'HotFix ID' -Value $update.HotFixID
$CustomObject | Add-Member  -MemberType NoteProperty -Name 'Installed By' -Value $update.InstalledBy
$CustomObject | Add-Member  -MemberType NoteProperty -Name 'Installed On' -Value $update.InstalledOn
$CustomObject | Add-Member  -MemberType NoteProperty -Name ' Reboot Time' -Value  $uptime.LastBootUpTime -PassThru
$CustomObject | Format-Table
$infoColl=$CustomObject 
}
} 
$infoColl | Export-Csv D:Patch_Tracker_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation #Export the results in csv file.

我再次尝试使用相同的脚本,结果如下。

Get-CimInstance : The client cannot connect to the destination specified in 
the request. Verify that the service on the destination is running and is 
accepting requests. Consult the logs and documentation for the WS-Management 
service running on the destination, most commonly IIS or WinRM. If the 
destination is the WinRM service, run the following command on the destination 
to analyze and configure the WinRM service: "winrm quickconfig".
At C:UsersshivamOneDrive - Essex Lake GroupScriptPatchTracker1.2V.ps1:20 
char:19
+ ...   $CIM_OS = Get-CimInstance -ComputerName $SL_Item -ClassName CIM_Ope ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ConnectionError: (rootcimv2:CIM_OperatingSystem 
:String) [Get-CimInstance], CimException
+ FullyQualifiedErrorId : HRESULT 0x80338012,Microsoft.Management.Infrastr 
ucture.CimCmdlets.GetCimInstanceCommand
+ PSComputerName        : localhost

Cannot find an overload for "op_Subtraction" and the argument count: "2".
At C:UsersshivamOneDrive - Essex Lake GroupScriptPatchTracker1.2V.ps1:22 
char:9
+         $UpTime_Days = [math]::Round(([datetime]::Now - $LastBootUpTi ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

ComputerName LastBootUpTime UpTime_Days                                        
------------ -------------- -----------                                        
localhost                   @{csname=ELGILE0635; LastBootUpTime=5/25/2021 5:... 

excel文件不计算任何内容。即使我尝试手动检查Cim_Instance命令,它也能工作。

相关内容

最新更新