powershell - 导出到excel的脚本

标签 powershell scripting powershell-2.0 powershell-remoting powershell-3.0

我有以下脚本:--寻求帮助以将输出转换为 Excel 格式

$servers = get-content “c:\list.txt”
foreach ($server in $servers)
{
$server
$command = “quser /server:” + $server
invoke-expression $command 
}

执行时获取以下格式的输出。

server1
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 Vdw231                ica-tcp#8           7  Active          .  11/5/2012 10:40 AM
 Vdw232                ica-tcp#60         16  Active      16:18  11/5/2012 2:22 PM
 Vdw233                ica-tcp#71          3  Active          .  11/6/2012 6:10 AM
 Vdw234                ica-tcp#72          1  Active          3  11/6/2012 6:59 AM
 Vdw235                ica-tcp#73          5  Active          .  11/6/2012 6:59 AM
 Vdw236                rdp-tcp#74          2  Active          .  11/6/2012 7:07 AM
server2
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 Vdw210                ica-tcp#44         14  Active      13:50  11/5/2012 9:03 AM
 Vdw211                ica-tcp#67          6  Active          .  11/6/2012 1:56 AM
 Vdw212                ica-tcp#70          1  Active         45  11/6/2012 6:34 AM
 Vdw213                ica-tcp#72          9  Active         25  11/6/2012 6:53 AM
 Vdw214  
server3
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 Vdw215                rdp-tcp#131         1  Active         19  11/5/2012 1:42 AM
 Vdw216                rdp-tcp#132         4  Active         17  11/5/2012 2:06 AM
 Vdw217                rdp-tcp#143         6  Active          .  11/6/2012 3:31 AM

我的要求是我想将此输出转换为 Excel 格式以提交给管理层。下面是我想从上面的脚本中得到的 Excel 格式...

enter image description here

最佳答案

我已经重写了这个,但我没有测试完整的脚本,它也没有优化。如果您遇到任何 有问题,请随时与我联系。

    $statuses = @()
    $servers = get-content "c:\list.txt"
    $splitter = [regex]"\s+"

    foreach ($server in $servers)
    {
        $command = "quser /server:$server" 
        $lines = @((invoke-expression $command | Out-String) -split "`n")
        #remove header
        $lines = $lines[1..$lines.count]
        foreach ($line in $lines)
        {
            $attrs = @($splitter.Split($line.Trim(),6))
            if ( $attrs -eq 6 )
            {
                $status = New-Object PSCustomObject -Property @{
                "SERVER"=$server;
                "USERNAME"=$attrs[0];
                "SESSIONNAME"=$attrs[1];
                "ID"=$attrs[2];
                "STATE"=$attrs[3];
                "IDLE_TIME"=$attrs[4];
                "LOGON_TIME"=[datetime]$attrs[5]}

                $statuses += $status
            }
        }
    }

    #your filter here
    #$statuses = $statuses | where{ XXXXX }

    $statuses | Export-Csv G:/test.csv -NoTypeInformation

关于powershell - 导出到excel的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13246301/

相关文章:

powershell - 导入 CSV GroupBy Sum

用于查看当前登录用户(域和机器)+状态(事件、空闲、离开)的 Powershell 脚本

bash - $* 在 bash 脚本中意味着什么?

ruby - 使用 Ruby/WinRM 从 Linux 运行 Exchange Powershell 命令

windows - powershell脚本中引用默认路径

powershell - "You cannot call a method on a null-valued expression"在代码中使用参数时

PHP - session_unset 是否取消注册 $_SESSION 变量?

linux - 30 分钟内每 5 分钟检查一次循环中的特定进程

powershell - 检查目录是否存在(如果不执行任何操作)

powershell - Mixlib::ShellOut - Windows 模拟失败