0

I'm trying to be fancy and create an script that other people on my team can use to reset the Config Cache in our SharePoint 2016 farm. This is sometimes necessary if we change anything around our Nintex Workflows or their databases. The settings really like to stick around.

I can get the script to work fine if I have a list of the servers and just use Get-Content and pipe that in to the UNC path where the config files are located. But if I try to keep the script more dynamic and use Get-SPServer, it puts in more information that just the server name.

Here's what I'm trying to run:

$SPServer=Get-SPServer | Where {($_.Role -isnotlike 'Invalid') and ($_.Status -is 'Online')}

Edit: Here's the correct string. Thank you Zack.

$SPServers = Get-SPServer | Where-Object { $_.Role -notlike ‘Invalid’ } | Select-Object -ExpandProperty Address

Then put that into a UNC path to loop through all the servers in the farm:

$selection = read-host

      if ($selection -eq 'TEST')
        {
        Write-Host "TESTING!!!! Clearing the Config Cache on Farm Servers" -ForegroundColor White -BackgroundColor Green
            ForEach ($Server in $SPServer)
              {
                if ( -Not (Test-Path **\\$Server\c$\ProgramData\Microsoft\SharePoint\Config**))
                        {
                        Write-Host "Cannot find folder for "\\$Server\c$\Tools\ClearConfigCache
                        }
                else    {
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Remove-Item -Include '*.xml' -Exclude '*cache*' -Force -Verbose -WhatIf
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Where-Object {$_.Name -eq 'cache.ini'} | Clear-Content -Include 'cache.ini' -Exclude '*.xml' -Force -WhatIf
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Where-Object {$_.Name -eq 'cache.ini'} | Add-Content -Value "1" -Verbose -WhatIf
                        Write-Host "Cache clear and reset complete on" $Server -ForegroundColor White -BackgroundColor Green
                        }
                        {
                        Start-Sleep -Seconds 2
                        }
              }
         }

The Test-Path causes it to fail and returns the path "\SPServer Name=[Whateverserver]\c$\ProgramData\Microsoft\SharePoint\Config

The same variable, though, works fine when I use it to stop the services.

ForEach ($Server in $SPServer)
            {
             $Service=(Get-Service -ComputerName $Server -Name SPTimerV4)
             Start-Service -InputObject $Service -Verbose
             $Service.WaitForStatus('Running','00:00:15')
            }

I don't want to have to use a list for this, there's go to be a way to just input the actual server name for that variable. Below is my full script, in case anyone else has a need for this as well.

Edit: updated the script with the select string from Zack, as well as some changes to the folder filters to not select the Central Admin folder.

Write-Host "                                                                                         

You are here because you need to reset the Config Cache in your SharePoint Farm.         


There are 4 options to choose from.                                                      


You can run a test to make sure the settings in the script are correct and will          
actually do what they're supposed to do and not delete your whole farm.....          
(I highly suggest you do this first)                                                 


You can actually do a Reset, and delete the .xml files that are in the config folders.   


If you forgot something, or something came up, you can Start the timer services back up  
without having to completely exit.                                                   


Or you can always choose to Stop if you are too chicken to pull the trigger.             


" -ForegroundColor White -BackgroundColor Red



$SPServers = Get-SPServer | Where-Object { $_.Role -notlike ‘Invalid’ } | Select-Object -ExpandProperty Address


