CSOMを利用したSharePointOnlineから権限情報を取得する方法

2024-07-30T09:44:44.5566667+00:00

以下の公開情報より、CSOMを利用してSharePoint Onlineの権限一覧を取得する方法を確認しました。

https://video2.skills-academy.com/ja-jp/archive/blogs/sharepoint_support/sharepoint-online-1250

上記のサイトよりサンプルのコマンドを実行したところ、以下のエラーが発生しました。

~~~~~エラー内容~~~~~

"0" 個の引数を指定して "ExecuteQuery" を呼び出し中に例外が発生しました: "接続が切断されました: 送信時に、予期しないエラ

ーが発生しました。。"

発生場所 C:_work\SPOShell.ps1:98 文字:3

  • $context.ExecuteQuery()
  • 
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    
        + FullyQualifiedErrorId : WebException
    
    

"0" 個の引数を指定して "ExecuteQuery" を呼び出し中に例外が発生しました: "接続が切断されました: 送信時に、予期しないエラ

ーが発生しました。。"

発生場所 C:_work\SPOShell.ps1:29 文字:3

  • $context.ExecuteQuery()
  • 
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    
        + FullyQualifiedErrorId : WebException
    
    

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to

be explicitly requested.

発生場所 C:_work\SPOShell.ps1:30 文字:12

  • foreach ($ra in $obj.RoleAssignments)
  •        ~~~
    
    • CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
    • FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException

"0" 個の引数を指定して "ExecuteQuery" を呼び出し中に例外が発生しました: "接続が切断されました: 送信時に、予期しないエラ

ーが発生しました。。"

発生場所 C:_work\SPOShell.ps1:57 文字:3

  • $context.ExecuteQuery()
  • 
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    
        + FullyQualifiedErrorId : WebException
    
    

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to

be explicitly requested.

発生場所 C:_work\SPOShell.ps1:58 文字:12

  • foreach ($list in $lists)
  •        ~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
    • FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException

"0" 個の引数を指定して "ExecuteQuery" を呼び出し中に例外が発生しました: "接続が切断されました: 送信時に、予期しないエラ

ーが発生しました。。"

発生場所 C:_work\SPOShell.ps1:76 文字:3

  • $context.ExecuteQuery()
  • 
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    
        + FullyQualifiedErrorId : WebException
    
    

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to

be explicitly requested.

発生場所 C:_work\SPOShell.ps1:77 文字:12

  • foreach ($aweb in $webs)
  •        ~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
    • FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException

~~~~~ここまで~~~~~

発生したエラーの原因や解決方法についてご教授いただけますでしょうか。

SharePoint
SharePoint
コンテンツ、知識、アプリケーションの共有と管理に使用される Microsoft 製品とテクノロジのグループ。
30 件の質問
PowerShell
PowerShell
コマンドライン シェルと関連するスクリプト言語で構成される Microsoft タスク自動化および構成管理フレームワークのファミリ。
20 件の質問
0 件のコメント コメントはありません
{count} 件の投票

1 件の回答

並べ替え方法: 最も役に立つ
  1. gekka 8,856 評価のポイント MVP
    2024-07-31T03:33:04.93+00:00

    そのページのサンプルコードは2015年のもので古く、多要素認証によるログインに対応できてないらしいです。

    PnP PowerShellを使ったログイン方法に変更すると動きました。

    # [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") > $null
    # [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") > $null
    
    function GetAllWebObjects()
    {
      # $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
      # $pwd = convertto-securestring $password -AsPlainText -Force
      # $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $pwd)
      # $context.Credentials = $credentials
    
      # $context= Connect-PnPOnline -Url $siteUrl -UseWebLogin
      $context= Connect-PnPOnline -Url $siteUrl -Interactive 
    
      $rootweb = $context.Web
      $context.Load($rootweb)
      $context.ExecuteQuery()
      WriteToFile -text "URL`tUser/Group`tRoles" -append $false
      GetWebObjectRoles -inweb $rootweb -url $rootweb.Url
    }
    
    #$outfilename="test.txt"
    #$siteUrl="https://*****.sharepoint.com/sites/*****"
    
    GetAllWebObjects
    
    1 人がこの回答が役に立ったと思いました。
    0 件のコメント コメントはありません

お客様の回答

回答は、質問作成者が [承諾された回答] としてマークできます。これは、ユーザーが回答が作成者の問題を解決したことを知るのに役立ちます。