Output formatting and for loop issue

  • I have this powershell code which goes through a list of servernames in a text file and outputs the corresponding values as here.

    CODE:

    $Results = Foreach ($Server in (get-content "C:\serverlist\Server.txt"))

    {

    Get-WmiObject Win32_LogicalDisk | Select DeviceID,VolumeName,Drive,DriveType,Size,FreeSpace | Format-Table

    }

    $Results | Out-File "C:\serverlist\ServerDetails.txt"

    The current code is printing only the output on the server where I am runnig this code.

    So if there 6 servers it is printing the same output 6 times.

    How can I also add the corresponding servername as one more additional column in the output?

    And also I do not want to repeat the column names as in the attached output.

    My result should look like this as below:

    So there should not be an extra line as in the attached file after the column headings.

    Also for each server the column names are repeating here as in the output attached.It should not happen.

    Everything should be in tab delimited format as I will be importing this into a SQL table.

    ServerName DeviceID VolumeName Drive DriveType Size FreeSpace

    ABCDA:

    ABCDC: 3 96528756736 41185206272

    ABCDD:

    ABCDE: Data 3 64421359616 30061256704

    ABCDF: Logs 3 32209104896 615387136

    EDGFA:

    EDGHC: 3 96528756736 41185206272

    EFGHD:

    EFGHE: Data 3 64421359616 30061256704

    EFGHF: Logs 3 32209104896 615387136

    Attached is the current output.

    Thanks

  • you were missing the -computer parameter

    for powershell v3

    Get-WmiObject Win32_LogicalDisk -computer (Get-Content c:\temp\computers.txt)|Select PSComputerName,DeviceID,VolumeName,Drive,DriveType,Size,FreeSpace|Export-Csv -NoTypeInformation -Delimiter "`t" -path c:\temp\alist.txt

    the command above will output the values with double quotes - this means that your import to sql server will need to deal with it through a format file if you use BCP.

    But it may be that you would be better served writing the output directly to sql server - see https://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd for a sample function

    if using the above function you could do something similar to

    $dt = Get-WmiObject Win32_LogicalDisk -computer (Get-Content c:\temp\computers.txt)|Select PSComputerName,DeviceID,VolumeName,Drive,DriveType,Size,FreeSpace|Out-DataTable

    and then use something similar to http://poshcode.org/2276 to write the datatable to your server

    you may need to convert the output to a valid object before the pipe as I haven't tested the second option

  • Only the Servername is changing but it is returning the same data for all the servers:

    $Results = Foreach ($Server in (get-content "C:\serverlist\Server.txt"))

    {Get-WmiObject Win32_LogicalDisk | Select @{Name="Server";Expression={$Server}},DeviceID,VolumeName,Drive,Size,FreeSpace | Format-Table

    }

    $Results | Out-File "C:\serverlist\ServerDetails.txt"

    How do I fix it.

  • sqlnewbie17 (4/21/2016)


    Only the Servername is changing but it is returning the same data for all the servers:

    $Results = Foreach ($Server in (get-content "C:\serverlist\Server.txt"))

    {Get-WmiObject Win32_LogicalDisk | Select @{Name="Server";Expression={$Server}},DeviceID,VolumeName,Drive,Size,FreeSpace | Format-Table

    }

    $Results | Out-File "C:\serverlist\ServerDetails.txt"

    How do I fix it.

    Is it always the data for the last server in the list?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • did either of you bother reading my reply? if you don't supply the server to the cmdlet Get-WmiObject it will only do the current server

  • frederico_fonseca (4/21/2016)


    did either of you bother reading my reply? if you don't supply the server to the cmdlet Get-WmiObject it will only do the current server

    I did bother. I presumed that the original poster would have done so too and his response was replying after trying your solution. That OK?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Gary Varga (4/21/2016)


    frederico_fonseca (4/21/2016)


    did either of you bother reading my reply? if you don't supply the server to the cmdlet Get-WmiObject it will only do the current server

    I did bother. I presumed that the original poster would have done so too and his response was replying after trying your solution. That OK?

    Offcourse. sorry if I was a bit harsh

  • No worries Frederico.

    Does this work sqlnewbie17? If so please mark appropriate post as solution for the rest of us. 🙂

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply