How can I Start-Process powershell.exe with some string splitter?

寵の児 提交于 2020-01-07 03:01:05

问题


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?


回答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



回答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"



回答3:


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


来源:https://stackoverflow.com/questions/39828984/how-can-i-start-process-powershell-exe-with-some-string-splitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!