0

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

1 Answer 1

5

Why are you doing it the hard way? You can use WMI to get the IP details of a remote computer.

Get-WMIObject -ComputerName "RemoteServer" Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$true" | Out-File $env:TEMP\ipdetails.txt

Now, once you have that file, you can move it using the commands you had in your script.

Sign up to request clarification or add additional context in comments.

5 Comments

Im not worried about the IP, thats just an example I gave for a non powershell command.
Hmm.that should have been clear. Anyway, the script works fine for me except the missing '-' in Invoke-WMIMethod line. -Credential is missing '-'. Other than that, the script was able to create a file on the remote machine with ($cmd > $ph) and I was able to read it using Get-Content $rph.
BTW, you don't have to map network drive to a remote conputer. Get-Item supports UNC paths.
For that matter, the OP doesn't need the cmd /c on cmd /c net use .... net use ... works just fine from PowerShell.
Thanks guys, also, strangly this does work on one setup, executing from XP to win7, however when executing from a win7 machine back to XP it does not. It is also not working from 7 to 2k3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.