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?

2
  • What are you trying to do? Getting your hostname? Why do you mix batch statements and PowerShell? Commented Sep 28, 2015 at 13:49
  • It is going to be run on a lot of server 2003 which may or may not have powershell. Commented Sep 28, 2015 at 17:16

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Sure, but this command is going to be run on several 2003 servers which may or may not have powershell....
@CRad14 if a server doesn't have PowerShell, how do you expect it to recognize a PowerShell construct e.g. $scriptblock={...}?
I don't, but I will be using Invoke-VMscript to pass the contents of the scriptblock to the OS.
Can you just use the -ScriptText parameter to pass text instead of a scriptblock?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.