开发者

Windows cmd.exe output in PowerShell

I have a script for remotely executing commands on other machines, however... when using windows cmd.ex开发者_JAVA百科e 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