LoginSignup
2
6

More than 5 years have passed since last update.

PowerShell で SharePoint Online にアクセスしてみたメモ

Last updated at Posted at 2017-04-16

ブログからの転載

しぇあぽいんとさんと仲良くなるために、まずは Windows では一番慣れている PowerShell を使って SharePoint Online へアクセスしてみたメモ。

環境

  • Windows 10 Pro
  • PowerShell 5.1

事前準備

SharePoint Online 用の SDK をインストールします。

サンプルコード

ユーザーが miyamiya@example.com、SharePoint Online の URL が https://<tenant>.sharepoint.com/sites/example/SitePages/Home.aspx と仮定してサンプルコード書く。

# SDK の読み込み
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

# アクセスする URL
$url  = 'https://<tenant>.sharepoint.com/sites/example/SitePages/Home.aspx'

# ユーザー名
$user = 'miyamiya@example.com'

# パスワードは都度入力
$secure = Read-Host -Prompt "Enter the password for ${user}(Office365)" -AsSecureString

# 認証情報生成
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $secure)

# WebRequest インスタンス生成
$request = [System.Net.WebRequest]::Create($url)

# 各種設定
$request.Credentials = $credentials
$request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$request.Accept = "application/json;odata=verbose"
$request.Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get

# レスポンスの取得
$response = $request.GetResponse()

# ストリームデータ取得
$readStream = New-Object System.IO.StreamReader ($response.GetResponseStream())

# 取得したデータを表示
$readStream.ReadToEnd()

考察

REST API を叩いて JSON を返すサンプルを参考にしましたが、別に REST API を叩きたいわけではなかったので、とりあえずアクセスした画面を HTML で保存するだけを行ってみました。

恐らくユーザー名とパスワードを使うのはあまり良くないのだろうけど、一般ユーザーではこの方法しかないんじゃないかな?と思っています。

参考

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6