Monday, May 21, 2012

Powershell: How to insert a new alert

Sometimes for test I needed to insert a new alert in my application.

To accomplished this task I use the following script:

clear

$webAppUrl = Read-Host -Prompt "Url Web Application: "

if ($webAppUrl.Length -ile 0) {
$webAppUrl = "http://yourSharepointSite"
}

$web = Get-SPWeb $webAppUrl

$userName = Read-Host -Prompt "Which user: "

$users = $web.Users

foreach ($user in $users) {
if ($user.Name -ne $userName) {
continue
}

$newAlert = $web.Alerts.Add()
$newAlert.AlertType = "List";
$newAlert.List = Read-Host -Prompt "Which list: "
$newAlert.AlwaysNotify = False
$newAlert.DynamicRecipient = "assignedTo";
$newAlert.EventTypeBitmask = 3
$newAlert.User = $user
$newAlert.AlertFrequency = Read-Host -Prompt "Alert Frequency (Daily/Weekly): "

$newAlert.Properties["SomeCustomProperties"] = "V_Value1;V_Value2"
$newAlert.Title = "Alert from powershell"
$newAlert.Update()
Write-Host "Alert inserted"
}


bye Winking smile

No comments:

Post a Comment