【问题标题】:Passing a Powershell object as a string parameter将 Powershell 对象作为字符串参数传递
【发布时间】:2012-07-03 18:55:08
【问题描述】:

我目前正在尝试编写以下 Powershell 脚本,用 SharePoint 术语来说,它会检索管理中心 url(在 $adminUrl 中检索),然后使用该 url 打开一个 Internet Explorer 窗口。

我还在将另一个字符串附加到 $adminUrl 之前,然后将其传递给 Navigate 方法:

$adminUrl = Get-spwebapplication -includecentraladministration | where {$_.DisplayName -eq "SharePoint Central Administration v4"} | select Url

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate($adminUrl + "/someurl") # <= Trying to pass the url here
$ie.Visible = $true

但我在尝试这样做时遇到了这个异常:

Cannot find an overload for "Navigate" and the argument count: "1".
At \\a\setup.ps1:9 char:1
+ $ie.Navigate($adminUrl)
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

我在这里遇到选角问题吗?

【问题讨论】:

    标签: sharepoint powershell


    【解决方案1】:

    $adminUrl 是一个带有 url 属性的对象,所以需要使用子表达式来传递:

    $ie.Navigate($adminUrl.Url + "/someurl")
    

    或带有子表达式:

    $ie.Navigate("$($adminUrl.Url)/someurl")
    

    只有先扩展 Url 属性的值,才能传递 $adminUrl 的值:

     ...| select -ExpandProperty Url
     $ie.Navigate("$adminUrl/someurl")
    

    【讨论】:

    • +1 太好了,这正是我想要的。我认为使用select 我只是直接返回url,但它实际上返回了一个包含该成员的对象。谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多