0
0

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 5 years have passed since last update.

ファーム内の全ワークフローインスタンスの状態をタブ区切りで出力する

Posted at

実行すると全Web applicationのワークフローインスタンスの状態をタブ区切りテキストで出力します。
毎日バッチ実行とかして監視対象にするとエラーのキャッチアップや、障害分析にわりと便利。


if((Get-PSSnapin Microsoft.SharePoint.Powershell) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}

#####################  設定 ########################################

$ReportPath = "C:\" #結果を保存したいパスをいれとく

###################################################################




$Reportname = "Report_" + (get-date -Format "yyyyMMdd_HHmmss") + '.log'
$ReportFullPath = Join-Path $ReportPath $Reportname

$arrHeader = @('WebURL','List URL','ListTitle','Item Title','Item ID','Item Last Modified','Item Last Editor','Workflow.ParentAssociation.Name','Workflow.StatusText','Workflow.StatusValue','Workflow.StatusUrl','Workflow.IsLocked','Workflow.IsCompleted','Workflow.InternalState','Workflow.Created','Workflow.Modified','Workflow.AuthorUser')

$arrHeader -join "`t" | Out-File -FilePath $ReportFullPath -Append 

foreach ($wa in get-spwebapplication)
{
    #WebApp
    foreach($oSites in $wa.Sites)
    {
        #sites
        foreach($oWeb in $oSites.AllWebs)
        {
            #Web
            foreach($oList in $oWeb.Lists)
            {
                #List
                if($oList.WorkflowAssociations.Count -gt 0)
                {
                    foreach($oItem in $oList.Items)
                    {
                        #Item
                        foreach($Workflow in $oItem.Workflows)
                        {
                            $Log = @($oWeb.Url ,$oList.DefaultViewUrl,$oList.Title , $oItem.Title, $oItem.ID,$oItem['Modified'],$oItem['Editor'],$Workflow.ParentAssociation.Name,$workflow.StatusText,$Workflow.StatusValue,(($oWeb.URL).Trim()+'/'+($Workflow.StatusUrl).Trim()),$Workflow.IsLocked,$Workflow.IsCompleted,$Workflow.InternalState,$Workflow.Created,$Workflow.Modified,$Workflow.AuthorUser)
                            write-host ( $Log -join "`t" )
                            $Log -join "`t" | Out-File -FilePath $ReportFullPath -Append 
                        }
                    }
                }
            }
        }
    }
}

おまけ
ログローテーションしたいときはこんな感じの関数を定義しとく

function RotateLog($LogFilePath , $RotateDays)
{
        $Logs = ls $LogFilePath -file -filter *.log | ? { ( (Get-Date) - ($_.LastWriteTime) ).Days -gt $RotateDays }
        if($Logs.Count -gt 0){
            try{
                $Logs.Delete()
                }catch [exception]{
                    throw ($error[0])
                }
        }
}
RotateLog -LogFilePath $ReportPath -RotateDays 20 # ログファイルのパスとローテーション間隔(日)を渡す
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?