【问题标题】:I have powershell command where each content needs to in inserted into cells of aexcel我有powershell命令,其中每个内容都需要插入到aexcel的单元格中
【发布时间】:2021-11-01 21:30:21
【问题描述】:

您好,我是 Powershell 新手,需要将下面的输出插入到 Excel 书的单元格中

systeminfo | findstr /B "Domain"
Get-LocalGroupMember -Group Administrators
Get-WmiObject -Class Win32_Product
Get-TimeZone
get-culture | format-list -property *
Get-NetFirewallProfile | select Name, enabled
get-wmiobject -class win32_logicaldisk


$nicx = Get-NetAdapterStatistics
Foreach ($nic in $nicx) {
  Get-NetAdapterAdvancedProperty -Name $nic.Name -DisplayName "Receive Buffer Size"
  Get-NetAdapterAdvancedProperty -Name $nic.Name -DisplayName "Send Buffer Size"
}

Get-NetAdapterBinding -ComponentID ms_tcpip6

命令的输出会是这样的

PS C:\Users\> systeminfo | findstr /B "Domain"
Domain:                    example.net
PS C:\Users> Get-LocalGroupMember -Group Administrators
ObjectClass Name                             PrincipalSource
----------- ----                             ---------------
User        server\admin         Local          
User        Example-NET\a_pra              ActiveDirectory
Group       Example-NET\Admin_server ActiveDirectory
Group       Example-NET\Domain Admins        ActiveDirectory

PS C:\Users> Get-WmiObject -Class Win32_Product |  findstr -i "Snow Inventory Agent"
Name              : Snow Inventory Agent
Vendor            : Snow Software AB
Caption           : Snow Inventory Agent
Name              : Microsoft Monitoring Agent
Caption           : Microsoft Monitoring Agent

PS C:\Users> Get-TimeZone 
Id                         : Central European Standard Time
DisplayName                : (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb
StandardName               : Central European Standard Time
DaylightName               : Central European Daylight Time
BaseUtcOffset              : 01:00:00
SupportsDaylightSavingTime : True

等所有命令输出都需要格式化为excel。

【问题讨论】:

  • 捕获输出并构建一个[PsCustomObject],您可以在其中将结果设置为属性。然后使用Export-Csv 将信息写入磁盘上的 CSV 文件,您可以打开/导入 Excel
  • 请添加一个您期望 excel 文件外观的示例。这些命令中的大多数都输出具有自己属性的多个对象。以 csv 格式获取所有输出的最简单方法是添加 ` | ConvertTo-Csv -NoTypeInformation | Out-File -Append -Path 'c:\path\file.csv'` 到每个命令的末尾

标签: powershell export-to-excel powershell-3.0


【解决方案1】:

这是一个很好的起点,但如果您希望其格式不同,则必须提供更多信息:

$ExcelFile = 'c:\folder\filename.csv'

systeminfo | findstr /B "Domain" | Out-File -A $ExcelFile
Get-LocalGroupMember -Group Administrators | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
Get-WmiObject -Class Win32_Product | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
Get-TimeZone | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
Get-Culture | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
Get-NetFirewallProfile | select Name, enabled | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
Get-WmiObject -class win32_logicaldisk | ConvertTo-Csv -NoType | Out-File -A $ExcelFile

Foreach ($nic in (Get-NetAdapterStatistics)) {
  Get-NetAdapterAdvancedProperty -Name $nic.Name -DisplayName "Receive Buffer Size" | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
  Get-NetAdapterAdvancedProperty -Name $nic.Name -DisplayName "Send Buffer Size" | ConvertTo-Csv -NoType | Out-File -A $ExcelFile
}

Get-NetAdapterBinding -ComponentID ms_tcpip6 | ConvertTo-Csv -NoType | Out-File -A $ExcelFile

在 excel 中,您仍然需要执行 Data > Text-to-Columns,因为您有多个具有不同字段的表

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多