FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Powershell - remote search index rebuild




At times the windows search gets messed up and to fix this you would need to rebuild the index.
this can be done via the Gui and doesn't take a lot of time.

What I usually do is check the windows event logs and if the symptoms clearly show an issue with the index I can rebuilt it remotely using this function.

Code:


[CmdLetBinding()]
param(   
    [string[]] $ComputerName = $env:COMPUTERNAME
)

Begin {
    # declare the path to the registry key
    [string] $WindowsSearch_Key = `
        'Software\Microsoft\Windows Search'

}

Process{

    $ComputerName | ForEach-Object {

        # Open the Remote HKLM Key
        [Microsoft.Win32.RegistryKey] $ServerKey = `
            [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( `
                [Microsoft.Win32.RegistryHive]::LocalMachine, `
                $_)

        Try{

            # Get th Windows search Registry key
            [Microsoft.Win32.RegistryKey] $regKey = `
                $ServerKey.OpenSubKey($WindowsSearch_Key, $true)
               
            Write-Verbose "Setting 'SetupCompletedSuccessfully' on $_"

            # Set the new value
            $regKey.SetValue('SetupCompletedSuccessfully', 0, `
                [Microsoft.Win32.RegistryValueKind]::DWord);


            Write-Verbose "Restarting Windows Search Service on $_"

            # restart the windows search service
            Restart-Service -InputObject ( `
                Get-Service -ComputerName $_ `
                            -Name wsearch `
            )

             Write-Verbose -Message "Processed $_"   
        }

        # SecurityException is thrown when the user does not
        # have the permissions required to access the registry
        # key in the specified mode
        catch [System.Security.SecurityException]
        {

            [string] $exception = @"
You don't have the required permissions to take this action.
Please run this script as Administrator
"@
            Write-Error -Message $exception

        }

    }

}



This can be executed on several machines at once.


the code works and gets the job done, maybe I'll update it once.


this code was written a while ago and my coding style has changed by now so please don't tear me a new one.



0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.