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
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?
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.
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.
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.
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.
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:
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.
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.
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?
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.
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.