繁体   English   中英

如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用

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

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

我查看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

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

不确定这是否有帮助,我在您的原始脚本中添加了一些内容并将 $args 更改为 $z 并且它似乎有效。

$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

这个答案与原始海报并非 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”时出现的,所以我认为它在这里很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM