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

后端 未结 2 1703
半阙折子戏
半阙折子戏 2021-02-19 18:18

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, w

2条回答
  •  春和景丽
    2021-02-19 18:50

    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
    

提交回复
热议问题