[英]Run Powershell with argument using Invoke-Command
I am trying run the power shell script on the remote computer, but its not working. 我正在尝试在远程计算机上运行Power Shell脚本,但是它不起作用。 tried below two option its fails.
尝试以下两个选项失败。
Invoke-Command -Session $Session -FilePath "c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP -Verbose"
Method 2: 方法2:
$script = [scriptblock]::Create("c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP -Verbose")
$result =Invoke-Command -Session $Session -script $script
How can I run this script on remote computer? 如何在远程计算机上运行此脚本?
Thanks SR 谢谢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"
This is the function I normally use to send remote commands to users computers on our domain. 我通常使用此功能向我们域上的用户计算机发送远程命令。 Wrote it so I don't have to remember how to do the Invoke-Command syntax correctly :) My only thought is that i'm not sure if this would work with a script, but it does work with one liners.
编写它,这样我就不必记住如何正确执行Invoke-Command语法了:)我唯一的想法是我不确定这是否可以与脚本一起使用,但它确实可以与一个衬里一起使用。 Up to you if you want to give it a go!
如果您想尝试一下,则取决于您! Good luck.
祝好运。
After removing special character from the script, below code worked. 从脚本中删除特殊字符后,以下代码起作用。
$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.