0

I want to start powershell.exe with a scriptblock like this (it's working fine):

Start-Process powershell.exe {
  Get-Help 
  Get-Process
}

But this script doesn't work:

Start-Process powershell.exe {
  $string = "123xAbcxEFG"
  $split1,$split2,$split3 = $string.Split("x")
  Write-Output $split1
  Write-Output $split2
  Write-Output $split3
  sleep 10
}

I think I need Add-Type -AssemblyName "SomeNameSpace", but how can I find that namespace? Any intellisense or something like this?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
saftargholi
  • 896
  • 1
  • 9
  • 25

3 Answers3

2

The problem is with the quotes. It works, if you e.g. put additional single quotes around your double quotes. It also works with triple double quotes.

Start-Process powershell.exe  {
$string = """123xAbcxEFG"""
$split1,$split2,$split3 = $string.split("""x""")
Write-Output $split1
Write-Output $split2
Write-Output $split3
sleep 10
}

How I changed your code to catch the error (without the additional double quotes):

$ScriptBlock = {
  $string = "123xAbcxEFG"
  $split1,$split2,$split3 = $string.split("x")
  Write-Output $split1
  Write-Output $split2
  Write-Output $split3
  sleep 10
}

Start-Process powershell -argumentlist "-noexit -command $ScriptBlock"
A189198
  • 396
  • 2
  • 7
  • 1
    I changed my answer, because there are several ways. I also updated how I found the problem. – A189198 Oct 03 '16 at 10:27
  • At line:3 char:42 + $split1,$split2,$split3 = $string.split(x) +~ Missing ')' in method call. At line:3 char:42 + $split1,$split2,$split3 = $string.split(x) + ~ Unexpected token 'x' in expression or statement. At line:3 char:43 + $split1,$split2,$split3 = $string.split(x) + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall – saftargholi Oct 03 '16 at 10:28
  • 1
    see my changed answer with the triple double quotes. – A189198 Oct 03 '16 at 10:29
  • 1
    yes, because this error gives you a hint that the quotes are not working as you did expect. – A189198 Oct 03 '16 at 10:29
1

you can use start-job it's actually start new powershell process . and you can do is easy. like this :

start-job 

for more help use :

get-help start-job
0

you can use start-job it's actually start new powershell process . and you can do is easy. like this :

$script= {get-help "dir"}
start-job -scriptblock $script