我有一个 .ps1 文件,可以从 PS 提示符执行。在文件的顶部我有:
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
在后面的代码中,它有:
$site = new-object Microsoft.SharePoint.SPSite $url;
我收到以下错误:
Unable to find type [Microsoft.SharePoint.SPWeb]: make sure that the assembly containing this type is loaded.
如果我直接在提示符下运行 LoadWithPartialName
语句,那么我就可以执行该脚本。
我做错了什么?
--更新--
当我删除空白时,制作代码:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
错误没有改变,因为 LoadWithPartialName
执行时没有错误。
--新信息--
这与添加带有 SPWeb 类型参数的函数有关。
这有效:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();
这有效:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function doSomething(){ }
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();
但是如果(您必须在出现问题之前启动一个新的 PS session 。此外,该函数是在 SPSite 第一次实例化之前还是之后并不重要:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function doSomething( [Microsoft.SharePoint.SPWeb] $web ){ }
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();
我认为第二个解决方法是不键入参数或将负载放入另一个 ps1 中。
最佳答案
该大会是否有可能不在 GAC 中?如果没有,则需要在 PowerShell 安装目录中才能找到 LoadWithPartialName。仅供引用,LoadWithPartialName 方法已过时,不推荐。如果您使用的是 PowerShell V2,请尝试在文件系统上找到 SharePoint.dll 并使用 Add-Type
例如:
Add-Type -Path <path>\Microsoft.SharePoint.dll
关于sharepoint - 在 PowerShell 中,尽管我调用 LoadWithPartialName,但对象未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9929452/