2016-11-29 170 views
2

我想讓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)並傳遞您的腳本塊和應用程序池名稱。 –

回答

3

運行scr ipt遠程,您必須確保PS-Remoting已啓用。

  1. 啓動Windows PowerShell作爲通過右鍵單擊Windows PowerShell快捷方式並選擇以管理員身份運行的管理員。

  2. WinRM服務默認配置爲手動啓動。您必須將啓動類型更改爲「自動」,並在要使用的每臺計算機上啓動該服務。在PowerShell提示符處,您可以驗證WinRM服務使用以下命令運行: GET服務的WinRM 如果服務沒有運行,請運行它通過啓動服務WinRM的

  3. 要配置Windows PowerShell遠程處理,輸入以下命令:

啓用-PSRemoting -force

  • 要啓用身份驗證,您需要將遠程計算機添加到WinRM中本地計算機的受信任主機列表中。爲了這樣做,類型:
  • WinRM的小號winrm /配置/客戶 '@ {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 ; 
    

    如果您滿意的答覆,歡迎喜歡和接受的答案,這將幫助別人也。

    +1

    謝謝!!!很好的解釋! –

    相關問題