[玩转系统] 使用 CIM 搜索 PowerShell
作者:精品下载站 日期:2024-12-14 08:03:40 浏览:14 分类:玩电脑
使用 CIM 搜索 PowerShell
昨天我分享了一个脚本,您可以使用它来清点 Windows PowerShell 和 PowerShell 7 安装的系统。这应该适用于大多数使用提供的安装程序安装 PowerShell 7 的人。但是,正如我不止一次指出的那样,这不会检测到任何侧面加载或带外安装。我在上一篇文章中提到过这一点。我认为您能做的最好的事情就是搜索 pwsh.exe 的硬盘驱动器实例。但我们不想进行强力递归目录搜索。相反,我将使用 Get-CimInstance。
Get-CimInstance -ClassName CIM_Logicalfile -Filter "filename='pwsh' AND extension = 'exe' AND drive='C:'"
我将搜索限制为驱动器 C 以加快速度。这种类型的搜索可以找到稳定安装和预览安装。
这为我提供了与之前相同的信息以及更多信息。我仍然可以使用 Get-Command 以相同的方式搜索 Windows PowerShell。
剧本
这是修改后的脚本。
#requires -version 5.1
[cmdletbinding()]
Param(
[Parameter(HelpMessage = "Specify the drive to search for instances of pwsh.exe")]
[ValidatePattern("^[c-zC-Z]$")]
[string]$Drive = "C"
)
#a private function to shorten filenames
Function _shortName {
Param($path)
$parts = $path.split("\")
$out = foreach ($part in $parts) {
if ($part -match "\s") {
"{0}~1" -f $part.Substring(0, 6)
}
else {
$part
}
}
$out -join "\"
}
Write-Verbose "Searching for PowerShell installations on $([System.Environment]::MachineName)"
$os = (Get-CimInstance -ClassName Win32_OperatingSystem -Property Caption).caption
#region Windows Powershell
Try {
Write-Verbose "Testing for Windows PowerShell"
$cmd = Get-Command -Name powershell.exe -ErrorAction stop
if ($cmd) {
Write-Verbose "Using $($cmd.path)"
#need to run Powershell.exe to get $PSVersion
$psh = &$cmd.path -nologo -noprofile -command { Get-Host }
#test for PowerShell 2.0 engine or feature
Write-Verbose "Testing for Windows PowerShell 2.0 engine or feature"
#get computersystem roles to determine if running on a server or client
#assuming operating system caption uses 'Server'
if ($os.caption -match "server") {
Write-Verbose "Running Get-WindowsFeature"
$f = Get-WindowsFeature PowerShell-V2
}
else {
Write-Verbose "Running Get-WindowsOptionalFeature"
$f = Get-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV2Root
}
if ($f.installed -OR $f.State -eq 'Enabled') {
$comment = "Windows PowerShell 2.0 feature enabled or installed"
}
else {
$comment = ""
}
#the install date will reflect the last time the OS was updated or installed
[pscustomobject]@{
PSTypeName = "PSInstallInfo"
Name = $cmd.Name
Path = $cmd.source
PSVersion = $psh.Version
Installed = (Get-Item $cmd.source).CreationTime
Comments = $comment
Computername = [System.Environment]::MachineName
OperatingSystem = $os
}
Remove-Variable cmd
} #$cmd found
} #try
Catch {
#this should never happen
Write-Verbose "Windows PowerShell not installed on $([Environment]::MachineName)."
}
#endregion
#region PowerShell 7
Write-Verbose "Testing for PowerShell 7 including preview builds"
$pwsh = Get-CimInstance -ClassName CIM_Logicalfile -Filter "filename='pwsh' AND extension = 'exe' AND drive='$($drive):'"
if ($pwsh) {
Foreach ($item in $pwsh) {
if ($item.path -match 'preview') {
$comment = "Preview"
}
else {
$comment = ""
}
#test for SSH
if (Test-Path $env:programdata\ssh\sshd_config ) {
#get short name
$short = _shortName $item.EightDotThreeFileName
Write-Verbose "Testing for an SSH subsystem using $short"
$ssh = Get-Content $env:programdata\ssh\sshd_config | Select-String $($short -replace "\", "\")
#'(?<=powershell\s).*pwsh.exe'
if ($ssh.matches.value -eq $short) {
$comment += " SSH configured"
}
}
[pscustomobject]@{
PSTypeName = "PSInstallInfo"
Name = Split-Path $item.name -Leaf
Path = Split-Path $item.name
PSVersion = $item.version
Installed = $item.InstallDate
Comments = $comment.Trim()
Computername = [System.Environment]::MachineName
OperatingSystem = $os
}
}
}
#endregion
#end of script
变化
我在此版本中做了一些更改。首先,我决定不将 PowerShell 2.0 引擎视为单独的版本。您对 5.1 和 2.0 使用相同的 powershell.exe 版本。相反,我在评论中反映了 2.0 引擎的状态。我还采用了不同的 SSH 方法。我没有测试 SSH 是否已安装并运行,而是检查 sshd_config 文件中是否存在 PowerShell 子系统。这使您可以在 PowerShell 中使用 SSH 远程处理进行连接。
这需要一些操作,因为来自 Windows 的长文件名在 sshd_config 文件中被截断。因此,我不是搜索 C:\Program Files\PowerShell\7\pwsh.exe”,而是寻找“c:\progra~1\powershell\7\pwsh.exe”。我编写了一个简短的辅助函数来转换路径。我意识到这并不是万无一失的,但它适用于我的 SSH 安装。
这是默认的本地输出。
和以前一样,您可以使用远程处理来清点其他服务器和桌面。
$c = Invoke-Command -FilePath C:\scripts\Get-PSExeFile.ps1 -ComputerName win10,srv1,srv2 -HideComputerName | sort computername
你喜欢那个吗?我修改了ps1xml文件的格式。
<!--
Format type data generated 07/13/2021 12:14:45 by PROSPERO\Jeff
This file was created using the New-PSFormatXML command that is part
of the PSScriptTools module.
https://github.com/jdhitsolutions/PSScriptTools
-->
<Configuration>
<ViewDefinitions>
<View>
<!--Created 07/13/2021 12:14:45 by PROSPERO\Jeff-->
<Name>default</Name>
<ViewSelectedBy>
<TypeName>PSInstallInfo</TypeName>
</ViewSelectedBy>
<GroupBy>
<ScriptBlock>
if ($host.name -match "console|code|serverremotehost") {
"{0} [$([char]27)[3m{1}$([char]27)[0m]" -f $_.Computername,$_.OperatingSystem
}
else {
"{0} [{1}]" -f $_.Computername,$_.OperatingSystem
}
</ScriptBlock>
<Label>Computername</Label>
</GroupBy>
<TableControl>
<!--Delete the AutoSize node if you want to use the defined widths
<AutoSize />.-->
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
<Width>19</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>PSVersion</Label>
<Width>16</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Installed</Label>
<Width>12</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Comments</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if (($host.name -match "console|code|serverremotehost") -AND ($_.path -match "preview")) {
"$([char]27)[38;5;219m$($_.PSVersion)$([char]27)[0m"
}
else {
$_.PSVersion
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.Installed.ToShortDateString()</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if (($host.name -match "console|code|serverremotehost") -AND ($_.comments-match "SSH configured")) {
<!-- replace SSH Detected with an ANSI sequence-->
$_.comments -replace "SSH configured","$([char]27)[1;38;5;155mSSH configured$([char]27)[0m"
}
else {
$_.comments
}
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
概括
此时,您应该有多种选择。希望其中之一能够满足您的需求。或者随意剪切所需的部分并将其粘贴到您自己的工具中。如果您采用这种方法,我希望您能分享您的工作。随时欢迎提问和评论。享受。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag