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

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

问题描述

我的任务是自动化内部流程.此过程涉及首先登录远程服务器 (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 的连接.

我在 CodeProject 上的帖子中使用了一些示例代码 建立所有远程桌面连接,并且它可以正常工作.代码使用 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.

摘自微软网站:

New-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.0Common7IDE"

# 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

一旦您有了脚本,您就可以按照 PowerShell 类.

Once you have a script, you can model you C# application after it following the example from PowerShell Class.

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

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