Search code examples
powershellpowershell-remoting

Powershell Remote DIR Command Ouput to Remote Server


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.


Solution

  • 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
    }