0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PowerShell】SharePoint上に複数サイトを一括で作成する

Last updated at Posted at 2022-05-19

はじめに

この記事ではWindows PowerShell ISEのバージョン5.1.19041.1645を使用しています。
SharePointでサイト作成ツールを作成する際に、SharePointOnlineの記事はたくさんありますがSharePointServerの記事って意外とないな~と思い備忘も兼ねてこちらに残します。
誰かの助けになればイイナ...。
業務でSharePointServerを使用している方もいらっしゃると思います。
今回はPowerShellを用いてサイトを一括で作成する方法をご紹介します。

やり方

用意するファイルは以下です。

  • ps1ファイル
  • ps1ファイルに読み込ませるxmlファイル
  • xmlファイルに読み込ませるcsvファイル

ps1ファイル

ps1ファイルの内容は以下です。

siteCreate.ps1
#モジュールをコンソールにインポート
if ($null -eq (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)) {
    Add-PsSnapin Microsoft.SharePoint.Powershell
}

#メッセージ関数
function Messages ($category) {

    #出力XML
    $scriptPath = "ログを出力する先のパス"
    $xml01 = [System.Xml.XmlDocument](Get-Content -Encoding UTF8 -Raw $scriptPath\output.xml)
    $outputPath = [string]$xml01.outputfile.log    
    
    if($category -eq "Start"){

        #処理時間を定義
        $time = Get-Date
        #処理開始
        $Message = "$time`r`n==処理を開始します==" | out-file $outputPath -Append
        return $Message
    
    }elseif($category -eq "Success"){

        #処理時間を定義
        $time = Get-Date
        #正常終了
        $Message = "$time`r`nスクリプトは正常に実行されました" | out-file $outputPath -Append
        return $Message

    }elseif($category -eq "Error"){

        #処理時間を定義
        $time = Get-Date
        #エラー発生
        $Error01 = "$time`r`nスクリプトに次のエラーが発生しました" | out-file $outputPath -Append
        $Error02 = $error[0] | Format-List -force | out-file $outputPath -Append
        $Error03 = "Error Occurring part  : $siteName" | out-file $outputPath -Append
        $Message = $Error01 + $Error02 +$Error03
        return $Message

    }elseif($category -eq "End"){

        #処理時間を定義
        $time = Get-Date
        #処理終了
        $Message = "$time`r`n==処理を終了します==`r`n" | out-file $outputPath -Append
        return $Message

    }
}

#処理開始
Messages Start

#エラー構文
try {

    #xml読み込み
    $scriptPath = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)
    $xml = [System.Xml.XmlDocument](Get-Content -Encoding UTF8 -Raw $scriptPath\config.xml)
    $csvPath = [string]$xml.files.content.item
    $line = @(Import-Csv $csvPath -Encoding Default)

    #サイト作成ループ
    for ($i=0; $i -lt $line.Length ;$i++) {

        #サイト作成のための変数を定義
        $siteUrl = $line[$i].SiteUrl
        $siteName = $line[$i].SiteName
        $template = $line[$i].Template

        #サブサイトの追加
        New-SPWeb $siteUrl -Template $template -Name $siteName -ErrorAction Stop

    }
    
    #正常処理
    Messages Success

}catch {

    #異常処理
    Messages Error

}finally {

    #処理終了
    Messages End

}

xmlファイル

xmlファイルには後に作成するサイト情報を記載したcsvファイルのパスを指定したconfig.xml
実行結果のlogファイルの出力先パスを指定したoutput.xmlの2種類を作成します。

config.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><files>
 <content>
  <id>1</id>
  <item>csvファイルまでを含むパス</item>
 </content>
</files>
output.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><outputfile>
 <log>logファイルまでを含むパス</log>
</outputfile>

csvファイル

ヘッダーにSiteUrl,SiteName,Templateを記載します。
左から作成したいサイトのURL、サイト名、サイトのテンプレートをそれぞれ入力します。
サイトテンプレートは多く存在しますが、一部載せますね。

ID Title Name Description
1 チーム サイト
(クラシック表示)
STS#0 ホーム ページのクラシック表示のサイトです。
Microsoft 365 のグループには接続されていません。
1 空のサイト STS#1 必要に応じてカスタマイズできる空のサイトです。
1 ドキュメント
ワークスペース
STS#2 ドキュメントを使用して共同作業を行うためのサイトです。
ドキュメント ライブラリには主要なドキュメントとそれに関連するファイルを保管できます。また、タスク リストにより必要な作業アイテムを割り当てでき、リンク リストによりドキュメントに関連するリソースを共有できます。
1 チーム サイト
(Microsoft 365 グループなし)
STS#3 Microsoft 365 グループに接続されていないサイト。

引用元:SharePointDeveloper「サイトテンプレートの種類を表す「サイトテンプレートID」」

https://sharepoint.orivers.jp/article/10432

おわりに

少し準備が面倒ですが、設定ファイルを外出しにすることでps1ファイルをいじらなくていいのでヒューマンエラーを未然に防げる確率は上がると思います。
お粗末ですが参考になれば幸いです。

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?