怎么去远程执行一段写好的function


PowerShell交流中心分类: Powershell基础怎么去远程执行一段写好的function
0
MissIsabel asked 7 年 ago

我有一个写好的function和一个serverlist, 想写一个ps1, 在一台机器上远程为serverlist中的机器执行这一段function, 应该怎么写?非常感谢~~

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 7 年 ago
$func = {
 function Say-Hello
 {
 param($Name)
 Write-Host "$Name,您好!"
 }

 Say-Hello -Name 'PSTips.NET' 
}
# 将函数定义转换成字符串


# Server List
#$serverList = Get-Content 'serverlist.txt'
$serverList=@('localhost')
foreach($server in $serverList )
{
 Invoke-Command -ComputerName $server -ScriptBlock $func
}