【问题标题】:How to use powershell.exe with -Command using a scriptblock and parameters如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用
【发布时间】:2017-03-13 13:30:31
【问题描述】:

由于不应该影响当前问题的原因,我需要运行一个脚本,在命令之外,在不同的 PowerShell 实例中使用定义和参数,而不使用 PSSession、后台作业或文件(我有 PSSession 的工作示例,后台作业和 .ps1 文件,我知道它们可以替换我正在尝试做的事情,但我还需要一个带有 powershell.exe -Command 的工作示例。

我查看了help for powershell.exe,它应该支持我正在尝试做的事情,但我无法让它与我需要的所有东西一起工作(脚本定义和命令之外的参数)。

作为一个工作示例,我有:

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

我至少需要能够将-ArgumentList 移动到命令之外,例如:

$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')

最好有:

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

我已经看过以下类似的问题,但找不到我需要的:

【问题讨论】:

  • 你的用例是什么?
  • 删除-WindowStyle Hidden
  • 在我看来您是在 PowerShell 中运行它,那么为什么您认为您需要 powershell.exe -Command 而不是简单地使用 Invoke-Command
  • @Bill_Stewart 我正在测试一个 API,我需要多次执行一个命令,但是由于一些开发人员不想接触的遗留代码,我每个实例只能执行一次命令,因此需要在新实例中执行命令/脚本。
  • @Ansgar Wiechers 感谢您使问题更具可读性。是的,我从 PowerShell 运行它,但是 Invoke-Command 在同一个实例中执行,并且如前所述,由于我无法控制的原因,我需要多次执行一些命令。

标签: powershell parameter-passing


【解决方案1】:

不确定这是否有帮助

$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

【讨论】:

    【解决方案2】:

    这个答案与原始海报不是 100% 相关,因为他们试图从 PowerShell 中运行 PowerShell。我正在尝试从命令提示符或特别是 WMI 运行 PowerShell。一点背景知识:原因我尝试这样做是因为 PowerShell 远程处理在我的目标计算机上启用,我想启用它。我不能使用winrm,因为它需要用户输入。所以,这行得通:

    $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)
    

    结果:

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 2
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ProcessId        : 12508
    ReturnValue      : 0
    PSComputerName   : 
    

    我可能应该在另一个问题中发布这个问题,但是这个问题出现在谷歌搜索“如何将脚本块传递给 powershell.exe”,所以我认为它在这里很有用。

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 2011-06-25
      • 2016-05-04
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多