iterate through servers with drive names

  • $Drives = Get-PSDrive -PSProvider 'FileSystem'

    foreach($Drive in $drives) {

    #select largest file in given directory

    Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |

    Sort-Object Length -Descending |

    #Select-Object -First 10

    select -first 10 name, Length,fullname

    }

    ConvertTo-Html -Property Name,Length,FullName -Head $Header |Out-File E:\ServersL_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).'html'

    Not creating a html file with data.Only empty html file. How can this be fixed.

    And can I iterate throrough a server list (text file source) and execute above code and have all records from all servers in the html output.

    Thanks

     

    • This topic was modified 9 months, 3 weeks ago by  mtz676.
    • This topic was modified 9 months, 3 weeks ago by  mtz676.
    • This topic was modified 9 months, 3 weeks ago by  mtz676.
  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • Your powershell is wrong for that. Your convert to HTML call is happening outside of your loop. You will need your convert to HTML to be inside the loop AND have it append (don't know the proper syntax for that offhand). But what I'd do is rather than selecting the first 10 objects and doing nothing with it, append those 10 objects to a variable then use the convert to HTML on that.

    EVERYTHING happening in the loop is inaccessible outside of the loop UNLESS you store the results in a variable and then it will be accessible outside of the loop.

    But I suspect you will get better support on a Powershell forum rather than a SQL Server forum like SQL Server Central.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Hello,

    I don't know if you have already tried this or not, but I think this will work for you:

    $Drives = Get-PSDrive -PSProvider 'FileSystem'

    foreach($Drive in $drives) {

    #select largest file in given directory

    Get-ChildItem -Path $Drive.Root  -Recurse -File -ErrorAction SilentlyContinue -Force |

    Sort-Object Length -Descending |

    #Select-Object -First 10

    select -first 10 name, Length,fullname | ConvertTo-Html -Property Name,Length,FullName -Head $Header |

    Out-File E:\ServersL_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).'html'

    }

    Somewhat related to a previous reply, the standalone ConvertTo-Html -Property Name,Length,FullName -Head $Header |Out-File E:\ServersL_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).'html' statement is not associated with the foreach loop that is generating the output.  That is why your .html file is empty.  It literally has no data available to convert.

    Additionally, you may want to try just one drive (e.g. C:\) to make sure it works as expected.  If it does, then scale it up and include the other drives.

    I hope this helps.

  • What is the problem you are trying to solve?

    😎

  • Trying to list down the 10 largest files across all drives from all servers.

Viewing 6 posts - 1 through 5 (of 5 total)

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