我已经创建了测试 PowerShell 类。我的计划是运行并行后台作业来创建类对象并执行类中的方法
class Test {
[String]$ComputerName
[String]TestMrParameter() {
if ($this.ComputerName -eq 'jhon'){throw "Bad thing happened"}
return $this.ComputerName
}
}
$names = @('david', 'jhon', 'mark','tent')
$jobs = [System.Collections.ArrayList]@()
function TestMrParameter2 {
param (
[String]$name
)
[Test]$obj = [Test]::new()
$obj.ComputerName = $name
Write-Host $name + "TestMrParameter2 Function Reached!!!"
$output=$obj.TestMrParameter()
Write-Host "Output of the Test class is" + $output
}
foreach ($name in $names) {
$out = Start-Job -ScriptBlock ${Function:TestMrParameter2} -ArgumentList $name
$jobs.Add($out) | out-null
}
我完成了我的工作,但没有达到我的期望。因此,我检查了每个作业的详细信息以获取更多见解,并在每个作业上发现了此错误。它无法识别类以从中创建对象。
Unable to find type [Test].
+ CategoryInfo : InvalidOperation: (Test:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
+ PSComputerName : localhost
The property 'ComputerName' cannot be found on this object. Verify that the property exists and can
be set.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
+ PSComputerName : localhost