<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell アーカイブ - 三日坊主の軌跡</title>
	<atom:link href="https://kururu-tech.net/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://kururu-tech.net/tag/powershell/</link>
	<description></description>
	<lastBuildDate>Wed, 22 May 2024 00:20:26 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>SharePoint OnlineをPowerShellで操作してみる</title>
		<link>https://kururu-tech.net/pc/sharepoint-online%e3%82%92powershell%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b/</link>
					<comments>https://kururu-tech.net/pc/sharepoint-online%e3%82%92powershell%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b/#respond</comments>
		
		<dc:creator><![CDATA[charo]]></dc:creator>
		<pubDate>Wed, 22 May 2024 00:20:24 +0000</pubDate>
				<category><![CDATA[PC]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://kururu-tech.net/?p=5129</guid>

					<description><![CDATA[<p>SharePoint OnlineをPowerShellで操作する方法はいくつかあるけれど、Invoke-Webrequestを使わずやってみる。 1. Entra IDからアプリの登録 バッチ処理が前提のため、まずEn [&#8230;]</p>
<p>投稿 <a href="https://kururu-tech.net/pc/sharepoint-online%e3%82%92powershell%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b/">SharePoint OnlineをPowerShellで操作してみる</a> は <a href="https://kururu-tech.net">三日坊主の軌跡</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[
<p>SharePoint OnlineをPowerShellで操作する方法はいくつかあるけれど、Invoke-Webrequestを使わずやってみる。</p>



<p></p>



<h2 class="wp-block-heading">1. Entra IDからアプリの登録</h2>



<p>バッチ処理が前提のため、まずEntra IDからSharePoint Online操作用のアプリを登録する。</p>



<p>■ 必要な設定項目のみ抜粋</p>



<p>[構成されたアクセス許可]</p>



<p>・Microsoft Graphから「アプリケーションの許可」</p>



<p>   ・Files.ReadWrite.All</p>



<p>   ・Sites.Read.All</p>



<figure class="wp-block-image size-full is-style-default"><a href="https://kururu-tech.net/wp-content/uploads/2024/05/image-2.png"><img fetchpriority="high" decoding="async" width="789" height="251" src="https://kururu-tech.net/wp-content/uploads/2024/05/image-2.png" alt="" class="wp-image-5132" srcset="https://kururu-tech.net/wp-content/uploads/2024/05/image-2.png 789w, https://kururu-tech.net/wp-content/uploads/2024/05/image-2-300x95.png 300w, https://kururu-tech.net/wp-content/uploads/2024/05/image-2-768x244.png 768w, https://kururu-tech.net/wp-content/uploads/2024/05/image-2-624x199.png 624w" sizes="(max-width: 789px) 100vw, 789px" /></a></figure>



<p></p>



<p>[証明書とシークレット]</p>



<p>自己証明書の作成とThumbprintの取得</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: powershell; title: ; notranslate">
$certname = &quot;spo-test&quot;
$crt = New-SelfSignedCertificate -Subject &quot;CN=$certname&quot; -CertStoreLocation &quot;Cert:\CurrentUser\My&quot; -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -KeyAlgorithm RSA -HashAlgorithm SHA256 -NotAfter(Get-Date).AddYears(20)
$crt | select Thumbprint
</pre></div>


<p>先の手順で作成した証明書をアップロード</p>



<figure class="wp-block-image size-large is-style-default"><a href="https://kururu-tech.net/wp-content/uploads/2024/05/image-1.png"><img decoding="async" width="1024" height="265" src="https://kururu-tech.net/wp-content/uploads/2024/05/image-1-1024x265.png" alt="" class="wp-image-5131" srcset="https://kururu-tech.net/wp-content/uploads/2024/05/image-1-1024x265.png 1024w, https://kururu-tech.net/wp-content/uploads/2024/05/image-1-300x78.png 300w, https://kururu-tech.net/wp-content/uploads/2024/05/image-1-768x199.png 768w, https://kururu-tech.net/wp-content/uploads/2024/05/image-1-624x161.png 624w, https://kururu-tech.net/wp-content/uploads/2024/05/image-1.png 1040w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p></p>



<h2 class="wp-block-heading">2. SharePoint Onlineにファイルをアップロード</h2>



<p>実際にPowerShellを使ってSharePoint Onlineにファイルをアップロードしてみる。</p>



<p>例）サイト名：テストサイトのドキュメントにあるテストフォルダにローカルのhoge.txtファイルをアップロード</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: powershell; title: ; notranslate">
$uploadFile = &quot;C:\folder\hoge.txt&quot;
$fileName = &quot;hoge.txt&quot;

try {
    Connect-MgGraph -ClientId &quot;先の手順で作成したアプリ登録のClient ID&quot; -CertificateThumbprint &quot;先の手順で取得したThumbprint&quot; -NoWelcome
    Write-Host &quot;&#x5B;Info] SPOに接続しました&quot;
} catch {
    Write-Host &quot;&#x5B;Error] SPOに接続できませんでした&quot;
    exit
}
$SiteID = Get-MgSite -Search &quot;テストサイト&quot; | Select-Object -ExpandProperty Id
$driveID = (Get-MgSiteDrive -SiteId $siteID | Where-Object {$_.Name -eq &quot;ドキュメント&quot;}).Id

$documentDriveItemID = (Get-MgDriveItemChild -DriveId $driveID -DriveItemId &quot;root&quot; | Where-Object {$_.Name -eq &quot;ドキュメント&quot;}).Id
$uploadFolderID = (Get-MgDriveItemChild -DriveId $driveID -DriveItemId $documentDriveItemID | Where-Object {$_.Name -eq &quot;テストフォルダ&quot;}).Id
$driveItemID = $uploadFolderID+&quot;:/&quot;+$fileName+&quot;:&quot;
try {
    Set-MgDriveItemContent -DriveId $driveID -DriveItemId $driveItemID -InFile $uploadFile
} catch {
    Write-Host &quot;&#x5B;Error] SPOにアップロードできませんでした&quot;
}
Disconnect-MgGraph
</pre></div>


<h2 class="wp-block-heading">3. はまりどころ</h2>



<p>Set-MgDriveItemContentのリファレンスマニュアルの説明が少なすぎて使い方がわからなかった…</p>



<p><a href="https://learn.microsoft.com/ja-jp/powershell/module/microsoft.graph.files/set-mgdriveitemcontent?view=graph-powershell-1.0">https://learn.microsoft.com/ja-jp/powershell/module/microsoft.graph.files/set-mgdriveitemcontent?view=graph-powershell-1.0</a></p>



<p>DriveItemIdの引数、素直に考えるとそのままDriveItem IDを入力すればよいと思いきや、それだとダメ</p>



<figure class="wp-block-image size-full is-style-default"><a href="https://kururu-tech.net/wp-content/uploads/2024/05/image-4.png"><img decoding="async" width="724" height="137" src="https://kururu-tech.net/wp-content/uploads/2024/05/image-4.png" alt="" class="wp-image-5135" srcset="https://kururu-tech.net/wp-content/uploads/2024/05/image-4.png 724w, https://kururu-tech.net/wp-content/uploads/2024/05/image-4-300x57.png 300w, https://kururu-tech.net/wp-content/uploads/2024/05/image-4-624x118.png 624w" sizes="(max-width: 724px) 100vw, 724px" /></a></figure>



<p>実際のところは…</p>



<p><strong>DriveItem ID+ &#8220;:/&#8221;+アップロードする際の任意のファイル名+&#8221;:&#8221;</strong></p>



<p></p>



<p>こんなのわからないって…</p>
<p>投稿 <a href="https://kururu-tech.net/pc/sharepoint-online%e3%82%92powershell%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b/">SharePoint OnlineをPowerShellで操作してみる</a> は <a href="https://kururu-tech.net">三日坊主の軌跡</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://kururu-tech.net/pc/sharepoint-online%e3%82%92powershell%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
