调用命令返回结果

问题描述 投票:1回答:1

使用C#一段时间后,我再次感觉像是PowerShell的新手;我无法在SharePoint服务器上运行Invoke-Command并返回结果。

我正在做的事情是在我所有的SharePoint网站和组中运行,并将某个用户所属的任何网站和组放入要返回的列表中。这是我在SharePoint服务器上运行的脚本块:

#Script block to be run
$sb = {
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $userName = $args[0]
    $domain = $args[1]

    $List = New-Object Collections.Generic.List[String]

    foreach ($oneSite in $iisSite){
        foreach ($siteCollection in $oneSite.Sites){

            foreach($web in $siteCollection.AllWebs){

                foreach($spUser in $web.Users){

                    if ($spUser -Like "$domain\$userName"){
                        if ($list -NotContains $web.Url){
                            $list.Add($web.Url)
                            Write-Host $web.Url -ForegroundColor White
                        }
                    }
                }

                foreach($spGroup in $web.Groups){
                    foreach ($groupUser in $spGroup.Users){
                        if($groupUser -Like "$domain\$userName"){
                            if ($list -NotContains $spGroup){
                                $list.Add($spGroup)
                                Write-Host $spGroup -ForegroundColor Red
                            }
                        }
                    }
                }
            }
        }
    }
    return $list
}

这就是我调用命令的方式:

$testing = Invoke-Command -ComputerName SHAREPOINTSERVER -ScriptBlock $sb -Authentication Credssp -Credential $credObject -ArgumentList $userName, $domain

我感觉自己在做一些愚蠢的事情,或者错过了一些明显的事情。在我的SharePoint服务器上本地运行时,它返回结果,但Invoke-Command中未返回任何内容。

非常感谢您的帮助:)

powershell sharepoint-2010 invoke-command
1个回答
0
投票

如果您只想要列出用户所属的URL,则应执行以下操作:

$iisSite.Sites.AllWebs |
    Where-Object { $_.Groups.Users -contains "${domain}\${userName}" } |
    Select-Object -Expand Users |
    Select-Object -Unique -Expand Url
© www.soinside.com 2019 - 2024. All rights reserved.