?
Solved

help with the script. not working for remote computers

Posted on 2016-07-18
3
Medium Priority
?
143 Views
1 Endorsement
Last Modified: 2016-08-14
Hi,
I am getting the error "RPC server is unavailable" when running this script below. I have network connectivity and can ping the computers. Please let me know if smth is wrong with the script that it is unable to update service creds on the remote computers.

THank you so much!!


[CmdletBinding()]

$UserName = "testtarget\svc"
$Password = "password"
$Service = "servicename"
$ServerN = "w03,w02,w01"

  [Parameter(Mandatory=$True,Position=5)][int]$SecondsToWait

function PowerShell-PrintErrorCodes ($strReturnCode)
{
#This function will print the right value. The error code list was extracted using the MSDN documentation for the change method as December 2014
Switch ($strReturnCode)
    {
    0{ write-host  "    0 The request was accepted." -foregroundcolor "white" -BackgroundColor "Red" }
    1{ write-host  "    1 The request is not supported." -foregroundcolor "white" -BackgroundColor "Red" }
    2{ write-host  "    2 The user did not have the necessary access."-foregroundcolor "white" -BackgroundColor "Red"}
    3{ write-host  "    3 The service cannot be stopped because other services that are running are dependent on it." -foregroundcolor "white" -BackgroundColor "Red"}
    4{ write-host  "    4 he requested control code is not valid, or it is unacceptable to the service." -foregroundcolor "white" -BackgroundColor "Red"}
    5{ write-host  "    5 The requested control code cannot be sent to the service because the state of the service (Win32_BaseService State property) is equal to 0, 1, or 2." -foregroundcolor "white" -BackgroundColor "Red"}
    6{ write-host  "    6 The service has not been started." -foregroundcolor "white" -BackgroundColor "Red"}
    7{ write-host  "    7 The service did not respond to the start request in a timely fashion." -foregroundcolor "white" -BackgroundColor "Red"}
    8{ write-host  "    8 Unknown failure when starting the service."-foregroundcolor "white" -BackgroundColor "Red" }
    9{ write-host  "    9 The directory path to the service executable file was not found." -foregroundcolor "white" -BackgroundColor "Red"}
    10{ write-host  "    10 The service is already running."-foregroundcolor "white" -BackgroundColor "Red" }
    11{ write-host  "    11 The database to add a new service is locked."-foregroundcolor "white" -BackgroundColor "Red" }
    12{ write-host  "    12 A dependency this service relies on has been removed from the system."-foregroundcolor "white" -BackgroundColor "Red" }
    13{ write-host  "    13 The service failed to find the service needed from a dependent service."-foregroundcolor "white" -BackgroundColor "Red" }
    14{ write-host  "    14 The service has been disabled from the system."-foregroundcolor "white" -BackgroundColor "Red" }
    15{ write-host  "    15 The service does not have the correct authentication to run on the system."-foregroundcolor "white" -BackgroundColor "Red" }
    16{ write-host  "    16 This service is being removed from the system."-foregroundcolor "white" -BackgroundColor "Red" }
    17{ write-host  "    17 The service has no execution thread." -foregroundcolor "white" -BackgroundColor "Red"}
    18{ write-host  "    18 The service has circular dependencies when it starts."-foregroundcolor "white" -BackgroundColor "Red" }
    19{ write-host  "    19 A service is running under the same name."-foregroundcolor "white" -BackgroundColor "Red" }
    20{ write-host  "    20 The service name has invalid characters."-foregroundcolor "white" -BackgroundColor "Red" }
    21{ write-host  "    21 Invalid parameters have been passed to the service."-foregroundcolor "white" -BackgroundColor "Red" }
    22{ write-host  "    22 The account under which this service runs is either invalid or lacks the permissions to run the service."-foregroundcolor "white" -BackgroundColor "Red" }
    23{ write-host  "    23 The service exists in the database of services available from the system."-foregroundcolor "white" -BackgroundColor "Red" }
    24{ write-host  "    24 The service is currently paused in the system."-foregroundcolor "white" -BackgroundColor "Red" }
    }
}



function PowerShell-Wait($seconds)
{
#This function will cause the script to wait n seconds
   [System.Threading.Thread]::Sleep($seconds*1000)
}


