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

时间:2018-03-05 20:42:37

标签: powershell

我正在尝试在远程计算机上运行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

2 个答案:

答案 0 :(得分:0)

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语法:)我唯一的想法是,我不确定这是否适用于脚本,但它确实适用于一个内衬。如果你想放手一下,由你决定!祝你好运。

答案 1 :(得分:0)

从脚本中删除特殊字符后,代码工作正常。

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

$result =Invoke-Command  -Session $Session  -script $script
相关问题