我非常急于尝试查找企业域中所有Windows 7计算机的方法,并确定他们上次使用Microsoft Office的方式。我们正在审核中,需要明天完成(是的),我想看看我们是否可以在支付我们不需要的许可证之前卸载Office。
我有一段时间没有使用PowerShell,坦率地说,这是我昨晚从这段代码开始发现的 - 即newb。也许,有更好或更简单的方法?我昨晚尝试搜索预先完成的脚本或程序,但找不到任何说明Office最后一次使用的内容。
我可以使用下面的代码和函数获取最新的“LastUse”,但我需要知道该号码所关联的计算机名称。现在它只返回最新的数字: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
答案 0 :(得分:1)
输出真的只是 $ LastUsed |测量 - 最新强>
将其更改为
$LastUsed = $LastUsed | Measure-Latest
If ($LastUsed -eq "19800000"){
$LastUsed = "Never"
}
"$ComputerName : $Name : $LastUsed"
下面的完整脚本将为您提供单独的所有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"
}
}
如果您希望上次使用任何Office产品,那么将for的输出存储在变量中,并使用$ 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"
答案 1 :(得分:1)
我认为如果你在每台机器上本地运行它,这可能就是你要找的东西:
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
如果您是针对远程计算机列表运行它
$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
}
}