在远程计算机上使用Powershell启动和停止应用程序池

问题描述:

我想让PowerShell在提供凭据后停止并在远程计算机上启动AppPool。在远程计算机上使用Powershell启动和停止应用程序池

函数来启动应用程序池:

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:\ 
    } 
+0

那么当你尝试这个解决方案时发生了什么?你是否收到任何错误信息或脚本是否无法正常工作? –

+0

它的工作完美,但现在我在每台服务器上手动单独运行对于例如: - 我想停止从服务器A.So服务器B中的应用程序池因此,我认为我需要提供一些信誉?如何做到这一点 –

+1

如果您的帐户在两台服务器上都有权限,则不需要传递凭证。只需创建一个接受'$ appPoolName'参数的scriptblock,将你的函数放在它的内部,然后遍历运行[invoke-command]的服务器列表(https://msdn.microsoft.com/en-us/powershell /reference/5.1/microsoft.powershell.core/invoke-command)并传递您的脚本块和应用程序池名称。 –

运行scr ipt远程,您必须确保PS-Remoting已启用。

  1. 启动Windows PowerShell作为通过右键单击Windows PowerShell快捷方式并选择以管理员身份运行的管理员。

  2. WinRM服务默认配置为手动启动。您必须将启动类型更改为“自动”,并在要使用的每台计算机上启动该服务。在PowerShell提示符处,您可以验证WinRM服务使用以下命令运行: GET服务的WinRM 如果服务没有运行,请运行它通过启动服务WinRM的

  3. 要配置Windows PowerShell远程处理,输入以下命令:

启用-PSRemoting -force

  1. 要启用身份验证,您需要将远程计算机添加到WinRM中本地计算机的受信任主机列表中。为了这样做,类型:
  2. WinRM的小号winrm /配置/客户 '@ {TrustedHosts = “RemoteComputer”}'

    1. 验证服务在远程主机正在运行,并通过远程主机上运行下面的命令接受请求:
    2. WinRM的quickconfig

      该命令分析和配置WinRM服务。

      在你的情况,你必须在ServerB中做所有这些,因为ServerB必须信任ServerA。

      做完这些之后,您可以从ServerA运行下面的脚本。我已经在脚本中添加了一些要点供您参考。您可以根据您的要求更改占位符。

      # 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 ; 
      

      如果您满意的答复,欢迎喜欢和接受的答案,这将帮助别人也。

    开始=>
开始=“4>
+1

谢谢!!!很好的解释! –