将两个命令合并为一个并在Powershell中导出到csv文件

时间:2018-11-17 15:16:35

标签: powershell powershell-v2.0 powershell-v3.0

下面是我正在使用的从两个命令的输出中获取数据的代码,然后将它们放入两个单独的数组中。当我组合数组时,输出看起来像我期望的那样,但是当我选择并尝试输出时,它存在间隙并且格式不正确。我该如何将其很好地输出到csv文件?

示例代码:

   $a = get-agentserver
$NewCSV = $a | ForEach-Object {
    New-Object PSObject -Prop @{
        'Client Name' = ($_."Name" -Split '\.(?!\d)')[0]
        'Policy Type' = $_."AgentServerType"
    'Backup State' = $_."BackupStatus"
    'logon' = $_."LogonAccountTestStatus"
    'account' = $_."LogonAccount"
            }
} | Select "Client Name","Policy Type","Backup State","logon","account"
#$NewCSV

$l = foreach($i in $a.name){
get-definition -agentserver $i}
$l | convertto-csv | out-file t1.csv
$m = import-csv t1.csv | select agentserver,name,selectionsummary
$defcsv = $m | foreach-object{
    new-object psobject -prop @{
        'Policy Name' = $_.Name
        'Backup Selection' = $_.selectionsummary
        }
} | select "Policy Name","Backup Selection"
#$defcsv

$hope = $NewCSV + $defcsv
$hope2 = $hope | select  "Client Name","Policy Name","Policy Type","Backup Selection"
$hope2

Ex输出$ hope(对我来说合适)

Client Name  : Name
Policy Type  : Ndmp
Backup State : Unknown
logon        : Succeeded
account      : ndmp_user

Policy Name      : Diff Bakcup 
Backup Selection : COMMON, D: (Partial)

$ hope2的前输出(这使我无法修复)

Client Name                   Policy Name                                     Policy Type Backup Selection
-----------                   -----------                                     ----------- ----------------
Name                                                                              Windows
Name                                                                              Windows
Name                                                                              Windows
Name                                                                              Windows
Name                                                                              Windows
Name                                                                              Ndmp
                              Diff Bakcup - Name       ,...                               COMMON, D: (Partial)
                              ArchiveJob_Backup_to_Tape                                   Name  \e$ (Partial), ...
                              Diff Bakcup - Name       ,...                               COMMON, D: (Partial)
                              ArchiveJob_Backup_to_Tape                                   Name\e$ (Partial), ...
                              Diff Bakcup - Name       ,...                               COMMON, D: (Partial)
                              Name Backup BLR_Pro...                                      /root_vdm/IN-BLR400-FS-C...

我已经清理了代码,并试图将命令输出放入一个变量中,并一遍遍遍它,这看起来要好得多,但是输出结果与我的$ hope2输出中的结果相同。标题“ Policy Name”和“ Backup Selection”之间有很大的差距。有没有办法使用正则表达式仅在Powershell的这两列下删除那些特定的空格?

这是我正在使用的新代码

$agentserver = get-agentserver
$agentserver | convertto-csv | select-object -skip 2 | out-file t2.csv
$agentserver = import-csv t2.csv -Header server,id,type,accountstate,logonaccount
$budefinition = foreach($i in $a.name){
get-backupdefinition -agentserver $i}
$budefinition | convertto-csv | out-file t1.csv
$converted_budef = import-csv t1.csv | select agentserver,name,selectionsummary
$a = $agentserver + $converted_budef
$NewCSV = $a | ForEach-Object {
    New-Object PSObject -Prop @{
        'Client Name' = ($_."server" -Split '\.(?!\d)')[0]
        'Policy Type' = $_."type"
    'Backup State' = $_."BackupStatus"
    'logon' = $_."LogonAccountTestStatus"
    'account' = $_."LogonAccount"
    'Policy Name' = ($_.Name -replace ","," ")
    'Backup Selection' = ($_.selectionsummary -replace ","," ")
                    }
} | Select "Client Name","Policy Name","Policy Type","Backup Selection"
$NewCSV 

我尝试完成的示例如下所示,然后我可以使用export-csv并拥有一个不错的csv文档。

Client Name Policy Name Policy Type Backup Selection
----------- ----------- ----------- ----------------
NAME         Diff Bakup Windows     Common D
NAME         Archive    Ndmp        /root_vdm/

执行$ NewCSV之后| fl我得到了两个单独列表的输出,如下所示,我需要将它们全部放在一个列表中。有什么想法可以在上面的代码中对其进行修复吗?

Client Name      : Name
Policy Name      :
Policy Type      : Ndmp
Backup Selection :

Client Name      :
Policy Name      : Diff Bakcup 
Policy Type      :
Backup Selection : COMMON  D: (Partial)

0 个答案:

没有答案