Powershell:提取部分值并按新格式输出
我目前正在学习Powershell,并正在编写一个脚本,从windows系统获取显示配置。我有两个问题: 问题1。剧本是:Powershell:提取部分值并按新格式输出,powershell,Powershell,我目前正在学习Powershell,并正在编写一个脚本,从windows系统获取显示配置。我有两个问题: 问题1。剧本是: "Display Config" >> system1.txt "---------------------------------------------------------------------------- ---------------------------------" >> system1.txt add-type -assem
"Display Config" >> system1.txt
"----------------------------------------------------------------------------
---------------------------------" >> system1.txt
add-type -assemblyName system.windows.forms
[system.windows.forms.screen]::AllScreens | Format-List Bounds | out-file -append system1.txt
输出是两个监视器分辨率的结果,如下所示:
Bounds : {X=0,Y=0,Width=1920,Height=1080}
Bounds : {X=1920,Y=0,Width=2560,Height=1440}
但我只想提取'Width'和'Height'的值,并使输出显示如下:
Width:1920
Height:1080
Width:2560
Height:1440
问题2:对于此脚本:
Get-WmiObject WmiMonitorconnectionparams -Namespace root\wmi | format-List
VideoOutputTechnology | out-file -append system1.txt
我得到的结果是:
VideoOutputTechnology : 10
VideoOutputTechnology : 4
但是需要根据url对值4和10进行解码,即'10=Displayport External'
:
我如何根据URL解码该值,并使结果仅在输出txt中显示为类似'Displayport External'
非常感谢您的回复。关于
视频输出技术
值。是的,它们是来自D3DKMDT\u VIDEO\u OUTPUT\u技术
枚举的值,但我相信您希望返回更具描述性的字符串。在这种情况下,您可以使用一个小函数将值转换为字符串:
function Resolve-VideoOutputTechnology {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 0)]
[int64]$VideoOutputTechnology
)
switch ($VideoOutputTechnology) {
-2 { return 'Uninitialized connector' } # D3DKMDT_VOT_UNINITIALIZED
-1 { return 'Unknown connector' } # D3DKMDT_VOT_OTHER
0 { return 'VGA connector' } # D3DKMDT_VOT_HD15
1 { return 'S-video connector' } # D3DKMDT_VOT_SVIDEO
2 { return 'Composite video connector' } # D3DKMDT_VOT_COMPOSITE_VIDEO
3 { return 'Component video connector' } # D3DKMDT_VOT_COMPONENT_VIDEO
4 { return 'Digital Video Interface (DVI) connector' } # D3DKMDT_VOT_DVI
5 { return 'High-Definition Multimedia Interface (HDMI) connector' } # D3DKMDT_VOT_HDMI
6 { return 'Low Voltage Differential Swing (LVDS) or Mobile Industry Processor Interface (MIPI) Digital Serial Interface (DSI) connector' } # D3DKMDT_VOT_LVDS
8 { return 'D-Jpn connector' } # D3DKMDT_VOT_D_JPN
9 { return 'SDI connector' } # D3DKMDT_VOT_SDI
10 { return 'External display port' } # D3DKMDT_VOT_DISPLAYPORT_EXTERNAL
11 { return 'Embedded display port' } # D3DKMDT_VOT_DISPLAYPORT_EMBEDDED
12 { return 'External Unified Display Interface (UDI)' } # D3DKMDT_VOT_UDI_EXTERNAL
13 { return 'Embedded Unified Display Interface (UDI)' } # D3DKMDT_VOT_UDI_EMBEDDED
14 { return 'Dongle cable that supports SDTV connector' } # D3DKMDT_VOT_SDTVDONGLE
15 { return 'Miracast connected session' } # D3DKMDT_VOT_MIRACAST
0x80000000 { return 'Internally display device (the internal connection in a laptop computer)' } # D3DKMDT_VOT_INTERNAL
default { return 'Unknown connector' }
}
}
问题1: 在评论中有效地提供了解决方案(PSv3+):
直接应用于.Bounds
返回的数组,在这种情况下,可以方便地返回数组元素各自的::AllScreens
属性值数组,这是一种称为PSv3+的功能.Bounds
然后从生成的设置列表宽度、高度的格式
实例中提取[System.Drawing.Rectangle]
和.Width
属性值,并将其显示为列表.Height
- 注意:所有
cmdlet的目的是创建仅用于显示的输出,即生成对人类观察者友好但不适合进一步编程处理的输出Format-*
- 注意:所有
- 由于您使用的是
,无需其他选项,Out File-Append
是一种方便的快捷方式。(您将获得UTF16 LE编码(“Unicode”)文件。)>
问题2: PowerShell内置了对.NET枚举的强大支持(
[enum]
-派生类型),但WMI在您的情况下报告的是简单整数[System.UInt32]
,因此您必须执行自己的映射
在PSv5+中,您可以定义自己的[enum]
类型,但是,在这种情况下,对该类型的简单转换可以帮助您:
enum VideoOutputTechnology {
D3DKMDT_VOT_UNINITIALIZED = -2
D3DKMDT_VOT_OTHER = -1
D3DKMDT_VOT_HD15 = 0
# ...
D3DKMDT_VOT_DVI = 4
D3DKMDT_VOT_DISPLAYPORT_EXTERNAL = 10
# ...
}
在PSv4中,您可以使用Add Type-TypeDefinition
通过包含C#enum定义的字符串定义枚举。
注:
- 我保留了最初的符号常量名称,但您可以随意重命名为更友好的名称;e、 例如,
->D3DKMDT\u VOT\u DISPLAYPORT\u EXTERNAL
-但请注意,不允许使用嵌入空格和特殊字符。如果不够友好的话,请考虑.< /P>DISPLAYPORT\u EXTERNAL
- 您正在创建符号常量的静态副本,因此这两个集合可能会不同步,尽管可能很少添加新常量。
(我不知道有任何预先存在的.NET枚举类型定义了这些常量,它们似乎是在
文件中定义的,您不能假设该文件存在于每台机器上;您可以通过web刮取URL,但这很脆弱。)*.h
Get-WmiObject WmiMonitorconnectionparams -Namespace root\wmi |
Format-List @{
n='VideoOutputTechnology'
e={ [VideoOutputTechnology] $_.VideoOutputTechnology }
} >> system1.txt
这将产生:
VideoOutputTechnology : D3DKMDT_VOT_DISPLAYPORT_EXTERNAL
VideoOutputTechnology : D3DKMDT_VOT_DVI
([system.windows.forms.screen]::AllScreens.Bounds)|选择宽度、高度
为什么不直接访问[system.windows.forms.screen]::AllScreens.Bounds
上的宽度和高度?
VideoOutputTechnology : D3DKMDT_VOT_DISPLAYPORT_EXTERNAL
VideoOutputTechnology : D3DKMDT_VOT_DVI