Regex Powershell将字段和格式添加到单行字段=值

Regex Powershell将字段和格式添加到单行字段=值,regex,powershell,formatting,Regex,Powershell,Formatting,我的Powershell报告需要: [时区信息]::本地 并在对象/格式列表中添加两个字段:主机名(或$env:computername)和日期字段 然后将所有“\t:\t”(选项卡之间的冒号)替换为“=”,将所有“\n”(新行)替换为“,” 关于最终结果: Id=中央标准时间,DisplayName=(UTC-06:00)中央时间(美国和加拿大),StandardName=中央标准时间,DaylightName=中央夏时制 BaseUtcOffset=-06:00:00,SupportsDay

我的Powershell报告需要: [时区信息]::本地

并在对象/格式列表中添加两个字段:主机名(或$env:computername)和日期字段

然后将所有“\t:\t”(选项卡之间的冒号)替换为“=”,将所有“\n”(新行)替换为“,”

关于最终结果: Id=中央标准时间,DisplayName=(UTC-06:00)中央时间(美国和加拿大),StandardName=中央标准时间,DaylightName=中央夏时制 BaseUtcOffset=-06:00:00,SupportsDaylightSavingTime=True,hostname=ANDREW-PC

拉取感兴趣的数据的相应powershell命令如下(简单):

最好的方法是什么

已添加,到目前为止,我已使用以下代码添加了两个字段:

$tz=[TimeZoneInfo]::Local
$time=date
Add-Member -inputobject $tz -membertype noteproperty -name time -value $time
$hostname=$env:computername
Add-Member -inputobject $tz -membertype noteproperty -name hostname -value $hostname
以下是我的最终解决方案:

$os=Get-CimInstance Win32_OperatingSystem | Select-Object  @{Expression={$_.caption};Label="os"},@{Expression={$_.InstallDate};Label="time_os_install"},@{Expression={$_.OSArchitecture};Label="os_arch"},@{Expression={$_.BootDevice};Label="os_boot"},@{Expression={$_.CSName};Label="hostname_os"},@{Expression={$_.SystemDirectory};Label="os_dir"},@{Expression={$_.RegisteredUser};Label="user_os"},@{Expression={$_.SerialNumber};Label="os_serial"},@{Expression={$_.Version};Label="os_ver"}
$bios=Get-wmiObject -class win32_bios | Select-Object @{Expression={$_.SMBIOSBIOSVersion};Label="bios_ver"},@{Expression={$_.Manufacturer};Label="bios_company"},@{Expression={$_.Name};Label="bios_name"},@{Expression={$_.Version};Label="version"}
$ipconf=Get-NetIPConfiguration -InterfaceAlias *Connection | Select-Object @{Expression={$_.ComputerName};Label="hostname"},@{Expression={$_.InterfaceAlias};Label="name"},@{Expression={$_.InterfaceIndex};Label="index"},@{Expression={$_.InterfaceDescription};Label="description"},@{Expression={$_.IPv4Address};Label="src"},@{Expression={$_.IPv4DefaultGateway.NextHop};Label="gateway"},@{Expression={$_.DNSServer.ServerAddresses[0]};Label="dns1"},@{Expression={$_.DNSServer.ServerAddresses[1]};Label="dns2"},@{Expression={$_.NetAdapter.MacAddress};Label="mac"},@{Expression={$_.NetAdapter.Status};Label="status"}
$tz=[TimeZoneInfo]::Local | Select-Object @{Expression={$_.Id};Label="tz_id"},@{Expression={$_.BaseUtcOffset};Label="tz_utc"},@{Expression={$_.SupportsDaylightSavingTime};Label="tz_dst"}
$time=Get-Date -UFormat %s
$hostname=[Environment]::MachineName
$user=[Environment]::UserName

$pattern_src = "[^\d\.].+"
$src=$ipo."IPv4 Address" -Replace $pattern_src,""

$pattern_ver2 = "[^a-zA-Z\d\-]+"
$bios_ver2=$bios.version -Replace $pattern_ver2,""

$computer = New-Object PsObject -property @{user=$user; hostname=$hostname; time=$time; os=$os.os; time_os_install=$os.time_os_install; os_arch=$os.os_arch; os_boot=$os.os_boot; hostname_os=$os.hostname_os; os_dir=$os.os_dir; user_os=$os.user_os; os_serial=$os.os_serial; os_ver=$os.os_ver; bios_ver=$bios.bios_ver; bios_company=$bios.bios_company; bios_name=$bios.bios_name; bios_ver2=$bios_ver2; tz_id=$tz.tz_id; tz_utc=$tz.tz_utc; tz_dst=$tz.tz_dst;adapter_hostname=$ipconf.hostname; adapter_name=$ipconf.name; adapter_index=$ipconf.index; adapter_description=$ipconf.description; src=$ipconf.src; gateway=$ipconf.gateway; dns1=$ipconf.dns1; dns2=$ipconf.dns2; mac=$ipconf.mac; adapter_status=$ipconf.status}

(($computer | out-string).split("`r`n") | ? {$_} | % {$_ -replace '\s+:\s+', '='}) -join ','`
试试这个:

$var = (([TimeZoneInfo]::Local | select *, @{n='time';e={get-date}}, @{n='hostname';e={$env:COMPUTERNAME}} | out-string).split("`r`n") | ? {$_} | % {$_ -replace '\s+:\s+', '='}) -join ','
$var

到目前为止,我有:$time=dateaddmember-inputobject$tz-membertype noteproperty-name time-value$time$hostname=$env:computername添加成员-inputobject$tz-membertype noteproperty-name主机名-value$hostname非常有效。如何将ps对象从:Get WMIOObject-class win32_bios添加到该对象?我可能在想:$var=([TimeZoneInfo]::Local | select*,@{n='time'e={get date},@{n='hostname';e={$env:COMPUTERNAME},@{n='user';e={$env:username},@{n='bios';e={get wmiObject-class-win32'u-bios}}}out-out-string)。拆分(“
r
n”)?{u124s},{,'但它似乎有一些额外的空格。这取决于您想从bios中提取什么属性
$var=([TimeZoneInfo]::Local | select*,@{n='time';e={get date},@{n='hostname';e={$env:COMPUTERNAME},@{n='user';e={$env:USERNAME},@{n='bios';e={(get wmiObject-class-win32| bios | select serialnumber,manufacturer*).Split('r'n')|?{$|{$|替换'\s+:\s+','='})-join','}}fl*| out string.Split('r'n')|?{$|{$|{$|替换'\s+:\s+','='})-join'
拆分、替换和连接确实对我的对象起了作用。这是我的最终解决方案:
($computer | out string).split(“
r
n”)|?{$}{$}{$}}{$}-replace'\s+:\s+','='})-join'.
我在我的原始帖子中发布了创建对象的完整解决方案,因为注释是有限的字符。
$var = (([TimeZoneInfo]::Local | select *, @{n='time';e={get-date}}, @{n='hostname';e={$env:COMPUTERNAME}} | out-string).split("`r`n") | ? {$_} | % {$_ -replace '\s+:\s+', '='}) -join ','
$var