powershell - 为什么 PowerShell 不能正确地转换为基类?

标签 powershell casting

我正在尝试调用一个需要两个参数的方法。第一个参数是 ClientRuntimeContext。我只有一个 ClientContext,但该类继承自 ClientRuntimeContext。如果我调用该方法,我会收到以下错误:

新对象:找不到“”的重载和参数计数:“2”。

所以,我认为我需要先转换对象。在转换之前,如果我查看 $clientContext.GetType().Name,它会返回“ClientContext”。这是我尝试过的两个转换表达式:

$clientRuntimeContext = $clientContext -as [Microsoft.SharePoint.Client.ClientRuntimeContext]
$clientRuntimeContext = [Microsoft.SharePoint.Client.ClientRuntimeContext]$clientContext

不幸的是,在这两种情况下,$clientRuntimeContext.GetType().Name 返回“ClientContext”而不是“ClientRuntimeContext”,我认为这是问题的根源。

有没有办法强制 PowerShell 正确转换?我查看了 Trace-Command TypeConversion,如 http://www.powershellatoms.com/powershell-101/casting-values-in-powershell/ 中所述,但这并没有帮助。

更新:由于大众需求,我提供了实际的脚本。我想避免 CSOM 和 SharePoint Online 的复杂性,但也许这就是问题的一部分。这是脚本。

$SPClient = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$SPClientRuntime = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll”

$siteUrl = "https://tenant.sharepoint.com"
$username = "user@tenant.onmicrosoft.com"
$password = Read-Host -Prompt "Enter Password" -AsSecureString

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
$clientContext.Credentials = $credentials

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

$webWFAssociations=$web.WorkflowAssociations
$clientContext.Load($webWFAssociations)
$clientContext.ExecuteQuery()

$clientRuntimeContext = $clientContext -as [Microsoft.SharePoint.Client.ClientRuntimeContext]
$WFServicesManager =New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($clientRuntimeContext, $clientContext.Web)

最后一行是失败的错误:New-Object:找不到“WorkflowServicesManager”的重载和参数计数:“2”。

最佳答案

如果我使用 WorkflowServices 程序集的“版本 16”而不是“版本 15”,我可以让您的代码正常工作。例如:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

关于powershell - 为什么 PowerShell 不能正确地转换为基类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266203/

相关文章:

java - 在 Java 中是否有首选的对象转换方法?

powershell - 如何使用PowerShell从字符串读取特定变量?

powershell - 在 psake 任务中使用时在模块中定义的功能范围

bash - 使用WinSCP在freeSSHD服务器上运行命令失败并显示 “Your shell is probably incompatible with the application (BASH is recommended)”

Java 将 object[] 转换为模型

mysql - mysql 中使用 bigint 和 varchar 时出现“cast”问题

java - 从字符串到泛型的转换

postgresql - Postgres Cast 复合类型

powershell - Get-ChildItem 结果看起来像 Powershell 中的相对路径

powershell - 如何通过 PowerShell 脚本调整 Windows 10 上的 Docker 桌面虚拟机的大小?