powershell脚本的重复输出

时间:2015-12-09 12:03:06

标签: powershell

以下脚本给出了重复的结果,请您告诉我这个脚本中的错误在哪里? 在输出中,如果您看到myserver2有重复的条目。

Get-Content "C:\test\computers.txt" | ForEach-Object { $_.TrimEnd() } | ForEach-Object {
    $value += Invoke-Command -Computer $_ -ScriptBlock {
        Param($computer)

        # Connect to SQL and query data, extract data to SQL Adapter
        $SqlQuery = "xp_fixeddrives"
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Data Source=$computer;Initial Catalog='Secaudit';Integrated Security = True";
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($SqlQuery, $Sqlconnection)

        $mdtable = New-Object System.Data.Dataset
        $nRecs = $SqlAdapter.Fill($mdtable)
        $nRecs | Out-Null
        $mdtable.Tables[0]

        for ($i=0; $i -lt $mdtable.tables.count; $i++) {
            $res = $mdtable.Tables[$i]
        }

        $res
    } -ArgumentList $_ -Credential $cred
}

$value

输出(我更改了服务器名称):

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : C
MB free            : 77659

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : E
MB free            : 143104

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : F
MB free            : 470680

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : G
MB free            : 34955

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : C
MB free            : 77659

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : E
MB free            : 143104

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : F
MB free            : 470680

PSComputerName     : myserver1
RunspaceId         : a1bd8b88-21b4-4b9c-ba1c-1e189b69fdfe
PSShowComputerName : True
drive              : G
MB free            : 34955

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : C
MB free            : 11393

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : G
MB free            : 16456

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : H
MB free            : 397295

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : I
MB free            : 389523

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : J
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : K
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : L
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : M
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : N
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : O
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : P
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : Q
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : R
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : S
MB free            : 1225745

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : T
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : X
MB free            : 184221

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : C
MB free            : 11393

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : G
MB free            : 16456

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : H
MB free            : 397295

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : I
MB free            : 389523

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : J
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : K
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : L
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : M
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : N
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : O
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : P
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : Q
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : R
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : S
MB free            : 1225745

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : T
MB free            : 409494

PSComputerName     : myserver2
RunspaceId         : 0575a3e3-37f7-4db9-ac9e-70e7ef5501b7
PSShowComputerName : True
drive              : X
MB free            : 184221

2 个答案:

答案 0 :(得分:2)

$mdtable.Tables[0]

已添加到$value,然后$res,在循环的第一个周期也是$mdtable.Tables[0]

答案 1 :(得分:-2)

为了确保仅使用此信息填充$ value: 改变$ value + = 在$ value = 如果你在此之后获得双打,请检查双值上的sql-query