3

I dont understand what is wrong. If I run this command in command prompt

dir /S/B | findstr "test" > \\server-name\c$\results.txt

It works fine. But If I try running it thru powershell on a remote computer

$result = ([WmiClass]"\\$s\ROOT\CIMV2:Win32_Process").create("cmd /c dir /S/B | findstr ""test"" > \\server-name\c$\results.txt")

I have also tried

$result = ([WmiClass]"\\$s\ROOT\CIMV2:Win32_Process").create("cmd /c dir /S/B | findstr ""test"" > \\192.168.1.100\c$\results.txt")

And I have tried created a log folder and sharing that out with full control for everyone

$result = ([WmiClass]"\\$s\ROOT\CIMV2:Win32_Process").create("cmd /c dir /S/B | findstr ""test"" > \\192.168.1.100\log\results.txt")

None of this seems to work, but if I change it to a local path it works

$result = ([WmiClass]"\\$s\ROOT\CIMV2:Win32_Process").create("cmd /c dir /S/B | findstr ""test"" > C:\results.txt")

Please help, thank you.

Me Myself
  • 129
  • 2
  • 14

3 Answers3

0

You can enable Psremoting on the machine and then use the Invoke-Command cmdlet to run remote command. I think that is more POSH way of doing it.

You can turn it on on many machines in your domain using Group Policy as well: http://blog.powershell.no/2010/03/04/enable-and-configure-windows-powershell-remoting-using-group-policy/

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • I am running this across the entire domain (on every single computer). And I can not turn things on or off. – Me Myself Nov 14 '11 at 19:14
0

In Powershell you should escape the "-character using the backtick ` when used between "".

The following command worked on my system:

([WmiClass]"\\$s\ROOT\CIMV2:Win32_Process").create("cmd /c dir /S/B | findstr `"test`" > \\192.168.1.100\c$\results.txt")
jon Z
  • 15,838
  • 1
  • 33
  • 35
  • Is it possible that you can't write to the root of the C? Can you try `> \\192.168.1.100\c$\windows\temp\results.txt"` – jon Z Nov 15 '11 at 13:21
  • I am running as DA, so I should have permissions. But I tried temp with no luck either. It must be a GPO thing? – Me Myself Nov 15 '11 at 13:24
  • try to run `cmd /c dir /S/B | findstr "test" > \\192.168.1.100\c$\results.txt` from an ordinary cmd box to eliminate that we're dealing with a permissions problem. – jon Z Nov 15 '11 at 13:27
  • I logged into to the remote machine using same account. I then open command line as administrator and I typed the above command. And it worked. See my dilemma and frustration? – Me Myself Nov 15 '11 at 13:33
  • This is probably a double-hop issue. Can you check if it works if `$s = "."` (which is what I used in my tests). – jon Z Nov 15 '11 at 14:46
0

I think all I am going to do is, after the command finishes move the file from the computer to the server.

    if(Test-path \\computer\c$\results.txt){
move-item -path \\computer\c$\results.txt \\server-name\c$ -force | out-host
}
Me Myself
  • 129
  • 2
  • 14