Powershell Excel Module

发布时间:2020-06-12 00:09:44 作者:raincity
来源:网络 阅读:1021

参考文档:

https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/25/introducing-the-powershell-excel-module-2/

https://github.com/dfinke/ImportExcel


把Powershell中的数据导出来并能很容易的加工好像一直是一个问题,比如说,导出成CSV格式。

普及一个知识,CVS Comma-Seperated Value 文件。呵呵,我也是刚知道的,以前还纳闷呢。


举个例子,我们以前的做法就是:

Get-Process | Export-Csv c:\Temp\ps.csv #生成CSV文件

Invoke-Item c:\temp\ps.csv  #打开看看


还有一种方法,前提就是那台电脑得安装了EXCEL程序,利用EXCEL COM组件来打开的。如下所示:


$xl = New-Object -ComObject excel.application   #生成一个EXCEL类的实例

$xl.visible = $true  #让这个实例显示出来

$xl.workbooks.add() #添加一个默认的表格


好消息就是现在有一个EXCEL模块来支持了,如果你的电脑的Powershell版本是5.0 可以用以下命令直接安装这个模块,然后就可以直接调用啦,非常强大的功能。


PS C:\> Install-Module importexcel


Untrusted repository

You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy

 value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y



安装成功后,非常惊喜的发现:

Get-Process | Export-Excel c:\temp\psnew.xlsx -show


当然这只是一个开始,更强大的功能还在后面呢。打开一个ISE,把下面的代码放进去,运行看一下结果:

rm $file -ErrorAction Ignore

ps |
    where company |
    select Company,PagedMemorySize,PeakPagedMemorySize |
    Export-Excel $file -Show -AutoSize `
        -IncludePivotTable `
        -IncludePivotChart `
        -ChartType ColumnClustered `
        -PivotRows Company `
        -PivotData @{PagedMemorySize='sum';PeakPagedMemorySize='sum'}

Powershell Excel Module

当然,还有一些更不可思议的执行效果:

$ps = ps

$ps |
    Export-Excel .\testExport1.xlsx  -WorkSheetname memory `
        -IncludePivotTable -PivotRows Company -PivotData PM `
        -IncludePivotChart -ChartType PieExploded3D
$ps |
    Export-Excel .\testExport1.xlsx  -WorkSheetname handles `
        -IncludePivotTable -PivotRows Company -PivotData Handles `
        -IncludePivotChart -ChartType PieExploded3D -Show


Powershell Excel Module

Powershell Excel Module


推荐阅读:
  1. PowreShell场景:自动添加OU用户到指定的组
  2. Powershell如何在Start-Job的Scriptblock里传参?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

powershell ers ce

上一篇:How to Write Go Code

下一篇:PHP学习笔记-----正则表达式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》