$Host.Runspace.ThreadOptions = “ReuseThread”有神马用?

$Host.Runspace.ThreadOptions = “ReuseThread”

在很多PowerShell的脚本中你都会看到这个语句被用来开头,那它的作用是什么呢?

答:这个设置可以提高对内存的使用效率,减少内存泄露的可能性。在Windows PowerShell中,每条cmdlet都会开启它自己的一个进程,如果把ThreadOptions选为ReuseThread,这些cmdlet会共享同一进程。

所以,建议在你的SharePoint脚本开头部分除了要指定加载SharePoint.PowerShell.dll之外,还应该指定使用ReusedThread,就像下面这样:

 1 $ver = $host | select version  
 2 if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}  
 3 if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))  
 4 { 
 5         Add-PSSnapin Microsoft.SharePoint.PowerShell 
 6 } 
原文地址:https://www.cnblogs.com/theSharePointEvents/p/why-use-reused-thread-for-threadoptions.html