spodownload.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<span class="p">[ </span><span class="no">System.Reflection。</span><span class="kt">アセンブリ</span><span class="p">] :: </span><span class="nf">LoadWithPartialName </span><span class="p">(</span><span class="s2">"Microsoft. SharePoint.Client" </span><span class="p">)</span><span class="o">| </span><span class="nf">Out-Null </span><span class="p">[ </span><span class="no">System.Reflection。</span><span class="kt">アセンブリ</span><span class="p">] :: </span><span class="nf">LoadWithPartialName </span><span class="p">(</span><span class="s2">"Microsoft.SharePoint.Client.Runtime" </span><span class="p">)</span><span class="o">| </span><span class="nf">アウトヌル</span> <span class="nv">$ pass </span><span class="o">= </span><span class="nf">convertto-securestring </span><span class="s2">"password" </span><span class="nt">-AsPlainText </span><span class="nt">-Force </span><span class="nv">$ credentials </span><span class="o">= </span><span class="nf">New-Object </span><span class="nx">Microsoft.SharePoint.Client. SharePointOnlineCredentials </span><span class="p">(</span><span class="s2">"userAccount" </span><span class="p">、</span><span class="nv">$ pass </span><span class="p">)</span><span class="nv">$ cookieValue </span><span class="o">= </span><span class="nv">$ credentials </span><span class="o">。</span><span class="nf">GetAuthenticationCookie </span><span class="p">(</span><span class="s2">"https://xxxxxxx.sharepoint. com/sites/site/" </span><span class="p">)</span> <span class="nv">$ cookieValue </span><span class="o">= </span><span class="nv">$ cookieValue </span><span class="o">。 </span><span class="p">(</span><span class="s2">"SPOIDCRL =" </span><span class="p">、</span><span class="s2">"" </span><span class="p">)</span><span class="nv">$ cookie </span><span class="o">= </span><span class="nf">New-Object </span><span class="nx">System.Net.Cookie </span><span class="nv">$ cookieを</span><span class="nf">置き換え</span><span class="o">ます。 </span><span class="nf">Name </span><span class="o">= </span><span class="s2">"SPOIDCRL" </span><span class="nv">$ cookie </span><span class="o">。</span><span class="nf">値</span><span class="o">= </span><span class="nv">$ cookieValue</span> <span class="nv">$ cookieContainer </span><span class="o">= </span><span class="nf">New-Object </span><span class="nx">System.net.CookieContainer </span><span class="nv">$ cookieContainer </span><span class="o">。</span><span class="nf">追加</span><span class="p">(</span><span class="s2">"https://xxxxxxx.sharepoint.com/sites/site/" </span><span class="p">、 </span><span class="nv">$クッキー</span><span class="p">)</span> <span class="nv">$ request </span><span class="o">= </span><span class="p">[ </span><span class="no">System.Net。</span><span class="kt">WebRequestクラス</span><span class="p">] :: </span><span class="nf">作成します </span><span class="p">(</span><span class="s2">"https://xxxxxxx.sharepoint.com/sites/site/Shared%20Documents/ test.xlsx" </span><span class="p">)</span><span class="nv">$リクエストを</span><span class="o">。</span><span class="nf">CookieContainer </span><span class="o">= </span><span class="nv">$ cookieContainer </span><span class="nv">$ response </span><span class="o">= </span><span class="nv">$ request </span><span class="o">。</span><span class="nf">GetResponse </span><span class="p">()</span><span class="nv">$ responseStream </span><span class="o">= </span><span class="nv">$ response </span><span class="o">。 </span><span class="nf">GetResponseStream </span><span class="p">()</span> <span class="nv">$ outputFile </span><span class="o">= </span><span class="p">[ </span><span class="no">System.IO。</span><span class="kt">ファイル</span><span class="p">] :: </span><span class="nf">作成</span><span class="p">(</span><span class="s2">"C:\ work \ test.xlsx" </span><span class="p">)</span><span class="nv">$ Buffer </span><span class="o">= </span><span class="nf">New-Object </span><span class="nx">Byte </span><span class="p">[] </span><span class="nx">1024 </span><span class="kr">Do </span><span class="p">{ </span><span class="nv">$ BytesRead </span><span class="o">= </span><span class="nv">$ responseStream </span><span class="o">。 </span><span class="nf">読む</span><span class="p">(</span><span class="nv">$バッファ</span><span class="p">、</span><span class="mi">0 </span><span class="p">、</span><span class="nv">$バッファ</span><span class="o">。</span><span class="nf">長</span><span class="p">)</span><span class="nv">$ OUTPUTFILE </span><span class="o">。 </span><span class="nf">Write </span><span class="p">(</span><span class="nv">$ Buffer </span><span class="p">、</span><span class="mi">0 </span><span class="p">、</span><span class="nv">$ BytesRead </span><span class="p">)</span><span class="p">} </span><span class="kr">While </span><span class="p">(</span><span class="nv">$ BytesRead </span><span class="o">-gt </span><span class="mi">0 </span><span class="p">)</span> <span class="nv">$ responseStream </span><span class="o">。</span><span class="p">()</span><span class="nv">$ responseStreamを</span><span class="nf">閉じます</span><span class="o">。</span><span class="nf">処分</span><span class="p">()</span> <span class="nv">$ outputFile </span><span class="o">。</span><span class="nf">Flush </span><span class="p">()</span><span class="nv">$ outputFile </span><span class="o">。</span><span class="p">()</span><span class="nv">$ outputFileを</span><span class="nf">閉じます</span><span class="o">。</span><span class="nf">処分</span><span class="p">()</span> |
参考
https://teratail.com/questions/79342