How to publish an Active Directory Group members list to Sharepoint

For auditing purpose I create a powershell script that publish on SharePoint (via WEBDAV) a page with members of some Admin Groups:

Remember to change

"mydc.domain.local"

with the address of your Domain Controller and check that is also a Global Catalog.

Change also

"https://teams01.domain.local/000002/Wiki/Pages/"

with the SharePoint address where publish the script created HTML page and

"D:\srv_opt\Production_Environment_AdminGroups.html"

with the working path plus HTML page name that this script should create.

Import-Module ActiveDirectory

$DController = "mydc.domain.local:3268"
$webdavaddress = "https://teams01.domain.local/000002/Wiki/Pages/"
$outputfile = "D:\srv_opt\Production_Environment_AdminGroups.html"

function Publish-File {
	param (
		[parameter( Mandatory = $true, HelpMessage="URL pointing to a SharePoint document library (omit the '/forms/default.aspx' portion)." )]
		[System.Uri]$Url,
		[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage="One or more files to publish. Use 'dir' to produce correct object type." )]
		[System.IO.FileInfo[]]$FileName,
		[system.Management.Automation.PSCredential]$Credential
	)
	$wc = new-object System.Net.WebClient
	if ( $Credential ) { $wc.Credentials = $Credential }
	else { $wc.UseDefaultCredentials = $true }
	$FileName | ForEach-Object {
		$DestUrl = "{0}{1}{2}" -f $Url.ToString().TrimEnd("/"), "/", $_.Name
		Write-Verbose "$( get-date -f s ): Uploading file: $_"
		$wc.UploadFile( $DestUrl , "PUT", $_.FullName )
		Write-Verbose "$( get-date -f s ): Upload completed"
	}

}

$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"

$b = Get-ADGroupMember "Domain Admins" -recursive | Get-ADUser -Server $DController -Properties displayname | Select-Object Name, Givenname, Surname, DisplayName
$c = Get-ADGroupMember "DNSAdmins" -recursive | Get-ADUser -Server $DController -Properties displayname | Select-Object Name, Givenname, Surname, DisplayName
$d = Get-ADGroupMember "DHCP-Admins" -recursive | Get-ADUser -Server $DController -Properties displayname | Select-Object Name, Givenname, Surname, DisplayName

$RunDate = Get-Date

$outfile = $b | ConvertTo-HTML -head $a -body "<H2>Admin Groups - Auditing</H2><P>This page was generated by a Script at $RunDate</P>" -PreContent "<H3>MyDomain - Domain Admins</H3>" -PostContent " "
$outfile = $outfile + ($c | ConvertTo-Html -fragment -PreContent "<H3>MyDomain - DNS Admins</H3>" -PostContent " ")
$outfile = $outfile + ($d | ConvertTo-Html -fragment -PreContent "<H3>MyDomain - DHCP Admins</H3>" -PostContent " ")
$outfile | Out-File $outputfile
Dir $outputfile | Publish-File -Url $webdavaddress
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s