do{write-host "Type TEST to " -foregroundcolor Black -backgroundcolor White -nonewline; Write-Host "--TEST--" -foregroundcolor Black -backgroundcolor Green -nonewline; Write-Host "resetting the config cache on all the servers in the farm" -foregroundcolor black -backgroundcolor white
   write-host "Type RESET to " -foregroundcolor Red -backgroundcolor White -NoNewline; Write-Host "!! RESET !!" -ForegroundColor White -BackgroundColor Red -NoNewline; Write-Host "the config cache on all the servers in the farm" -foregroundcolor red -backgroundcolor white
   write-host "Type START to start the SharePoint Timer Service on all the servers in the farm" -foregroundcolor black -backgroundcolor white
   write-host "Type STOP if you need to go call your mom." -foregroundcolor Black -backgroundcolor White

    $selection = read-host
      if ($selection -eq 'TEST')
        {
        Write-Host "TESTING!!!! Clearing the Config Cache on Farm Servers" -ForegroundColor White -BackgroundColor Green
            Start-Sleep -Seconds 4
            ForEach ($SPServer in $SPServers)
              {
                if ( -Not (Test-Path \\$SPServer\c$\ProgramData\Microsoft\SharePoint\Config))
                        {
                        Write-Host "Cannot find folder for "\\$SPServer\c$\Tools\ClearConfigCache
                        }
                else    {
                        Get-ChildItem -Path \\$SPServer\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Where-Object {($_.Name -ne 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*') -and ($_.Name -like '*.xml')} | Remove-Item -Force -Verbose -WhatIf
                        Get-ChildItem -Path \\$SPServer\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Where-Object {($_.Name -eq 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*')} | Clear-Content -Force -WhatIf
                        Get-ChildItem -Path \\$SPServer\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Where-Object {($_.Name -eq 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*')} | Add-Content -Value "1" -Verbose -WhatIf
                        Write-Host "Cache clear and reset complete on" $SPServer -ForegroundColor White -BackgroundColor Green
                        }
                        {
                        Start-Sleep -Seconds 2
                        }
              }
         }
    elseif ($selection -eq 'RESET')
        {
        Write-Host "Clearing the Config Cache on Farm Servers" -ForegroundColor Red -BackgroundColor Black
            ForEach ($Server in $SPServers)
                { 
                $Service=(Get-Service -ComputerName $Server -Name 'SPTimerV4')
                Stop-Service -InputObject $Service -Verbose
                $Service.WaitForStatus('Stopped','00:00:15')
                }
            ForEach ($Server in $SPServers)
                {
                Get-Service -InputObject $Service -Verbose | Format-Table MachineName,Status,Name,DisplayName
                }
            Write-Host "You have about 10 seconds if you want to abort this operation. Press Ctrl+C to stop"
            Start-Sleep -Seconds 10

            ForEach ($Server in $SPServers)
              {
                if ( -Not (Test-Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config))
                        {
                        Write-Host "Cannot find folder for "\\$Server\c$\ProgramData\Microsoft\SharePoint\Config
                        }
                else    {
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Select Name, FullName | Where-Object {($_.Name -ne 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*') -and ($_.Name -like '*.xml')} | Remove-Item -Include '*.xml' -Exclude '*cache*' -Force
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Select Name, FullName | Where-Object {($_.Name -eq 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*')} | Clear-Content -Force
                        Get-ChildItem -Path \\$Server\c$\ProgramData\Microsoft\SharePoint\Config\*.* -Recurse | Select Name, FullName | Where-Object {($_.Name -eq 'cache.ini') -and ($_.FullName -notlike '*00000000-0000-0000-0000-000000000000*')} | Add-Content -Value "1" -Verbose
                        Write-Host "Cache clear and reset complete on" $Server -ForegroundColor White -BackgroundColor Green
                        }
                        {
                        Start-Sleep -Seconds 2
                        }
              }
         }
    elseif ($selection -eq 'START')
         {
        ForEach ($Server in $SPServers)
            {
             $Service=(Get-Service -ComputerName $Server -Name 'SPTimerV4')
             Start-Service -InputObject $Service -Verbose
             $Service.WaitForStatus('Running','00:00:15')
            }
        ForEach ($Server in $SPServers)
            {
            Get-Service -InputObject $Service -Verbose | Format-Table MachineName,Status,Name,DisplayName
            }
          }
    elseif ($selection -eq 'STOP')
          {
          ForEach ($Server in $SPServers)
            {  
            $Service=(Get-Service -ComputerName $Server -Name 'SPTimerV4')
            Start-Service $Service -Verbose
            $Service.WaitForStatus('Running','00:00:15')
            }
          ForEach ($Server in $SPServers)
            {
            Get-Service -InputObject $Service -Verbose | Format-Table MachineName,Status,Name,DisplayName
            }
            Write-Host "Fine! Be that way!" -ForegroundColor Black -BackgroundColor White
            Start-Sleep -Seconds 5
          exit
          }
    else
          {
           "Really? You only had 4 choices! How hard was that? You probably need to rethink your life."
           Start-Sleep -Seconds 5
           "Fine....Let's try this again, shall we?"
           Start-Sleep -Seconds 3
          }
  }
Until ($selection -eq 'STOP','RESET')

ForEach ($Server in $SPServers)
{  
$Service=(Get-Service -ComputerName $Server -Name 'SPTimerV4')
Start-Service -InputObject $Service -Verbose
$Service.WaitForStatus('Running','00:00:15')
}
ForEach ($Server in $SPServers)
{
Get-Service -InputObject $Service -Verbose
}
$SPFarmServers.Dispose()
$SPServers.Dispose()
$Server.Dispose()
Write-Host "Thank you for playing, hope it all works out for you!" -foregroundcolor DarkCyan -backgroundcolor Magenta

1 Answer 1

0

The Get cmdlet needs the Select-Object at the end to select the property you need.

Instead of:

$SPServer=Get-SPServer | Where-Object {($_.Role -ne 'Invalid') and ($.Status -eq 'Online')}

Use:

$SPServer=Get-SPServer | Where-Object {($_.Role -ne 'Invalid') -and ($_.Status -eq 'Online')} | Select-Object -ExpandProperty Address

The Select at the end finds the given property (in this case Address), and then expands it so you don't end up with an object with just an Address property. Select-Object can be really helpful when you need a property off a set of objects.

1
  • That worked perfect! I guess I was just brain fried on Friday. Came in today and fixed several other issues in my script above as well. I was trying to do create a text file, get the content, then delete the text file when the script was over, lol. $SPFarmServers = Get-SPServer | Where {($_.Role -notlike 'Invalid') -and ($_.Status -like 'Online')} $SPFarmServers | Select Address | Out-File ".\SPServer.txt" -Force $SPServers = Get-Content .\SPServer.txt
    – GingaNinja
    Commented May 8, 2017 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.