question
Add-Type
It supports adding C#, VB, JavaScript, dll and other programming languages as new .NET type 1 , and its scalability is extremely high.
However, it should be noted that Add-Type
the type cannot be removed, and once loaded in the same session window, repeated operations will only throw an error that the .NET type already exists.
solution
Referring to x0n 's answer in StackOverflow
question 2 , loaded types are not unregistered until the session is closed. Charlie Kilian and Start-Automating cleverly use the type to start an additional thread, and use the new type to automatically log out after the end of the run. code show as below:Job
Start-Job -ScriptBlock {
Add-Type -path 'my.dll'
$myObj = New-Object My.MyTestClassName
$result = $myObj.TestMethod
$result
} | Receive-Job -Wait -AutoRemoveJob