注: PowerShellとの連携には、ShareGate MigrateのProまたはEnterpriseサブスクリプションが必要です。Essentialsプランでは利用できません。
foreachループステートメントでBrowser authenticationや最新の認証方式を使用する場合、接続のたびに資格情報を入力しなくて済むように、PowerShell パラメーター -UseCredentialsFrom を使うと、1つの接続を作成し、その接続の資格情報をスクリプト内で再利用できます。
-UseCredentialsFrom
-UseCredentialsFrom の仕組みを説明するために、基本的な接続スクリプトの例を次に示します。
# Connect to the first SharePoint site using browser authentication and store the connection
$connection = Connect-Site -Url "http://mytenant/sites/mysitecollection1" -ModernAuth
# Use the stored credentials to connect to the second SharePoint site
Connect-Site -Url "http://mytenant/sites/mysitecollection2" -UseCredentialsFrom $connection
# Use the same stored credentials to connect to the third SharePoint site
Connect-Site -Url "http://mytenant/sites/mysitecollection3" -UseCredentialsFrom $connection
このスクリプトを実行すると、ブラウザー ウィンドウが開き、mysitecollection1 に接続できます。接続が確立されると、スクリプトはその最初の接続を自動的に使用して、mysitecollection2 と mysitecollection3 に接続します。
foreachループで -UseCredentialsFrom を使用する方法
foreachループステートメントは通常、括弧を使って、CSV ファイルの各行に対する接続コマンドとコピーコマンドをアプリで繰り返し実行します。以下に例を示します。
# Import the Sharegate module
Import-Module ShareGate
# Define the path to the CSV file containing site information
$csvFile = "C:\CSV\CopySites.csv"
# Import the CSV file into a table using a comma as the delimiter
$table = Import-Csv $csvFile -Delimiter ","
# Define source site credentials
$srcUsername = "sourceusername"
$srcPassword = ConvertTo-SecureString 'sourcepassword' -AsPlainText -Force
# Connect to the destination site using browser authentication and store the connection
$dstsiteConnection = Connect-Site -Url "https://tenantname.sharepoint.com/sites/sitename" -Browser
# Declare variables for source and destination sites
Set-Variable -Name srcSite
Set-Variable -Name dstSite
# Loop through each row in the CSV table and copy sites from source to destination
foreach ($row in $table) {
# Clear any existing variable values for source and destination sites
Clear-Variable -Name srcSite
Clear-Variable -Name dstSite
# Connect to the source site using credentials
$srcSite = Connect-Site -Url $row.SourceSite -Username $srcUsername -Password $srcPassword
# Connect to the destination site using the stored credentials from the browser connection
$dstSite = Connect-Site -Url $row.DestinationSite -UseCredentialsFrom $dstsiteConnection
# Copy the source site to the destination site with merging and subsites included
Copy-Site -Site $srcSite -DestinationSite $dstSite -Merge -Subsites
}
このスクリプトを実行すると、foreach 文の前に、ブラウザー ウィンドウで資格情報の入力を求められます。
接続は変数 $dstsiteConnection に保存され、ループの各パスで -UseCredentialsFrom $dstsiteConnection として再利用されます。
これにより、コピーするサイトごとに資格情報を再入力する必要がなくなります。
注: $dstsiteConnection 変数には、CSV ファイルに記載されている URL を使用しないでください。記載されている URL のいずれかを使用すると、Clear-Variable コマンドによって資格情報が消去されます。
セッションをまたいで保存済みの接続を再利用する
-UseCredentialsFrom パラメーターは、同じスクリプト内で接続を再利用します。セッションをまたいで接続を再利用するには、接続時に -SaveConnection を使って Connection Manager に保存します。その後、Get-SharePointConnection で取得し、-UseConnection を使って Connect-Site または Connect-Tenant に渡します。
$connection = Get-SharePointConnection -Id 01c0ebab-a17e-4ae3-9caf-ba09f05f2970
Connect-Site -Url "http://mytenantdomain/sites/mysitecollection" -UseConnection $connection
詳細については、Get-SharePointConnectionを参照してください。
この記事はAIによって翻訳されています。ご不明な点がある場合は、英語の原文をご確認ください。
