Home ProgrammingPowerShell PowerShell: PowerShell, WSUS, and SharePoint

PowerShell: PowerShell, WSUS, and SharePoint

by Kliment Andreev
4.5K views

Here is a small script that I made based on different PowerShell scripts. This script will connect to a WSUS server, get a list of all servers and create a CSV file that contains the computer name, how many updates are needed, how many updates were not installed, how many of them were installed and how many of them failed to install. After that, the script will connect to a SMTP server and try to send this CSV file to an e-mail address. Finally, if you have a SharePoint server or the free SharePoint services running, you can publish the file.
Make sure that you have PoshWSUS module and WSUS console installed prior to running this script. In addition if you are using v2.0.3 of PoshWSUS module, you have to manually modify Disconnect-WSUSServer.ps1 file as desribed here.

Import-Module PoshWSUS
# Use -port 8530 if you are not running WSUS on defualt port 80
Connect-WSUSServer -wsusserver the_name_of_your_wsus_server -port 8530
# The CSV file that has been generated
$file = "c:\Reports\MissingUpdates.csv"
# The IP address of the SMTP server
$smtpserver = "127.0.0.1"
# Get the update status for each client
Get-WSUSUpdateSummaryPerClient | sort neededcount -des | select computer, neededcount, notinstalledcount, \
installedcount, failedcount,lastupdated | Export-CSV $file
# Send an email using SMTP
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "[email protected]"
$msg.To.Add("[email protected]")
$msg.Subject = "Notification from WSUS server - Missing Updates"
$msg.Body = "Attached is the Excel file. Do not reply to this message."
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

# Upload to SharePoint
$path = "C:\Reports"
$destination = "http://share_point_server/Shared%20Documents/Reports"

# If you want to publish the CSV file using domain credentials, uncomment the following two lines. 
# This will reveal you password
#$securePasssword = ConvertTo-SecureString "Your_Password" -AsPlainText -Force
#$credentials = New-Object System.Management.Automation.PSCredential ("DOMAIN\username", $securePasssword)
# If you want to publish the file, using the crednetials of the user that's running the script 
# comment the previous two lines, and uncomment the line below.
$credentials = [System.Net.CredentialCache]::DefaultCredentials

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
# This code will publish all files that are larger than 0 bytes from C:\Reports folder.
# You can modify it to suit your needs
Get-ChildItem $path | Where-Object {$_.Length -gt 0} | ForEach-Object {
    $webclient.UploadFile($destination + "/" + $_.Name, "PUT", $_.FullName)
    # Remove-Item $_.FullName -Force
    }

# Disconnect from WSUS
Disconnect-WSUSServer

Related Articles

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More