Author Posts

March 15, 2018 at 2:52 pm

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?

March 15, 2018 at 2:53 pm

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?

March 15, 2018 at 4:13 pm

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.

March 15, 2018 at 6:24 pm

The pipeline is just an implicit loop, though.

March 16, 2018 at 1:15 am

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'

March 17, 2018 at 12:28 pm

Thanks @postanote, these topics are seem to be advanced for me as of now however I will give them try.