远程服务器上运行脚本文件 [英] Run script file on remote server

查看:188
本文介绍了远程服务器上运行脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我负责自动化内部流程。 这个过程涉及首先被记录在远程服务器(A)上。 从服务器A,用户将连接到远程服务器(B)。

I'm tasked with automating an internal process. This process involves first being logged on a remote server (A). From server A, a user would connect to Remote Server (B).

在通过身份验证到服务器B,用户需要执行三个主要任务:

Once authenticated onto Server B, the users need to perform three main tasks:

  1. 在本地目录更改文件名。
  2. 开启命令提示符并执行IISRESET
  3. 在打开Internet Explorer,以确保与IIS的连接是重新建立。

我用一些样本code上形成的 $ C $的CProject 让所有的远程桌面连接,它的工作没有问题。 在codeS使用的ActiveX MSTSC库

I've used some sample code form a post on CodeProject to make all the remote desktop connections and it's working without issue. The codes uses the ActiveX MSTSC Library.

我的问题出在上述步骤。 一旦连接到服务器B,我该如何务实地执行这些操作。 眼下,我们确实有一个内部的脚本执行这些步骤。 要执行脚本,用户必须登录到服务器B和手动运行脚本。

My question lies in the steps outlined above. Once connected to Server B, how do I pragmatically perform these operations. Right now, We do have an internal script which performs these steps. To execute the script, the user must log onto Server B and manually run the Script.

如果这是可能的,使连接和执行脚本务实这将是一个解决办法。如果由于某种原因,这是不可能的,我需要务实执行三个步骤中的溶液,而不是

If it's possible to make the connections and to execute the script pragmatically this would be a solution. If for some reason this is not possible, I would need to perform the three steps pragmatically as a solution instead.

感谢你的帮助。

推荐答案

您可以使用PowerShell的新的PSSession

You could use Powershell New-PSSession.

从微软网站摘录:

新的PSSession cmdlet将创建一个Windows PowerShell会话   (PSSession的)本地或远程计算机上。当你创建一个   PSSession时,Windows PowerShell中建立一个持久连接   远程计算机。

The New-PSSession cmdlet creates a Windows PowerShell session (PSSession) on a local or remote computer. When you create a PSSession, Windows PowerShell establishes a persistent connection to the remote computer.

将以下保存到PS1文件:

Save the following to a PS1 file:

Write-Output "Please supply your credential for <insert server name>"
$cred = Get-Credential -Message "Enter credential for <insert server name>"

Try
{
    $s1 = new-pssession -computername <fully qualified domain name (FQDN)> q -credential $cred -Authentication Credssp
}
Catch [system.exception]
{
    "Your account doesn't have access to <insert server name>"
    "Not sure what permission are really require on the server"
    "When I have a chance, need to test with the other developers"
}

# If you wanted to set a path you could do this
$env:Path = $env:Path + ";C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"

# Create the command we want to execute on the remote server
# This block can contain anything you do at command prompt
$block = {
dir
cd "\Foo"
.\Bar.bat
}

if ($s1)
{
    Invoke-Command $block -session $s1
    remove-pssession $s1
}

Start-Sleep 5

一旦你有一个脚本,可以从如下<一个例子,你的模型后,C#应用程序href="https://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.powershell(v=vs.85).aspx"相对=nofollow>的PowerShell类。

这篇关于远程服务器上运行脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