如何在Powershell中“多线程”我的存储迁移脚本?

时间:2015-08-21 20:54:39

标签: powershell automation powercli

我已经阅读了各种资源,其中包括 Start-Job 作为“多线程”任务的方法,但我没有弄清楚如何将变量传递给它。我正在使用PowerCLI cmdlet将大型公司的存储从旧阵列迁移到新阵列。他们在美国有7个站点。

迁移自动化的想法是多部分ps1。 第1部分:

Define variables
Import-csv list-of-vms | foreach {
Count the active tasks
While count is at or over threshold, loop.
else
Run site specific ps1 (part 2)
sleep 60
Count
}

第2部分:

if target has move or is queued, exit.
else, move to New-Array
exit

我需要的是一种将$ _.vm从Import-Csv传递到Part 2脚本的方法......以及解释:)

1 个答案:

答案 0 :(得分:0)

所以你不能得到的是使用带脚本的参数。网上有很多指南和帖子可以解释如何使用参数,但你可以先在powershell中运行Get-Help about_parameters -full。一个快速而肮脏的例子是:

Param(
    [String]$UserName,
    [String]$GroupName
)

$ADUser = Get-ADUser $UserName -prop MemberOf
If($GroupName -in $ADUser.MemberOf){"$UserName is a member of $GroupName"}

这允许用户执行脚本并指定用户和组,如果用户在组中,它将说出同样多的内容。执行将是这样的:

Script.ps1 -UserName 'TMTech' -GroupName 'CN=Administrators,OU=SecurityGroups,DC=ChinchillaFarm,DC=com'

那将检查用户是否TMTech'位于ChinchillaFarm.com域的Administrators组中。除非您开始进入高级参数设置,否则您也可以按位置传递参数,因此可以缩短为:

Script.ps1 'TMTech' 'CN=Administrators,OU=SecurityGroups,DC=ChinchillaFarm,DC=com'

插入警告!您的参数必须首先出现在大多数情况下。在您的脚本(或功能,但现在不要去那里)允许的唯一的事情是[cmdletbinding()]行,以及您希望在脚本中有任何基于注释的帮助。