Author Posts

June 12, 2015 at 12:43 pm

I have two scripts that I created, first one tests to make sure the computers are online (from an AD OU) and IF the computer is online, it writes the computer name to a txt file. The second part of the script takes that txt file and I'm attempting to run a script remotely on all the computers, over 400 of them in all.

So my question is in regard to PSSESSION and how to do this on all computers. I know there is a threshold of 32 PSSESSIONs, so how do I get the script to execure on the other hundreds of computers?

First part of my script: gathers the computers that are in an OU, currently ONLINE and writes them to a text file
**************
$computers = get-adcomputer -Filter {operatingSystem -eq "Windows Embedded Standard"} -SearchBase "OU=OU, DC=DC, DC=LOC"
foreach ($computer in $computers) {
$client = $computer.name

if (Test-Connection -ComputerName $client -BufferSize 16 -Count 1 -Quiet)
{
Write-output $client | out-file c:\temp\online.txt -append
}
else
{
Write-Warning "$client is offline" 3>> c:\temp\offline.txt
}
}

The second part of the script – all I want to do is remove-item (folders and files) and then close the pssession, move onto the next computers
*******************
$pssession = new-pssession -ComputerName (get-content c:\temp\online.txt)
invoke-command -session $pssession -ScriptBlock {
$path1 = "c:\temp\*"
$path2 = "c:\windows\temp\*"
$path3 = "c:\support\*"
$path4 = "c:\windows\system32\ccm\cache\chd00329.8.system"
$path5 = "c:\windows\system32\ccm\cache\chd00329.9.system"

if (test-path -path $path1) {
remove-item -path $path1 -Recurse
}
if (test-path -path $path2) {
remove-item -path $path2 -Recurse
}
if (test-path -path $path3) {
remove-item -path $path3 -Recurse
}
if (test-path -path $path4) {
remove-item -path $path4 -Recurse
}
if (test-path -path $path5) {
remove-item -path $path5 -Recurse
}
}

This seems to work, on 32 computers, but it never goes on to the rest of the computers so I know I'm doing something wrong.

Any help is greatly appreciated

Thanks
j

June 12, 2015 at 11:07 pm

Hi There, This is not really a job for a PS-Session. You could use Invoke-Command to get this done which has a default throttle limit of 32 nodes. This can be altered with the -Throttlelimit Parameter. That is one option. Here is another option i just quickly put together that targets all machines in the domain. Let me know if you have any questions:

$Computers = Get-ADComputer -Filter * | select -ExpandProperty Name
foreach ($computer in $Computers) {

if(Test-Connection -ComputerName $Computer -count 1 -Quiet){
$Files = @{
Files = "c:\temp\*",
"c:\windows\temp\*",
"c:\support\*",
"c:\windows\system32\ccm\cache\chd00329.8.system",
"c:\windows\system32\ccm\cache\chd00329.9.system"
}

$Remove = New-Object -TypeName PsObject -Property $Files

$Remove.Files | Remove-Item -Recurse -Force -ErrorAction 'SilentlyContinue'

Write-Host "Files in $($files.files) have been removed from $Computer" -ForegroundColor Green

}

Else{

Write-Host "$Computer is not online" -ForegroundColor Yellow

}
}

June 15, 2015 at 6:19 am

Thanks for the reply, you are absolutely right, this particular use case shouldn't use pssession. I was attempting to use it incorrectly.

Appreciate the help