-2

I want to connect to a remote machine with psexec, use findstr to parse a file and save the needed record to a variable to be used later.

Here's c:\dir here\file.xml:

<Editable default="XXX07770004183" description="Station Id" name="MachineID.PSID" regex="^XXX[a-zA-Z0-9]{4}[0-9]{4}[a-zA-Z0-9]{3}$"/>     
<Editable default="17" description="Machine Number" name="MachineID.MachineID" regex="^[0-9]+$"/>
<Editable default="32" description="Asset number" name="MachineID.AssetNumber" regex="^[0-9]+$"/>
<Editable default="AAALLL74" description="Serial number" name="MachineID.SerialNumber" regex="^[a-zA-Z0-9]+$"/>

If I connect to the remote machine manually and run the findstr below, it will report the correct value above: 32.

for /f tokens^=2^ delims^=^" %%a in ('findstr /C:"c:\dir here\file.xml') do set asset=%%a
echo %asset%

I'd like to loop these two together by having psexec connect to the machine, look in c:\dir here\file.xml and find the asset number and save it to a variable. For that matter, I should be able to save any of the fields in c:\dir here\file.xml to a variable.

What I have attempted:

@ECHO Off
SETLOCAL EnableDelayedExpansion
SET listT=list.txt

  FOR /F %%A in ('TYPE "%listT%"') do (
    bin\psexec -u registeredused -p secret \\%%A -s cmd /c findstr /C:"MachineID.AssetNumber" "c:\dir here\file.xml"

    for /f tokens^=2^ delims^=^" %%b in ('findstr /C:"MachineID.AssetNumber" "c:\dir here\file.xml"') do set asset=%%b
    echo %asset%
    )

Result:

>find.cmd

PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com


cmd exited on 172.16.1.41 with error code 1.
ECHO is off.

So it can map a drive but basically fails beyond that.

I apologize for being so vague earlier.

Registered User
  • 255
  • 4
  • 12
  • Why are you calling `findstr` twice? It looks to me like the second `for` should be running the `psexec` command, not the `findstr` command. – Harry Johnston Sep 27 '16 at 22:47
  • Can you show what that would look like? – Registered User Sep 27 '16 at 22:55
  • 1
    Not sure how anyone can help with the lack of details. You are only showing part of your batch file. We have no idea what the format of the machine list file us. Same goes for the XML file you are trying to get information from. – Squashman Sep 28 '16 at 01:29

2 Answers2

0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%A in ('TYPE "%listT%"') do (
 bin\psexec -u %USER_NAME% -p %PASSWORD% \\%%A -s cmd /c findstr /C:"MachineID.AssetNumber" "c:\dir here\file.xml"   
 for /f tokens^=2^ delims^=^" %%b in ('findstr /C:"MachineID.AssetNumber" "c:\dir here\file.xml"') do set asset=%%b
 echo !asset!
)

But since you don't describe what you intend to do, it's a guessing game as to whether this is right.

This code should set asset to the last "MachineID.AssetNumber" in file.xml.

The element %var% refers to the value of var before the for started executing. To see the value as it might change, use SETLOCAL ENABLEDELAYEDEXPANSION and use !var! as explained in thousands of So items, should you search for delayedexpansion.

And in this case, perhaps echo %%b rather than set asset... is what you want - to list all the entries.

halfer
  • 19,824
  • 17
  • 99
  • 186
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • edited above. hope it helps. basic idea: connect to computer via psexec, findstr specific item, save to variable. – Registered User Sep 28 '16 at 04:17
  • @rob : Please refrain from abetting our new self-appointed chief censor in his stalking and roll back his negative work and downvotes in this and https://stackoverflow.com/a/40682546/2128947. – Magoo Jun 20 '18 at 01:08
  • Hi @Magoo: I'm genuinely happy to hear feedback from anyone here, and especially authors whose material I edit. I have not downvoted this post. I downvoted the other one because I felt the rollback to reinstate hostile material was itself hostile, and I do not say that to offend you. In general, where an edit is justified, it is better to either accept it or ping the editor for a discussion. – halfer Jun 20 '18 at 20:27
  • If you like, we can have a philosophical conversation about the tensions between politeness and censorship, but users of this platform have essentially agreed to [Be Nice](https://stackoverflow.com/help/be-nice). I think it would be a pity if ordinary civility was regarded as an attack on free speech, and perhaps if it is cast in that light, you don't agree with that rendering either. There are of course many gradations between these two points, and if you would like to suggest where that line might lie, I shall be most keen to hear your view. – halfer Jun 20 '18 at 20:28
  • (Your profile statement about excessive sensitivity is noted, but I'd invite you to consider that encouragements towards civility are not always related to 'political correctness' culture wars). – halfer Jun 20 '18 at 20:46
  • @rob Your silence speaks volumes. – Magoo Jun 21 '18 at 02:30
  • @Magoo: your attempt to ping Rob failed because he has not edited this post (and thus he will not get a notification). However, I encourage you to engage with community members if you can - much better than reaching out to "power-drunk noobie mods" who you are trying to ignore `;-)`. Bring your sense of humour too. (If my comments bother you terribly, flag them all as "no longer needed"). – halfer Jun 21 '18 at 08:20
0

I decided not to use findstr and resolved this with a nested loop using xml.exe. See more here: windows batch loop not completing

Community
  • 1
  • 1
Registered User
  • 255
  • 4
  • 12