Query AD for folder size

This topic contains 14 replies, has 2 voices, and was last updated by  cm 3 months, 2 weeks ago.

  • Author
    Posts
  • #77842

    cm
    Participant

    Want to do an AD Query and search all servers within an OU for C:\Windows\Temp directory and if the content within is greater than/equal to 500MB, list it out. Ideally be put in a button that will spit it out via RichTextBox

    Hope I said it right.

    Thank you

  • #77848

    Don Jones
    Keymaster

    Well, this forum isn't really about writing a script for you, but we're happy to try and answer questions! Did you have a specific question you wanted to start with?

  • #77856

    cm
    Participant

    Sure, I understand.

    Basically I write [sample]

    $Server = Get-ADComputer Filter 'nam -like __*"' -Search Base "OU= DC=" -SearchScope Subtree -Properties Name | Sort NAME
    $folder = 'c:\Windows\Temp'

    if (Test-Path $folder) {

    }

    ...

    I assume where I'd start is the $folder as it points locally. Once done the info can be written to a RichTextBox (this will be wrapped in a button).

    EDIT: Btw I bought your book Powershell in a month of lunches and it really helped.

  • #77859

    Don Jones
    Keymaster

    Well, it depends on how you're going to connect to the machines that you want to check.

    Will you be using PowerShell Remoting?

    Or will you be just using file services to connect?

    You probably also want to do a "Select-Object -Expand Name" to turn the Name property into a plain string. As-is, your $server variable will contain objects that have been sorted by name. Do the Select-Object after your existing Sort-Object.

    You're also likely to get back more than one server. I would probably store them in $servers, not $server, and then:

    ForEach ($server in $servers) {
    
    }
    

    Within that loop, $server will represent one item. If you did the Select-Object as I suggested, then $server would contain a single computer name, which will be pretty easy to work with.

    • #77872

      cm
      Participant

      You'll have to excuse me; I am coming from years of VBS – Yes I would like to search and list multiple computers.

      I know in VBS I could do the following:

      SetObjFolder = objFSO.GetFolder ("\\" & strComputer & "\c$\Windows\Temp") but I'm not sure the PS equivalent.

  • #77881

    Don Jones
    Keymaster

    You could do exactly that if you wanted to. Or, just use Get-ChildItem to retrieve a folder. Add -recurse for subdirectories, -Directory to only get directories.

    • #77926

      cm
      Participant

      Don,

      What am I doing wrong? Nothing is happening. I also want it to display in a text box.

      $Servers = Get-ADComputer -Filter 'name -like "*___*"' -SearchBase "OU=[redacted],OU=[redacted],OU=[redacted],OU=[redacted],DC=[redacted],DC=[redacted],DC=[redacted]" -SearchScope Subtree -Properties Name | Select-object -Expand Name

      For each ($server in $servers)
      {
      Get-ChildItem -Path C:\Windows\Temp | Where-Object { $_.length/1MB -gt 500 } | select fullname @{ n = "Size MB"; e = { $_.length/1MB} }
      }
      }

  • #77941

    Don Jones
    Keymaster

    Notice in your old VBScript how you constructed a UNC that included the server name and the C$ share? You've not done that. You're simply retrieving a local folder. You're not using your $server variable at all.

    Also, a text box is VASTLY more complex. That's not something you can just do in a line or two. If Out-Gridview is acceptable, use that, as it's the only native GUI function.

    • #78293

      cm
      Participant

      Ok Don, I've finally had some time to work on it.

      Code

      Nothing gets outputed. Do I need an if then statement? Right now I just care if it outputs at all, even if its in an Output-GridView. I'll worry about the richtextbox later.

  • #78298

    Don Jones
    Keymaster

    You probably need to write the path in double quotes:

    "\\$server\c`$\Windows\Temp"
    

    Also notice the back-tick used to escape the second dollar sign, so that it doesn't get treated as a variable.

    As a test, eliminate your Where-Object statement – just let Get-ChildItem run, and make sure you get results back from that. I would also consider adding to the top of the script:

    $VerbosePreference = "Continue"
    

    And then just before your Get-ChildItem:

    Write-Verbose "Attempting \\$server\c`$\Windows\Temp"
    

    That will enable you to visually verify the path being attempted.

    • #78307

      cm
      Participant

      Don,

      I appreciate all the help. I've done exactly that yet nothing happens, not even a confirmation it's running. I guess I'll have to do my research on other ways this can be done.

  • #78308

    Don Jones
    Keymaster

    If you did all that and got no output, it's because $servers didn't contain anything. So, just after you populate $servers, do this:

    Write-Verbose "Server list: $servers"
    

    And see if you get anything. If you don't, then you know your problem is in that AD query. This is kind of my process for debugging – I have an assumption about what's in each variable, but I need to validate that assumption. Verbose output is one of many ways to do that.

  • #78379

    cm
    Participant

    Don,

    I seem to be getting further, but have hit a roadblock with the following code – I changed it up to invoke-command:

    Code v2

    What do I do for the path, when it tells me 'path cannot be found'? I can see it's looking for that exact path which doesn't exist, but how do I tell it to search for the folder remotely?

  • #78380

    Don Jones
    Keymaster

    Ah, OK. Heh.

    Invoke-Command sends the command TO THE REMOTE COMPUTER, so the command runs locally there. So you have to ask yourself – if you were ON the remote computer, what would "servers$" mean? Probably nothing. "$servers" wouldn't mean anything either, because $servers is a variable on YOUR machine, not on all of the remote ones.

    You want to switch back to a local path – e.g., "C:\Windows\Temp" – and each server will look locally in that location.

    • #78385

      cm
      Participant

      It still isn't showing any computers but it seems to be my searchbase, I'll keep working on it. Probably need to try one computer at a time and go from there.

You must be logged in to reply to this topic.