繁体   English   中英

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

[英]Start and Stop APP Pool Using Powershell on Remote Machine

提供凭据后,我试图使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-Remoting

  1. 右键单击Windows PowerShell快捷方式,然后选择以管理员身份运行”,以管理员身份启动Windows PowerShell。

  2. 默认情况下, WinRM服务配置为手动启动。 您必须将启动类型更改为“自动”,然后在要使用的每台计算机上启动服务。 在PowerShell提示符下,可以使用以下命令验证WinRM服务是否正在运行: get-service winrm如果该服务未运行,请通过Start-Service winrm使其运行。

  3. 要配置Windows PowerShell进行远程处理,请键入以下命令:

Enable-PSRemoting –force

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

winrm的winrm / config / client'@ {TrustedHosts =“ RemoteComputer”}'

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

winrm快速配置

此命令分析和配置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 ;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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