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

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


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*5test-connection -Count 2 www.google.comWrite-Output $a1Write-Output $a2}$z=@(8,'abc')$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $z 

$abc

48abcabcabcabcabcPSComputerName                 : okIPV4Address                    :1.1.1.4IPV6Address                    : __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=0Address                        : www.google.comBufferSize                     : 32NoFragmentation                : FalsePrimaryAddressResolutionStatus : 0ProtocolAddress                :1.1.1.4ProtocolAddressResolved        : RecordRoute                    : 0ReplyInconsistency             : FalseReplySize                      : 32ResolveAddressNames            : FalseResponseTime                   : 19ResponseTimeToLive             : 252RouteRecord                    : RouteRecordResolved            : SourceRoute                    : SourceRouteType                : 0StatusCode                     : 0Timeout                        : 4000TimeStampRecord                : TimeStampRecordAddress         : TimeStampRecordAddressResolved : TimestampRoute                 : 0TimeToLive                     : 80TypeofService                  : 0PSComputerName                 : okIPV4Address                    :1.1.1.4IPV6Address                    : __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=0Address                        : www.google.comBufferSize                     : 32NoFragmentation                : FalsePrimaryAddressResolutionStatus : 0ProtocolAddress                :1.1.1.4ProtocolAddressResolved        : RecordRoute                    : 0ReplyInconsistency             : FalseReplySize                      : 32ResolveAddressNames            : FalseResponseTime                   : 21ResponseTimeToLive             : 252RouteRecord                    : RouteRecordResolved            : SourceRoute                    : SourceRouteType                : 0StatusCode                     : 0Timeout                        : 4000TimeStampRecord                : TimeStampRecordAddress         : TimeStampRecordAddressResolved : TimestampRoute                 : 0TimeToLive                     : 80TypeofService                  : 08abc


This answer isn't 100% relevant to the original poster, because they are trying to run PowerShell from within PowerShell. I am trying to run it PowerShell from either a command prompt, or specifically WMI. A little background: the reason I'm trying to do this is because PowerShell remoting is not enabled on my target machine and I want to enable it. I can't use winrm because it requires user input. So, this works:

$x=Get-WmiObject -ComputerName "<computer name>" -Namespace "root\cimv2" -Class "Win32_Process" -List$x.Create('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Enable-PSRemoting}"',$null,$null)

Results:

__GENUS          : 2__CLASS          : __PARAMETERS__SUPERCLASS     : __DYNASTY        : __PARAMETERS__RELPATH        : __PROPERTY_COUNT : 2__DERIVATION     : {}__SERVER         : __NAMESPACE      : __PATH           : ProcessId        : 12508ReturnValue      : 0PSComputerName   : 

I should probably post this in a different question, but this one came up on a google search for "how to pass scriptblock to powershell.exe", so I thought it would be useful here.