powershell - 在远程机器上使用 Powershell 启动和停止 APP 池

标签 powershell iis-7 powershell-2.0

在提供凭据后,我试图让 PowerShell 在远程计算机上停止和启动 AppPool。

启动应用程序池的函数:

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

最佳答案

要远程运行脚本,您必须确保 PS-远程 已启用。

  • 通过右键单击 Windows PowerShell 快捷方式并选择 以管理员身份启动 Windows PowerShell以管理员身份运行 .
  • WinRM 服务默认配置为手动启动。您必须将启动类型更改为自动并在您要使用的每台计算机上启动该服务。在 PowerShell 提示符下,您可以使用以下命令验证 WinRM 服务是否正在运行:
    获取服务 winrm
    如果服务没有运行,请通过 使其运行启动服务winrm
  • 要配置 Windows PowerShell 进行远程处理,请键入以下命令:

  • Enable-PSRemoting –force


  • 要启用身份验证,您需要将远程计算机添加到 WinRM 中本地计算机的受信任主机列表中。为此,请键入:

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


  • 通过在远程主机上运行以下命令来验证远程主机上的服务是否正在运行并且正在接受请求:

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

    如果您对答案感到满意,请随意喜欢并接受对他人也有帮助的答案。

    关于powershell - 在远程机器上使用 Powershell 启动和停止 APP 池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40869031/

    相关文章:

    powershell - 我们可以使用 Java 代码执行 Windows Azure PowerShell

    linux - 执行脚本后,PowerShell 窗口始终会被清理

    IIS 提供的 Perl CGI 脚本无法启动并出现 HTTP-502 错误,但如果再次尝试则可以正常工作

    .net - IIS 的子域设置指向同一个 Web 应用程序

    asp.net - iis7应用程序asp.net 2.0的并发用户限制

    powershell-2.0 - Powershell 哈希表到 HTML

    azure - 将滚动文件上传到 Azure Blob

    powershell - 如何查看 PowerShell 事件处理程序中生成的错误消息?

    powershell - 在 'user' 上找不到对象 'Domain Controller'

    PowerShell ISE 如何使用 ScriptBlock 关闭自动创建新选项卡?