Windows cmd.exe output in PowerShell

Posted by noledgeispower on Stack Overflow See other posts from Stack Overflow or by noledgeispower
Published on 2011-03-19T03:35:30Z Indexed on 2011/03/19 8:09 UTC
Read the original article Hit count: 340

Filed under:
|

I have a script for remotely executing commands on other machines, however... when using windows cmd.exe commands It does not write to the file on the remote server. Here is the code.

$server = 'serverName'  
$Username = 'userName'  
$Password = 'passWord'  
$cmd = "cmd /c ipconfig"  

########################  

########################  

$ph = "C:\mPcO.txt"  
$rph = "\\$server\C$\mPcO.txt"  

$cmde = "$cmd > $ph"  
$pass = ConvertTo-SecureString -AsPlainText $Password -Force  
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist  "$Username",$pass  
Invoke-WmiMethod win32_process -name create -ComputerName $server -ArgumentList $cmde  Credential $mycred  
cmd /c net use \\$server\C$ $password /USER:$username  
Get-Content $rph  
Remove-Item $rph  
cmd /c net use \\$server\C$ /delete  

As you can see we simply write

$cmde = "$cmd > $ph"  

if I use a PowerShell command I use

$cmde = "$cmd | Out-File $ph"  

and it works fine. Any advice Appreciated

© Stack Overflow or respective owner

Related posts about powershell

Related posts about cmd.exe