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



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

并将两个字段:主机名(或$env:计算机名)和日期字段添加到对象/格式列表中,

然后将所有"\t:\t"(

制表符之间的冒号)替换为"=",将所有""(换行符)替换为","

对于最终结果:id=中部标准时间,显示名称=(UTC-06:00)中部时间(美国和加拿大),标准名称=中部标准时间,夏令时间名称=中部夏令时BaseUtcOffset=-06:00:00,支持夏令时=True,主机名=ANDREW-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-Zd-]+"
$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

最新更新