Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to check whether the file exists in a remote server or not, i had tried with powershell but no result. can some one help me to work it out in any other way apart from third party website or DLL.

I am using the below code but no fruitful result for me :(

C#
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
           connectionInfo.ComputerName = "ServerName";
           System.Security.SecureString SecPass = new System.Security.SecureString();
           string password = "xxx";

           foreach (char c in password)
           {
               SecPass.AppendChar(c);
           }

           connectionInfo.Credential = new PSCredential(@"abcd", SecPass);
           Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
           runspace.Open();
           using (PowerShell ps = PowerShell.Create())
           {
               ps.Runspace = runspace;
               ps.AddScript("Get-Service");

               // Do something with result ...

               PSCommand new1 = new PSCommand();
               String machinename = "ServerName";
               String file = "D:\\abc.txt";
               //new1.AddCommand("Invoke-Command");
               new1.AddParameter("computername", machinename);
               new1.AddParameter("filepath", file);


               ps.Commands = new1;
               Console.WriteLine(ps.Commands.ToString());
               var results = ps.Invoke();
               //Collection<psobject> results = ps.Invoke();

           }
           runspace.Close();
Posted
Updated 17-Nov-14 23:29pm
v2
Comments
ZurdoDev 18-Nov-14 7:57am    
Why didn't it work?

THe biggest problem you'll have is permissions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900