One thing I'm much more cognizant about is putting any constant variables at the TOP of my scripts. This allows me to more easily reuse my scripts, and also to change the variables quickly without having to look all over the place.
#BEGIN SCRIPT
<#
REQUIRED: Make a folder called c:\lists, and put a file in it named ServerNTPSettingsAudit.txt that contains your servers' names (one per line). Also, this script assumes that you have a C:\temp folder.
#>
#Variables
$List = "C:\Lists\ServerNTPSettingsAudit.txt"
$Attachment = "C:\Temp\NTPSettings.csv"
#Email Variables
$To = "reporting@contoso.com"
$From = "reporting@contoso.com"
$SMTPServer = "mail.contoso.com"
$Subject = "PS Report - NTP Settings Audit"
$Body = "See Attached"
#Get the list of servers
$Servers = Get-Content $List
#Create an empty array to hold the data
$NTPSettings = @()
#Foreach server, get some NTP settings from the registry (remotely, obviously)
Foreach ($Server in $Servers){
$HKLM = 2147483650 #HKEY_LOCAL_MACHINE
$reg = [wmiclass]"\\$Server\root\default:StdRegprov"
$key = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
$value = "Type"
$NTPType = $reg.GetStringValue($HKLM, $key, $value) ## REG_SZ
$key = "SYSTEM\CurrentControlSet\Services\W32Time\Config"
$value = "AnnounceFlags"
$NTPFlags = $reg.GetDWordValue($HKLM, $key, $value) ## REG_DWORD
$key = "SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NTPServer"
$value = "Enabled"
$NTPServer = $reg.GetDWordValue($HKLM, $key, $value) ## REG_DWORD
$ServerItem = New-Object System.Object
$ServerItem | Add-Member -type NoteProperty -Name "Server Name" -value $Server
$ServerItem | Add-Member -type NoteProperty -Name "NTP Type" -value $NTPType.sValue
$ServerItem | Add-Member -type NoteProperty -Name "AnnounceFlags" -value $NTPFlags.uValue
$ServerItem | Add-Member -type NoteProperty -Name "IsNTPServer" -value $NTPServer.uValue
$NTPSettings += $ServerItem
} #End Foreach
#Export the array to CSV
$NTPSettings | Export-csv -NoTypeInformation $Attachment
#Send me the list as an email attachment
Send-mailmessage -To $To -From $From -SmtpServer $SMTPServer -Subject $Subject -Body $Body -Attachments $Attachment
#Delete the temp file
Remove-Item $Attachment -Force -ErrorAction SilentlyContinue
#END SCRIPT
Hi there! Software development is definitely not my thing, so I decided to hand it over to professionals, mostly because my patients' safety is a priority for me. Thanks to Pro4People my clinic has a brand new software tailored to our needs and it's great to work with. See more at https://pro4people.com/medical-device-software-development/ :)
ReplyDelete