运行 交互式 'master' 本地 PC 以及远程 PC 和日志上的 PowerShell 脚本
Run interactive 'master' PowerShell script on local PC as well as remote PCs and log
我编写了一个大型(983 行)交互式 PowerShell 脚本来管理本地服务器的几乎所有方面。我有一个包含服务器列表的文本文件。我想为之疯狂。
文本文件内容:
Server1Name
Server2Name
PowerShell 交互式菜单:
Write-Host "Welcome to the environment control script."
Write-Host " "
Write-Host "What would you like to do? Select the number:"
Write-Host "========================================================================="
Write-Host "1. Create a backup of the environment (existing servers)"
Write-Host "2. Backup environment and deploy updated environment (existing servers)"
Write-Host "3. Deploy new environment including IIS (clean servers)"
Write-Host "4. Deploy just IIS (clean servers)"
Write-Host "5. Remove Build w/ backup and cleanup IIS (existing servers)"
Write-Host "6. Remove Build w/out backup and cleanup IIS (existing servers)"
Write-Host "7. Remove and cleanup only IIS (existing servers)"
Write-Host "8. Deploy new DLL's (existing servers)"
Write-Host "9. [EXIT]" -foregroundcolor "yellow"
Write-Host " "
用途:
试图摆脱在我的服务器上拥有 1000 个批处理文件的情况
示例:
我在 "Master" 服务器上 运行 脚本,文本文件中还有 10 个其他服务器。起初,脚本仅在主服务器上 运行ning。一旦我选择选项 6,命令是 运行 在所有 11 个服务器(主服务器 + 10 个其他服务器)上执行相同的命令。
我知道如何 运行 使用类似 invoke-command
或 enter-pssession
,等等...但是,在我的情况下,我必须远程访问每台服务器,因为它是交互式的...除非有办法做到这一点?
此外,如果可能,启用日志记录,这样我就可以确认每台 PC 运行 脚本并成功完成所有操作。现在,我让它开始一个 t运行 脚本并在结束时停止。在本地效果很好。
如有任何帮助,我们将不胜感激。
更新:2016 年 3 月 8 日
所以,@Cobster 帮助了我,我们已经完成了 99%。
使用以下代码,脚本似乎正在尝试 运行,但还有一个问题。
$sessions = New-PSSession -ComputerName $servers
$job = Invoke-Command -Session $sessions -ScriptBlock ${function:Backup} -AsJob
If you want a different job for each machine, you can do the following:
$jobs = $sessions | ForEach-Object { Invoke-Command -Session $_ -ScriptBlock ${function:Backup} -AsJob }
Wait-Job -Job $jobs
问题是,完成此操作后,invoke-command 似乎没有在我的脚本顶部导入我之前声明的所有变量(实际上包括我所有的路径等,因为它们是未在该函数内定义。
这会导致一堆这些类型的错误(准确地说是 7 个,一个用于我在执行步骤之前的每个测试路径检查):
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
+ PSComputerName : *ServerName*
解法:
在invoke-command
和
中使用-ArgumentList $variable1, $variable2
param
(
$variable1,
$variable2
)
函数中的方法...由 Cobster 提供。
通过使用 -AsJob 开关,Invoke-Command 可以在后台运行。
通过使用 Receive-Job cmdlet 获取每个后台作业完成后的输出,也可以进行日志记录。
我编写了一个大型(983 行)交互式 PowerShell 脚本来管理本地服务器的几乎所有方面。我有一个包含服务器列表的文本文件。我想为之疯狂。
文本文件内容:
Server1Name
Server2Name
PowerShell 交互式菜单:
Write-Host "Welcome to the environment control script."
Write-Host " "
Write-Host "What would you like to do? Select the number:"
Write-Host "========================================================================="
Write-Host "1. Create a backup of the environment (existing servers)"
Write-Host "2. Backup environment and deploy updated environment (existing servers)"
Write-Host "3. Deploy new environment including IIS (clean servers)"
Write-Host "4. Deploy just IIS (clean servers)"
Write-Host "5. Remove Build w/ backup and cleanup IIS (existing servers)"
Write-Host "6. Remove Build w/out backup and cleanup IIS (existing servers)"
Write-Host "7. Remove and cleanup only IIS (existing servers)"
Write-Host "8. Deploy new DLL's (existing servers)"
Write-Host "9. [EXIT]" -foregroundcolor "yellow"
Write-Host " "
用途:
试图摆脱在我的服务器上拥有 1000 个批处理文件的情况
示例:
我在 "Master" 服务器上 运行 脚本,文本文件中还有 10 个其他服务器。起初,脚本仅在主服务器上 运行ning。一旦我选择选项 6,命令是 运行 在所有 11 个服务器(主服务器 + 10 个其他服务器)上执行相同的命令。
我知道如何 运行 使用类似 invoke-command
或 enter-pssession
,等等...但是,在我的情况下,我必须远程访问每台服务器,因为它是交互式的...除非有办法做到这一点?
此外,如果可能,启用日志记录,这样我就可以确认每台 PC 运行 脚本并成功完成所有操作。现在,我让它开始一个 t运行 脚本并在结束时停止。在本地效果很好。
如有任何帮助,我们将不胜感激。
更新:2016 年 3 月 8 日
所以,@Cobster 帮助了我,我们已经完成了 99%。
使用以下代码,脚本似乎正在尝试 运行,但还有一个问题。
$sessions = New-PSSession -ComputerName $servers
$job = Invoke-Command -Session $sessions -ScriptBlock ${function:Backup} -AsJob
If you want a different job for each machine, you can do the following:
$jobs = $sessions | ForEach-Object { Invoke-Command -Session $_ -ScriptBlock ${function:Backup} -AsJob }
Wait-Job -Job $jobs
问题是,完成此操作后,invoke-command 似乎没有在我的脚本顶部导入我之前声明的所有变量(实际上包括我所有的路径等,因为它们是未在该函数内定义。
这会导致一堆这些类型的错误(准确地说是 7 个,一个用于我在执行步骤之前的每个测试路径检查):
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
+ PSComputerName : *ServerName*
解法:
在invoke-command
和
-ArgumentList $variable1, $variable2
param
(
$variable1,
$variable2
)
函数中的方法...由 Cobster 提供。
通过使用 -AsJob 开关,Invoke-Command 可以在后台运行。
通过使用 Receive-Job cmdlet 获取每个后台作业完成后的输出,也可以进行日志记录。