简体   繁体   中英

Start and Stop APP Pool Using Powershell on Remote Machine

I am trying to get PowerShell to stop and start an AppPool on a remote machine, after providing credentials.

Function To Start Application Pool:

Function fnStartApplicationPool([string]$appPoolName)
{
  import-module WebAdministration
   if((Get-WebAppPoolState $appPoolName).Value -ne 'Started')
   {
      Start-WebAppPool -Name $appPoolName
   }
}

Function To Stop Application Pool:

Function fnStopApplicationPool([string]$appPoolName)
{
  import-module WebAdministration
   if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
   {
      Stop-WebAppPool -Name $appPoolName
   }
}

My code that is not working:

 if ($pathback -eq $false) 
   { 
      #Copying Data from Source to Destination
      copy-Item  -Recurse $backupsrc -Destination $backupdes 
      write-host "Backup Successful" 

      #Validating the apppool value
      import-module WebAdministration 
      if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped') 
      {
        #Stop apppool       
        Stop-WebAppPool -Name $appPoolName 
        write-host "AppPool Stopped Successfully"
      }
      #Copying Data from Source to Destination

      #Start apppool
      Start-WebAppPool -Name $appPoolName 
      write-host "AppPool Started Sucessfully"
      cd c:\  
   } 

For running a script remotely, you have to ensure that PS-Remoting is enabled.

  1. Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator .

  2. The WinRM service is configured for manual startup by default. You must change the startup type to Automatic and start the service on each computer you want to work with. At the PowerShell prompt, you can verify that the WinRM service is running using the following command: get-service winrm If the service is not running, please make it running by Start-Service winrm

  3. To configure Windows PowerShell for remoting, type the following command:

Enable-PSRemoting –force

  1. To enable authentication, you need to add the remote computer to the list of trusted hosts for the local computer in WinRM. To do so, type:

winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'

  1. Verify that the service on the remote host is running and is accepting requests by running the following command on the remote host:

winrm quickconfig

This command analyzes and configures the WinRM service.

In your case, you have to do all these in ServerB because ServerB has to trust ServerA.

After doing these, you can run the below script from ServerA. Certain points I have added in the script itself for your reference. You can change the placeholders according to your requirement.

# Embedding the password in the script.
# If you do not have a domain creds, then use the username and password directly.

$MyDomain='MyDomain' ;
$MyClearTextUsername='Username' ;
$MyClearTextPassword='Password' ;
$MyUsernameDomain=$MyDomain+'\'+$MyClearTextUsername;
$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force ;
$MyCreds=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword ;

# Placing the script under a ScriptBlock
$MyScriptblock={param($appPoolName,$pathback)
# Since you have mentioned that it is working fine locally, I am not checking this part. Assuming its fine.
# Defining the functions as Global. So that you can use it anywhere although I am putting in the scriptblock.
# Make sure the module is present in the remote system. It should be cause you have already mentioned it is working fine when you are running from that system.
        Function fnStartApplicationPool([string]$appPoolName)
                            {
      import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Started')
       {
          Start-WebAppPool -Name $appPoolName
       }
        }
        Function fnStopApplicationPool([string]$appPoolName)
                            {
      import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
       {
          Stop-WebAppPool -Name $appPoolName
       }
        }
                if ($pathback -eq $false) 
                   { 
                      #Copying Data from Source to Destination
                      copy-Item  -Recurse $backupsrc -Destination $backupdes 
                      write-host "Backup Successful" 

                      #Validating the apppool value
                      import-module WebAdministration 
                      if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped') 
                      {
                        #Stop apppool       
                        Stop-WebAppPool -Name $appPoolName 
                        write-host "AppPool Stopped Successfully"
                      }
                      #Copying Data from Source to Destination

                      #Start apppool
                      Start-WebAppPool -Name $appPoolName 
                      write-host "AppPool Started Sucessfully"
                      cd c:\  
                   } 

        }

# As you want to Stop the App pool in Server B from Server A.
# run the script under server A and provide the Server B creds

$result=Invoke-Command -ComputerName 'ServerB' -Credential $MyCreds -ScriptBlock $MyScriptblock -ArgumentList $appPoolName,$pathback ;
$result ;

If you are satisfied with the answer, feel free to like and accept the answer that will help others also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM