一直试图解决这个问题,似乎无法弄明白。
我有以下脚本:
$Servers = Get-Content -Path "C:\Utilities_PowerShell\ServerList.txt"
$IISServiceName1 = 'W3SVC'
$IISServiceName2 = 'IISAdmin'
$IISServiceName3 = 'WAS'
$IISarrService = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3
$IISarrServiceCheck = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 -ErrorAction SilentlyContinue -ErrorVariable NoService
function IISServiceStatus # Checks for status of IIS services
{
param (
$IISServiceName1,
$IISServiceName2,
$IISServiceName3,
$IISarrService,
$IISarrServiceCheck
)
if (Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3)
{
Write-Host "Status of IIS service(s) on $env:ComputerName :"
Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 | Select Name,DisplayName,Status | Format-Table -AutoSize
}
else
{
Write-Host " No IIS service(s) were found..." -foreground "red"
}
}
$Sessions = New-PSSession -ComputerName $Servers
$EndJobs = $Sessions | ForEach-Object {
Invoke-Command -Session $_ -ScriptBlock ${function:IISServiceStatus} -AsJob -ArgumentList $IISServiceName1, $IISServiceName2, $IISServiceName3, $IISarrService, $IISarrServiceCheck | Wait-Job | Receive-Job
Write-Host " "
}
每当我运行它,我得到的只是输出:
Status of IIS service(s) on *PC* :
如果我在loop / invoke-command之外运行该函数,结果绝对是完美的。我的远程循环出了什么问题?
我已经尝试将变量放在函数中,我尝试在没有参数列表的情况下运行invoke-command等。
更新:2016年3月17日
结果......如果我按原样运行我的实际脚本,$EndJobs
的结果很奇怪,因为它在一个表中输出所有服务,然后在另一个表中输出三个IIS服务。这可以解释为什么当我运行我的invoke-command(stopIIS)脚本块时...我不得不重新启动整个服务器,因为它将所有服务都关闭了。
不通过remote / invoke-command运行时,这些函数运行完美。
哎呀... invoke-command严重搞砸了我的东西!
任何人都有任何关于我如何从文本文件在一组服务器上运行我的本地脚本(100%工作)的想法/提示,没有像这样的奇怪问题? invoke-command是唯一的方法吗?
如果你将它全部包装到像这样的脚本块中,你有同样的问题吗?
$Servers = Get-Content 'C:\Utilities_PowerShell\ServerList.txt'
$Sessions = New-PSSession -ComputerName $Servers
$EndJobs = $Sessions | ForEach-Object {
Invoke-Command -Session $_ -ScriptBlock {
$IISServiceName1 = 'W3SVC'
$IISServiceName2 = 'IISAdmin'
$IISServiceName3 = 'WAS'
$IISarrService = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3
$IISarrServiceCheck = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 -ErrorAction SilentlyContinue -ErrorVariable NoService
function IISServiceStatus { # Checks for status of IIS services
param (
$IISServiceName1,
$IISServiceName2,
$IISServiceName3,
$IISarrService,
$IISarrServiceCheck
)
if (Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3) {
Write-Host "Status of IIS service(s) on $env:ComputerName :"
Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 | Select Name,DisplayName,Status | Format-Table -AutoSize
} else {
Write-Host ' No IIS service(s) were found...' -ForegroundColor Red
}
}
IISServiceStatus $IISServiceName1 $IISServiceName2 $IISServiceName3 $IISarrService $IISarrServiceCheck
} -AsJob | Wait-Job | Receive-Job
Write-Host ' '
}
$EndJobs
我有类似的问题。我正在使用credssp来测试第二跳auth,以实现干净地关闭生产环境的自动化。我的脚本有3个部分;会话设置,调用,会话拆解。如果我单独运行每一件,我得到输出。如果我运行整个脚本,我会得到与我单独运行时得到的输出量相匹配的空白行...在我的调用中没有任何花哨的东西(反引号行继续 - 我更喜欢Python的格式化范例比Powershell / C#更好):
Invoke-Command `
-Session $workingSession `
-ScriptBlock {
get-service *spool* -ComputerName server01
}