Powershell将字段和格式添加到单行field = value

时间:2016-02-22 21:43:30

标签: regex powershell formatting

我的Powershell报告需要采取:     [的TimeZoneInfo] ::本地

并添加两个字段:Hostname(或$ env:computername)和日期字段到object / format-list,

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

最终结果: Id =中央标准时间,DisplayName =(UTC-06:00)中部时间(美国和加拿大),StandardName =中央标准时间,DaylightName =中部夏令时 BaseUtcOffset = -06:00:00,SupportsDaylightSavingTime =真,主机名=安卓鲁-PC

用于提取感兴趣数据的各个powershell命令(简单地):

[TimeZoneInfo]::Local
Hostname
$env:computername
date

这样做的最佳方式是什么?

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

$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 ','`

1 个答案:

答案 0 :(得分:0)

试试这个:

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