0

Here is what I am trying to get to work in a scriptblock

$scriptblock={
for /f "tokens=14" %i in ('"ipconfig | findstr IPv4"') do set ip=%i

nslookup %ip%
}

Everytime though I get

Exception calling "Create" with "1" argument(s): "At line:4 char:4 + for /f "tokens=14" %i in ('"ipconfig | findstr IPv4"') do set ip=%i + ~ Missing opening '(' after keyword 'for'. " At line:1 char:1 + $scriptblock=[scriptblock]::create($scriptblock) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ParseException

I have tried setting it to a here string first then using [scriptblock]::create(), but still get the same thing.

How do I get this into the scriptblock?

brodoll
  • 1,851
  • 5
  • 22
  • 25
CRad14
  • 3
  • 1

1 Answers1

3

Don't try to mix batch and PowerShell commands. To get the IPv4 address try this:

$ip = (ipconfig | Foreach {if ($_ -match 'IPv4 address.*?:\s+(.*)') {$matches[1]}})[0]

Of if you are on Windows 8 or higher, you can use:

Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Sure, but this command is going to be run on several 2003 servers which may or may not have powershell.... – CRad14 Sep 28 '15 at 17:15
  • @CRad14 if a server doesn't have PowerShell, how do you expect it to recognize a PowerShell construct e.g. `$scriptblock={...}`? – Keith Hill Sep 28 '15 at 17:33
  • I don't, but I will be using Invoke-VMscript to pass the contents of the scriptblock to the OS. – CRad14 Sep 28 '15 at 18:15
  • Can you just use the `-ScriptText` parameter to pass text instead of a scriptblock? – Keith Hill Sep 28 '15 at 22:17