简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM