繁体   English   中英

使用Invoke-Command使用参数运行Powershell

[英]Run Powershell with argument using Invoke-Command

我正在尝试在远程计算机上运行Power Shell脚本,但是它不起作用。 尝试以下两个选项失败。

Invoke-Command  -Session $Session -FilePath "c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP  -Verbose"

方法2:

$script = [scriptblock]::Create("c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP  -Verbose")

$result =Invoke-Command  -Session $Session  -script $script

如何在远程计算机上运行此脚本?

谢谢SR

function Send-RemoteCommand
{
    [CmdletBinding()]
    Param
    (
    [Parameter(Position = 0, Mandatory = $true,ValueFromPipeline=$True,HelpMessage="Computer Name to run command on.")]
    [String]$ComputerName,
    [Parameter(Position = 1, Mandatory = $true,ValueFromPipeline=$false,HelpMessage="Command to be ran on remote computer.")]
    [String]$Command
    )

    Invoke-Command -ComputerName $ComputerName -ScriptBlock {param($Arg) Invoke-Expression -Command $Arg} -ArgumentList "$Command"
}

Send-RemoteCommand -ComputerName "ComputerNameHere" -Command "YourPowerShellCommandsHere"

我通常使用此功能向我们域上的用户计算机发送远程命令。 编写它,这样我就不必记住如何正确执行Invoke-Command语法了:)我唯一的想法是我不确定这是否可以与脚本一起使用,但它确实可以与一个衬里一起使用。 如果您想尝试一下,则取决于您! 祝好运。

从脚本中删除特殊字符后,以下代码起作用。

$script = [scriptblock]::Create("c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP  -Verbose")

$result =Invoke-Command  -Session $Session  -script $script

暂无
暂无

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

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