使用 [執行指令碼命令] 在遠端主機上執行指令碼Run a script on a remote host by using Run Script command

重要

已不再支援此版本的 Virtual Machine Manager (VMM),建議升級至 VMM 2019This version of Virtual Machine Manager (VMM) has reached the end of support, we recommend you to upgrade to VMM 2019.

System Center Virtual Machine Manager (VMM) 支援使用 [執行指令碼命令] 功能,從遠端在主機上執行命令。System Center Virtual Machine Manager (VMM) supports the ability to execute commands remotely, on a host by using the Run Script command feature. 當您想要從遠端主機啟動服務或收集資訊時,這項功能非常有用。This feature is useful in scenarios where you want to start a service or collect information from a remote host.

您也可以使用新增至 VMM 程式庫的自訂資源在主機上執行指令碼。You can also run the script on the host by using the custom resources that you added to the VMM library. 自訂資源可含有可對伺服器執行特定命令的批次檔,但不一定要使用自訂資源。Custom resources can consist of batch files that can execute specific commands against the server, but the use of custom resources is not required.

範例案例 1 - 啟動服務Example scenario 1 - Start a service

下列範例案例提供有關如何使用 [執行指令碼命令] 功能,在 VMM 主機上啟動服務 sftlist 的資訊。The following example scenario provides information on how you can start a service sftlist on a VMM host using the Run Script command feature.

  1. 在 [VMM 主控台] 的工具列上,按一下 [主機] 並選取 [執行指令碼命令] 。In the VMM console, on the toolbar, click Host and select Run Script Command.

    主機

    [執行指令碼命令] 頁面會隨即顯示:The Run Script Command page appears:

    主機

  2. 舉例來說,對顯示的選項鍵入下列詳細資料:As an example, type the following details against the options displayed:

    • 部署順序:從下拉式功能表中選取部署順序。Deployment Order: Select the deployment order from the drop-down menu.

    • [可執行程式] :cmd.exeExecutable program: cmd.exe

    • [參數] :/q /c net start sftlistParameters: /q /c net start sftlist

    • 指令碼資源套件執行身分帳戶:無Script resource package and Run As account: None

      /q 會關閉命令列的回應,而 /c 則會執行命令並終止。The /q turns off the echo for the command line and /c carries out the command and then terminates. 如果您未使用這些參數,則會從 cmd.exe 處理序得到傳回碼為 0 的結果,且不會顯示作業中命令的實際結果。If you do not use these switches, the outcome will be a return code 0 from the cmd.exe process and will not display the actual result of the command in the job.

      執行指令碼表單

  3. 選取此命令的 [逾時] 秒數。Select the Timeout seconds for this command.

  4. 按一下 [進階..] Click Advanced.. 並為產生的任何錯誤指定輸出檔案與記錄檔位置。and specify the output file and log file location for any errors.

  5. 按一下 [完成] 。Click Finish.

在 VMM 主控台中監視作業的結果。Monitor the job in the VMM console for the result. 如果發生任何錯誤,請瀏覽錯誤檔記錄,並遵循適用的建議。In case of any error, go through the error file log and follow the recommendations as applicable.

以下是錯誤訊息的範例Here is a sample error message

錯誤訊息

錯誤說明

範例案例 2 - 使用自訂資源啟動服務Example scenario 2 - Start a service using custom resources

在此範例中,我們會在步驟 2 使用下列值,其餘步驟則維持不變。In this example, at step 2, we use the following values, rest of the steps remain the same.

  • 部署順序:從下拉式功能表中選取部署順序。Deployment Order: Select the deployment order from the drop-down menu.

  • [可執行程式] :cmd.exeExecutable program: cmd.exe

  • [參數] :/q /c services.cmdParameters: /q /c services.cmd

  • 指令碼資源封裝:SAV_x64_en-US-4.9.305.198.cr(4.9.305.198)Script resource package: SAV_x64_en-US-4.9.305.198.cr(4.9.305.198)

  • 執行身分帳戶:系統管理員Run As account: Admin

    在此範例中,我們在名為 SAV_x64_en-US-4.9.305.198.cr(4.9.305.198) 的自訂資源資料夾中包含了名為 services.cmd 的批次檔,並指定管理員為 [執行身分帳戶] 。For this example, we included a batch file named services.cmd under a custom resource folder named SAV_x64_en-US-4.9.305.198.cr(4.9.305.198), and specified the Run As account as Admin.

    執行指令碼自訂表單

    批次檔會執行 net stop sftlist/y,再執行 net start sftlist。The batch file performs a net stop sftlist/y and then a net start sftlist. 在此案例中,會將自訂資源資料夾傳輸到代理程式主機,並複製到 windows\temp 中。然後建立格式為 scvmm.xxxxxxxx 的資料夾包含所有檔案。In this scenario, the custom resource folder is transferred to the agent host and copied under windows\temp. A folder with the format, scvmm.xxxxxxxx, is created to contain all the files. 在這之後,該資料夾會執行批次檔,然後代理程式會將對應的結果傳回 VMM,並顯示在作業中。From here, it executes the batch file and the agent returns the corresponding outcome to VMM and displays it in the job. 如果指令碼產生錯誤,其會在指定位置下建立記錄檔。If the script generates an error, it creates a log under the specified location.