Using C#+powershell for file transfer between Windows systems

Source: Internet
Author: User

The new winserver2016 supports a nano-mode, like the previous core mode, can only be remotely managed, only support x64, only 610M, do not let the CentOS mini version alone.

This nano version, by default only WinRM, so only PowerShell, after installation F11 reset password can be used.

The following is a. NET program way to transfer files remotely.

The premise is that the local and remote PowerShell can be connected. In general, Windows does not turn on PowerShell by default, run Enable-psremoting (administrator) on both sides, and add the target server to the trusted host:

Example: Set-item wsman:\localhost\client\trustedhosts-value 192.160.0.100

The ability to reference System.Management.Automation.dll is required. Generally it appears in many places in the system, I from C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__ 31bf3856ad364e35\system.management.automation.dll

Reference.

        static void Main (string[] args)          {           //Create PS Object              var ps = powershell.create ();             PS. AddCommand ("new-pssession");//Get a new Session object             PS. Addargument ("192.168.0.100");//destination server address             var ss = New System.Security.SecureString ();            string pwd = "123123";//Administrator Password             foreach (var ch in pwd)                  SS. Appendchar (CH);//Enter char into the secure string in turn            Pscredential cred = new Pscredential ("Administrator", ss);//Construct a voucher with a secure string              PS. Addparameter ("credential", cred);//Voucher Parameters                          PSSession session = null;             var ret = PS. Invoke ();//execute new-pssession            if (PS. Streams.Error.Count > 0)//error             {                 Console.WriteLine (Ps. Streams.error[0]);                return;           }             ELSE &NBsp;              session = Ret[0]. Baseobject as pssession;                             PS. Commands.clear ();//cleanup command, Reuse PS Object             PS. AddCommand ("CP");//copy-item            PS. Addargument ("c:\\users\\fyter\\ document \ \ Test file. txt");//local file              PS. Addparameter ("Destination", "c:\\");//destination computer hard disk location to which the session is connected              PS. Addparameter ("Tosession", session);//target session             Var result = PS. Invoke ();//execute             if (PS. Streams.Error.Count > 0)                 Console.WriteLine (Ps. Streams.error[0]);       }

  

Using C#+powershell for file transfer between Windows systems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.