DecrepitSoldier
DecrepitSoldier

Reputation: 3

Running a PowerShell script inside a Batch file

I've been trying to integrate a PowerShell script inside a Batch file but I seem to get some errors when trying to do so.

This is my code so far:

for /F "tokens=* delims=," %%A in (C:\MACs.log) do (

@ECHO OFF
PowerShell.exe "& arp -a | select-string "%%A" |% {$_.ToString().Trim().Split(" ")[0]} >> C:\tempfile.log

Ok so, when I run this command in PowerShell everything is working OK but in Batch I get some token errors.

The main post of this code is to create a loop from the MACs.log where I have a couple of MAC addresses that I want the IP addresses to and output them to tempfile.log.

If someone can lend a helping hand it would be greatly appreciated.

Upvotes: 0

Views: 181

Answers (1)

MC ND
MC ND

Reputation: 70923

Why not directly solve it from the batch file?

(for /f %%a in ('
    arp -a ^| findstr /l /i /g:"c:\MACs.log" 
') do @echo %%a) > c:\tempfile.log

The inner pipe command will execute the arp and filter its output using the mac addresses found in MACs.log. This list is processed by the for /f command that will retrieve the first token in the line (default behaviour), that is, the ip address, and echo it. All the output is sent to a tempfile.

Upvotes: 1

Related Questions