How to use powershell.exe with -Command using a scriptblock and parameters

ぃ、小莉子 提交于 2019-12-10 01:23:30

问题


For reasons that should not impact the current question I need to run a script, with the definition and parameters outside the command, inside a different PowerShell instance, without using PSSessions, background jobs or files (I have working examples for PSSession, background jobs and .ps1 files and I'm aware they can replace what I'm trying to do, but I need a working example with powershell.exe -Command as well).

I looked at the help for powershell.exe, and it should support what I am trying to do, but I can't get it working with all that I need (script definition and parameters outside the command).

As a working example I have:

$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
    param($a1,$a2)
    $a1*6
    $a2*5} -Argumentlist @(8,'abc')}

I need to be able to at least move the -ArgumentList outside the command, like:

$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
param($a1,$a2)
$a1*6
$a2*5} -Argumentlist @($args[0],$args[1])} -args @(8,'abc')

and even better have:

$script={
param($a1,$a2)
$a1*6
$a2*5}
$args=@(8,'abc')
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $args

I already looked at the following similar questions, but couldn't find what I needed:

  • How to run a Powershell script from the command line and pass a directory as a parameter
  • How can I Start-Process powershell.exe with some string splitter?

回答1:


Not sure if this helps I added a few things to your original script and changed $args to $z and it seemed to work.

$script={
param($a1 =1 ,$a2 = 2)
$a1*6
$a2*5
test-connection -Count 2 www.google.com
Write-Output $a1
Write-Output $a2
}
$z=@(8,'abc')
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $z 

$abc

48
abcabcabcabcabc

PSComputerName                 : ok
IPV4Address                    :1.1.1.4
IPV6Address                    : 
__GENUS                        : 2
__CLASS                        : Win32_PingStatus
__SUPERCLASS                   : 
__DYNASTY                      : Win32_PingStatus
__RELPATH                      : Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRou
                                 te="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
__PROPERTY_COUNT               : 24
__DERIVATION                   : {}
__SERVER                       : ok
__NAMESPACE                    : root\cimv2
__PATH                         : \\ok\root\cimv2:Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressName
                                 s=FALSE,SourceRoute="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
Address                        : www.google.com
BufferSize                     : 32
NoFragmentation                : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress                :1.1.1.4
ProtocolAddressResolved        : 
RecordRoute                    : 0
ReplyInconsistency             : False
ReplySize                      : 32
ResolveAddressNames            : False
ResponseTime                   : 19
ResponseTimeToLive             : 252
RouteRecord                    : 
RouteRecordResolved            : 
SourceRoute                    : 
SourceRouteType                : 0
StatusCode                     : 0
Timeout                        : 4000
TimeStampRecord                : 
TimeStampRecordAddress         : 
TimeStampRecordAddressResolved : 
TimestampRoute                 : 0
TimeToLive                     : 80
TypeofService                  : 0


PSComputerName                 : ok
IPV4Address                    :1.1.1.4
IPV6Address                    : 
__GENUS                        : 2
__CLASS                        : Win32_PingStatus
__SUPERCLASS                   : 
__DYNASTY                      : Win32_PingStatus
__RELPATH                      : Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRou
                                 te="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
__PROPERTY_COUNT               : 24
__DERIVATION                   : {}
__SERVER                       : ok
__NAMESPACE                    : root\cimv2
__PATH                         : \\ok\root\cimv2:Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressName
                                 s=FALSE,SourceRoute="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
Address                        : www.google.com
BufferSize                     : 32
NoFragmentation                : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress                :1.1.1.4
ProtocolAddressResolved        : 
RecordRoute                    : 0
ReplyInconsistency             : False
ReplySize                      : 32
ResolveAddressNames            : False
ResponseTime                   : 21
ResponseTimeToLive             : 252
RouteRecord                    : 
RouteRecordResolved            : 
SourceRoute                    : 
SourceRouteType                : 0
StatusCode                     : 0
Timeout                        : 4000
TimeStampRecord                : 
TimeStampRecordAddress         : 
TimeStampRecordAddressResolved : 
TimestampRoute                 : 0
TimeToLive                     : 80
TypeofService                  : 0

8
abc


来源:https://stackoverflow.com/questions/40342632/how-to-use-powershell-exe-with-command-using-a-scriptblock-and-parameters

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