Need help on my first powershell script

This topic contains 5 replies, has 3 voices, and was last updated by  Jatinder Pal Singh 2 months, 1 week ago.

  • Author
    Posts
  • #96054

    Jatinder Pal Singh
    Participant

    Hi Team,

    I am preparing to create my first powershell script. Below are the steps:

    1. Retrieving the C drive space from multiple servers.
    2. Check the server whose C drive is 15% or less.
    3. Run the cleanup script on servers with 15% or less space in C drive
    4. Export the result in excel file
    5. Send an email along with excel file

    Actually I am stuck in between 1st and 2nd command.
    I have retrieved the data using following script:

    $Cdrive=Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $server -Filter "DeviceID='C:'" | Select-Object -Property PSComputerName,DeviceID,VolumeName, @{Name="TotalGB";expression={$_.Size / 1GB -as [int]}}, @{Name="FreeGB";expression={$_.FreeSpace / 1GB -as [int]}}, @{Name="PFree";expression={$_.FreeSpace / $_.Size * 100 -as [int]}}

    So now data is retrieved. I want to fetch only servers whose C drive space is 15% or less and then run the cleanup script.
    Is there any option without using the loop?

  • #96056

    Don Jones
    Keymaster

    As an observation, I think if you made this into a script, versus a giant one-liner, you'd find it easier to write and maintain in the long run.

    That said – what's the problem with using a loop?

  • #96071

    Jatinder Pal Singh
    Participant

    The only concern I have in using a loop is it takes time as it works with one object at a time instead of parallel. If it is possible without loop that will be good otherwise I would just need an overview on how should I fetch only servers with 15% of space then run the cleanup script. Steps 3,4 and 5 are easy for me to work on.

  • #96081

    Don Jones
    Keymaster

    The pipeline is just an implicit loop, though.

  • #96131

    postanote
    Participant

    As for...

    The only concern I have in using a loop is it takes time as it works with one object at a time instead of parallel.

    The fact that you are working with a collection of servers, means you are going to have to loop to hit them all.

    Why not look to PoSH Parallel processing effort?
    Do a search for the below to see the details:

      'PowerShell miiti-threaded'
      'PowerShell Runspace'
      'PowerShell WorkFlows'
      'PowerShell Jovs'

You must be logged in to reply to this topic.