我正尝试在远程计算机上运行power shell脚本,但它不起作用。尝试了下面两个选项,但失败了。
Invoke-Command -Session $Session -FilePath "c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP -Verbose"方法二:
$script = [scriptblock]::Create("c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP -Verbose")
$result =Invoke-Command -Session $Session -script $script如何在远程计算机上运行此脚本?
感谢SR
发布于 2018-03-06 05:28:11
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语法了:)我唯一的想法是,我不确定这是否适用于脚本,但它确实适用于一行程序。如果你想试一试,那就看你了!祝好运。
发布于 2018-03-07 02:52:00
从脚本中删除特殊字符后,下面的代码正常工作。
$script = [scriptblock]::Create("c:\ConfigureRemotingForAnsible.ps1 -CertValidityDays 3650 -EnableCredSSP -Verbose")
$result =Invoke-Command -Session $Session -script $scripthttps://stackoverflow.com/questions/49119118
复制相似问题