function main()
{

#The main code. This function is called at the end of the script, in line 138
$svcD=gwmi win32_service -computername $ServerN -filter "name like '%$Service%'"
write-host "----------------------------------------------------------------"  
write-host "REMEMBER TO RUN THE SCRIPT AS ADMINISTRATOR"  -foregroundcolor "RED" -backgroundcolor "yellow"


write-host "Services found:"  $svcD.Count -foregroundcolor "green"
$svcD | ForEach-Object {

write-host "Service to change user and pasword: "   $_.name -foregroundcolor "green"

write-host "----------------------------------------------------------------"  


       if ($_.state -eq 'Running')
       {
         
           write-host "    Attempting to Stop de service..."
           $Value = $_.StopService()
            if ($Value.ReturnValue -eq '0')

               {
                $Change = 1      
                $Starts = 1      
                write-host "    Service stopped" -foregroundcolor "white" -BackgroundColor "darkgreen"
                }
               Else
               {
                    write-host "    The stop action returned the following error: " -foregroundcolor "white" -BackgroundColor "Red"
                    PowerShell-PrintErrorCodes ($Value.ReturnValue)
                     $Change = 0
                     $Starts = 0
                }
       }
       Else
       {
         write-host "    As the service is not running before, is not going to be started after the change." -foregroundcolor "RED" -backgroundcolor "yellow"
         $Starts = 0
         $Change = 1
       
       }
       
           if ($Change -eq 1 )
           {
             write-host "    Attemtping to change the service..."
               #this is the method that will do the user and pasword change
               $Value = $_.change($null,$null,$null,$null,$null,$null,$UserName,$Password,$null,$null,$null)
               if ($Value.ReturnValue -eq '0')
                {
                   write-host "    Pasword and user changed" -foregroundcolor "white" -BackgroundColor "darkgreen"
                   if ($Starts -eq 1)
                        {
                            write-host "    Attemtping to start the service, waiting $SecondsToWait seconds..."
                            PowerShell-Wait ($SecondsToWait)
                            $Value =  $_.StartService()
                            if ($Value.ReturnValue -eq '0')
                                {
                                    write-host "    Service started sucsesfully" -foregroundcolor "white" -BackgroundColor "darkgreen"
                                }
                             Else
                                {
                                write-host "    Error while starting the service: " -foregroundcolor "white" -BackgroundColor "red"
                                 PowerShell-PrintErrorCodes ($Value.ReturnValue)
                                }
                        }                                                          
                    }
                Else
                 {
                 write-host "    The change action returned the following error: "  -foregroundcolor "white" -BackgroundColor "red"
                  PowerShell-PrintErrorCodes ($Value.ReturnValue)
                 }
                }                      

   write-host "----------------------------------------------------------------"    
}

write-host "PROCESS COMPLETED"  -foregroundcolor "RED" -backgroundcolor "yellow"

}

clear  #clearing  the screen

main   #Calling the main function that will do the job.
1
Comment
Question by:creative555
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 41718129
$serverN is an array and you are not doing   a foreach-object
 function PowerShell-Wait($seconds) can be replaced by
start-sleep -seconds $seconds

Open in new window


[CmdletBinding()]

