Hi,
I have written a power shell script to add users into the SharePoint group as below
Need to log if any user is not found but
Only file is getting created in the path without logging if any user is not found.
Please let me if i am making any mistake
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$url = "url"
$site = new-object Microsoft.SharePoint.SPSite($url)
#$web = $site.OpenWeb()
$proposalsGroup = $web.SiteGroups["groupname"]
$filepath = $MyInvocation.MyCommand.Definition
# Executes a Stored Procedure from Powershell and returns the first output DataTable
function Exec-Sproc{
param($Conn, $Sproc, $Parameters=@{})
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$SqlCmd.Connection = $Conn
$SqlCmd.CommandText = $Sproc
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($SqlCmd)
$DataSet = New-Object System.Data.DataSet
[Void] $SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
return $DataSet.Tables[0]
}
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=servername;DataBase=DBName;Integrated Security=True"
$Res = Exec-Sproc -Conn $SqlConnection -Sproc "[dbo].[Proc_procedurename]"
foreach ($Row in $Res)
{
try
{
$ID=$($Row[0])
$username="domain\"+$ID
write-host $username
$user = $web.Site.RootWeb.EnsureUser($username)
if($user -eq $null)
{
Write-Host "User not found"
}
else
{
$proposalsGroup.AddUser($user)
write-host $user.DisplayName -ForegroundColor White "has been added to Proposals Group" -BackgroundColor Green
}
}
catch
{
Add-Content D:\Nucleus\Scripts\proposalsaccesserror.txt $DocNameError
}
}
Regards,
Sudheer
Thanks & Regards, Sudheer