[英]PowerShell Last Time MS Office was Used
I am in a super hurry to try and find a way to query all of our Windows 7 computers on the corporate domain, and determine the last time they used Microsoft Office. 我非常急于尝试找到一种查询公司域中所有Windows 7计算机的方法,并确定它们上次使用Microsoft Office的时间。 We are under audit, that needs to be finished tomorrow (yep), and I want to see if we can uninstall Office before paying for licenses we don't need.
我们正在接受审核,需要明天(yep)完成,我想看看我们是否可以在卸载不需要的许可证之前卸载Office。
I haven't used PowerShell in a while, and frankly this is what I've found since starting on this code last night - ie newb. 我已经有一段时间没有使用PowerShell了,坦率地说,这是自从昨晚在这段代码上开始以来找到的-即newb。 Maybe, there's a much better or easier way?
也许,有更好更好的方法吗? I tried searching last night for a pre-done script or program, but couldn't find anything that says when Office was last used.
昨晚我尝试搜索预先编写的脚本或程序,但找不到上一次使用Office的内容。
I can get the latest 'LastUse' using the below code and function, but I need to know which computer name that number is associated with. 我可以使用下面的代码和功能来获取最新的“ LastUse”,但我需要知道该数字与哪个计算机名称相关联。 Right now it just returns the latest number: 20170928
现在,它只返回最新的数字:20170928
function Measure-Latest {
BEGIN { $latest = $null }
PROCESS {
if (($_ -ne $null) -and (($latest -eq $null) -or ($_ -gt $latest))) {
$latest = $_
}
}
END { $latest }
}
$Software = Get-WmiObject -Class win32_softwarefeature | Select Caption,LastUse
$ComputerName = $env:COMPUTERNAME
$(foreach ($item in $Software)
{
$Name = $Item.Caption
$LastUsedString = $Item.Lastuse.Substring(0,8)
$LastUsed = [int]$LastUsedString
if ($Name -like 'Microsoft Office*' -or $Name -like 'Microsoft Outlook')
{
$LastUsed
} }) | Measure-Latest
The output was really just $LastUsed | 输出实际上只是$ LastUsed | Measure-Latest
最新测量
Changed that to 更改为
$LastUsed = $LastUsed | Measure-Latest
If ($LastUsed -eq "19800000"){
$LastUsed = "Never"
}
"$ComputerName : $Name : $LastUsed"
Full script below will give you the last use of all Microsoft Office products individually 下面的完整脚本将使您分别单独使用所有Microsoft Office产品
function Measure-Latest {
BEGIN {
$latest = $null
}
PROCESS {
if (($_ -ne $null) -and (($latest -eq $null) -or ($_ -gt $latest))) {
$latest = $_
}
}
END {
$latest
}
}
$Software = Get-WmiObject -Class win32_softwarefeature | Select Caption,LastUse
$ComputerName = $env:COMPUTERNAME
foreach ($item in $Software) {
$Name = $Item.Caption
$LastUsedString = $Item.Lastuse.Substring(0,8)
$LastUsed = [int]$LastUsedString
if ($Name -like 'Microsoft Office*' -or $Name -like 'Microsoft Outlook') {
$LastUsed = $LastUsed | Measure-Latest
If ($LastUsed -eq "19800000"){
$LastUsed = "Never"
}
"$ComputerName : $Name : $LastUsed"
}
}
If you would like just the last time any Office Product was used then store the output of the for each in a variable and call it with $Computername variable 如果您只想最后一次使用任何Office产品,则将每个Office产品的输出存储在变量中,并使用$ Computername变量进行调用
function Measure-Latest {
BEGIN {
$latest = $null
}
PROCESS {
if (($_ -ne $null) -and (($latest -eq $null) -or ($_ -gt $latest))) {
$latest = $_
}
}
END {
$latest
}
}
$Software = Get-WmiObject -Class win32_softwarefeature | Select Caption,LastUse
$ComputerName = $env:COMPUTERNAME
$LastRun = $(foreach ($item in $Software) {
$Name = $Item.Caption
$LastUsedString = $Item.Lastuse.Substring(0,8)
$LastUsed = [int]$LastUsedString
if ($Name -like 'Microsoft Office*' -or $Name -like 'Microsoft Outlook') {
$LastUsed
}
}) | Measure-Latest
"$ComputerName : $LastRun"
I think this might be what you are looking for if you are running it local on each machine: 我认为这可能是您在每台计算机上本地运行时要寻找的东西:
Get-WmiObject -Class Win32_SoftwareFeature |
Where-Object {$_.Caption -match 'Microsoft\sOffice|Microsoft\sOutlook'} |
Select-Object Caption, @{n='LastUse';e={[int]($_.Lastuse.Substring(0, 8))}},@{n='ComputerName';e={$env:COMPUTERNAME}} |
Sort-Object LastUse |
Select-Object -Last 1
If you are running it against a list of remote computers 如果正在针对远程计算机列表运行它
$ComputerNames = Get-Content C:\example\list.txt
Get-WmiObject -Class win32_softwarefeature -ComputerName $ComputerNames |
Where-Object {$_.Caption -match 'Microsoft\sOffice|Microsoft\sOutlook'} |
Select-Object Caption, @{n='LastUse';e={[int]($_.Lastuse.Substring(0, 8))}},PSComputername |
Group-Object PSComputername |
ForEach-Object {
$Group = $_.Group |
Sort-Object LastUse |
Select-Object -Last 1
[PSCustomObject]@{
ComputerName = $_.Name
Caption = $Group.Caption
LastUse = $Group.LastUse
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.