$UserName = 'testtarget\svc'
$Password = 'password'
$Service = 'servicename'
$ServerN = 'DC01','DC02','WSUS'
$adminuser = 'domain\administrator'
$adminpassword = "p@ssw0rd"
$secpasswd =  ConvertTo-SecureString $adminpassword -AsPlainText -Force
$credendial = New-Object System.Management.Automation.PSCredential ($adminuser, $secpasswd)
function PowerShell-PrintErrorCodes 
{
     param
     (
         [Object]
         $strReturnCode
     )

#This function will print the right value. The error code list was extracted using the MSDN documentation for the change method as December 2014
Switch ($strReturnCode)
    {
    0{ write-host  '    0 The request was accepted.' -foregroundcolor 'white' -BackgroundColor 'Red' }
    1{ write-host  '    1 The request is not supported.' -foregroundcolor 'white' -BackgroundColor 'Red' }
    2{ write-host  '    2 The user did not have the necessary access.'-foregroundcolor 'white' -BackgroundColor 'Red'}
    3{ write-host  '    3 The service cannot be stopped because other services that are running are dependent on it.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    4{ write-host  '    4 he requested control code is not valid, or it is unacceptable to the service.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    5{ write-host  '    5 The requested control code cannot be sent to the service because the state of the service (Win32_BaseService State property) is equal to 0, 1, or 2.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    6{ write-host  '    6 The service has not been started.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    7{ write-host  '    7 The service did not respond to the start request in a timely fashion.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    8{ write-host  '    8 Unknown failure when starting the service.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    9{ write-host  '    9 The directory path to the service executable file was not found.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    10{ write-host  '    10 The service is already running.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    11{ write-host  '    11 The database to add a new service is locked.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    12{ write-host  '    12 A dependency this service relies on has been removed from the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    13{ write-host  '    13 The service failed to find the service needed from a dependent service.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    14{ write-host  '    14 The service has been disabled from the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    15{ write-host  '    15 The service does not have the correct authentication to run on the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    16{ write-host  '    16 This service is being removed from the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    17{ write-host  '    17 The service has no execution thread.' -foregroundcolor 'white' -BackgroundColor 'Red'}
    18{ write-host  '    18 The service has circular dependencies when it starts.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    19{ write-host  '    19 A service is running under the same name.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    20{ write-host  '    20 The service name has invalid characters.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    21{ write-host  '    21 Invalid parameters have been passed to the service.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    22{ write-host  '    22 The account under which this service runs is either invalid or lacks the permissions to run the service.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    23{ write-host  '    23 The service exists in the database of services available from the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    24{ write-host  '    24 The service is currently paused in the system.'-foregroundcolor 'white' -BackgroundColor 'Red' }
    }
}

#Clear-Host  #clearing  the screen
foreach ($server in $serverN){
if ((Test-Connection -ComputerName $Server -Count 1 -Quiet) )
{
$svcD=Get-WmiObject win32_service -computername $Server -filter "name like '%$Service%'" -Credential $credendial
write-host '----------------------------------------------------------------'  
write-host 'REMEMBER TO RUN THE SCRIPT AS ADMINISTRATOR'  -foregroundcolor 'RED' -backgroundcolor 'yellow'


write-host 'Services found:'  $svcD.Count -foregroundcolor 'green'
$svcD | ForEach-Object {

write-host 'Service to change user and pasword: '   $_.name -foregroundcolor 'green'

write-host '----------------------------------------------------------------'  


       if ($_.state -eq 'Running')
       {
         
           write-host '    Attempting to Stop de service...'
           $Value = $_.StopService()
            if ($Value.ReturnValue -eq '0')

               {
                $Change = 1      
                $Starts = 1      
                write-host '    Service stopped' -foregroundcolor 'white' -BackgroundColor 'darkgreen'
                }
               Else
               {
                    write-host '    The stop action returned the following error: ' -foregroundcolor 'white' -BackgroundColor 'Red'
                    PowerShell-PrintErrorCodes ($Value.ReturnValue)
                     $Change = 0
                     $Starts = 0
                }
       }
       Else
       {
         write-host '    As the service is not running before, is not going to be started after the change.' -foregroundcolor 'RED' -backgroundcolor 'yellow'
         $Starts = 0
         $Change = 1
       
       }
       
           if ($Change -eq 1 )
           {
             write-host '    Attemtping to change the service...'
               #this is the method that will do the user and pasword change
               $Value = $_.change($null,$null,$null,$null,$null,$null,$UserName,$Password,$null,$null,$null)
               if ($Value.ReturnValue -eq '0')
                {
                   write-host '    Pasword and user changed' -foregroundcolor 'white' -BackgroundColor 'darkgreen'
                   if ($Starts -eq 1)
                        {
                            write-host "    Attemtping to start the service, waiting $SecondsToWait seconds..."
                            Start-Sleep -seconds $SecondsToWait
                            $Value =  $_.StartService()
                            if ($Value.ReturnValue -eq '0')
                                {
                                    write-host '    Service started sucsesfully' -foregroundcolor 'white' -BackgroundColor 'darkgreen'
                                }
                             Else
                                {
                                write-host '    Error while starting the service: ' -foregroundcolor 'white' -BackgroundColor 'red'
                                 PowerShell-PrintErrorCodes ($Value.ReturnValue)
                                }
                        }                                                          
                    }
                Else
                 {
                 write-host '    The change action returned the following error: '  -foregroundcolor 'white' -BackgroundColor 'red'
                  PowerShell-PrintErrorCodes ($Value.ReturnValue)
                 }
                }                      

   write-host '----------------------------------------------------------------'    
}

write-host 'PROCESS COMPLETED'  -foregroundcolor 'RED' -backgroundcolor 'yellow'
}
else {write-Host ('Server {0} cannot be reached' -f $server)}
}

Open in new window

1
 

Author Comment

by:creative555
ID: 41721473
thank you for explanation!! Makes sense.

I am still getting the error though (:
PS C:\Windows\system32> C:\scripts\Set-ADMSserviceLogonAccountServersv2.ps1
Server server01,server02 cannot be reached


I can ping servers. Tried using FQDN and still the same error.

Also, I want to update more than one service account. I see you added Foreach loop for the server. Can you pls tell me how to do the same for more than one service account.

foreach ($server in $serverN){
if ((Test-Connection -ComputerName $Server -Count 1 -Quiet) )
{
1
 

Author Closing Comment

by:creative555
ID: 41755695
thank you so much!! so sorry for the delay
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

WAP (Web Application Proxy) provides reverse proxy functionality for web applications in the corporate network which allows users on most devices to access internal web applications from external networks.
This article is divided into two sections. 1) First describes how you can connect to server / service / device with Username and secure password in powershell. 2) Second part is to use secure credentials and connect to vCenters which are in Link…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question