Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在远程服务器上运行脚本文件_C#_.net_Batch File_Scripting_Remote Access - Fatal编程技术网

C# 在远程服务器上运行脚本文件

C# 在远程服务器上运行脚本文件,c#,.net,batch-file,scripting,remote-access,C#,.net,Batch File,Scripting,Remote Access,我的任务是使内部流程自动化。 此过程涉及首先登录到远程服务器(a)。 从服务器A,用户将连接到远程服务器(B) 在服务器B上进行身份验证后,用户需要执行三项主要任务: 更改本地目录中的文件名 打开命令提示符并执行“iisreset” 打开Internet explorer以确保重新建立与IIS的连接 我已经使用了一些示例代码来创建所有远程桌面连接,它可以正常工作。 代码使用的是 我的问题在于上述步骤。 一旦连接到服务器B,我如何实际地执行这些操作。 现在,我们有一个内部脚本来执行这些步骤。 要执

我的任务是使内部流程自动化。 此过程涉及首先登录到远程服务器(a)。 从服务器A,用户将连接到远程服务器(B)

在服务器B上进行身份验证后,用户需要执行三项主要任务:

  • 更改本地目录中的文件名
  • 打开命令提示符并执行“iisreset”
  • 打开Internet explorer以确保重新建立与IIS的连接
  • 我已经使用了一些示例代码来创建所有远程桌面连接,它可以正常工作。 代码使用的是

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

    如果可以建立连接并实际执行脚本,这将是一个解决方案。如果出于某种原因,这是不可能的,我将需要务实地执行这三个步骤作为解决方案


    谢谢您的帮助。

    您可以使用psexec在另一台机器上运行脚本。它随sysinternals套件一起提供
    命令如下所示:
    psexec\\serverB

    您可以使用Powershell

    摘自微软网站:

    New-PSSessioncmdlet可创建Windows PowerShell会话 (PSSession)在本地或远程计算机上。当您创建 PSSession,Windows PowerShell建立到的永久连接 远程计算机

    将以下内容保存到PS1文件:

    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
    
    写入输出“请为提供您的凭据”
    $cred=获取凭证-消息“输入凭证”
    尝试
    {
    $s1=新pssession-computername q-credential$cred-Authentication Credssp
    }
    Catch[系统异常]
    {
    “您的帐户无权访问”
    “不确定服务器上真正需要什么权限”
    “当我有机会时,需要与其他开发人员一起测试”
    }
    #如果您想设置路径,可以这样做
    $env:Path=$env:Path+“C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\Common7\IDE”
    #创建要在远程服务器上执行的命令
    #此块可以包含在命令提示下执行的任何操作
    $block={
    迪尔
    cd“\Foo”
    蝙蝠
    }
    如果有的话(1美元)
    {
    调用命令$block-会话$s1
    删除pssession$s1
    }
    开始睡眠5
    

    一旦有了脚本,您就可以按照中的示例对C#应用程序进行建模。

    谢谢,我将对此进行研究!这需要在特定的时间发生吗?您是否可以在服务器B上运行一个进程来检查它是否需要运行脚本?不,该进程没有时间间隔。因为这是一个产品的支持服务,所以它是即时发生的。嘿,谢谢你的回复。我将研究从C程序运行PS1文件。需求说明程序必须在.NET平台上构建,因为它有一些GUI组件和额外功能。我的脚本只是展示了powershell脚本的外观。然后,要从c可执行文件运行它,您可以执行以下操作: