Scenario: You want to see if NLA (Network Level Authentication) is enabled on all of your servers for Remote Desktop.
The PowerShell code below is not a .ps1, although you can make a .ps1 out of it if you would like. I like to teach working “within” PowerShell, not just being a script monkey.
- Build your $Servers Variable — Select a method to populate $Servers
- Run the Query to build the $Results table
- Display the $Results — Select a method
$Servers = “ExSrv1″,”ExSrv2” #Feel free to list the servers by name
#-or-
$Servers = Import-csv C:tempservers.csv | Select -expandproperty Name #Create a CSV with a NAME column header
#-or-
$Servers = get-adcomputer -searchbase “OU=Servers,DC=steve,DC=com” -properties Name -Filter *| Select -ExpandProperty Name #Pull a list of AD Computers via AD PowerShell
$Results = @()
$Servers | %{
#Collect NLA Data
$name = $_
$NLA = Get-WmiObject -class “Win32_TSGeneralSetting” -Namespace rootcimv2terminalservices -ComputerName $name -Filter “TerminalName=’RDP-tcp'” | Select -ExpandProperty UserAuthenticationRequired
#Display in PowerShell Screen
“$name : NLA_Enabled:$(If($NLA -eq 1){“Yes”}else{“No”})”
#Add the data to the $results variable
$ServerObj = New-Object PSObject
$ServerObj | Add-Member NoteProperty -Name “Server” -value $name
$ServerObj | Add-Member NoteProperty -Name “NLA_Enabled” -value $(If($NLA -eq 1){“Yes”}else{“No”})
$Results += $ServerObj
#Reset Variable
$Name = $null
$NLA = 0
}
$Results
#-or Export the Results –
$Results | Export-CSV C:tempresults.csv
#-or Email the Results –
$Header = @”
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black; text-align: center;}
tr:nth-child(odd) { background-color:#F2F2F2;}
tr:nth-child(even) { background-color:white;}
</style>
“@
$body =”$($results | ConvertTo-Html -Head $header)”
$smtp = “smtp.steve.com”
$to = “steve@steve.com”
$from = “SteveDoingSomething@steve.com”
$subject = “Results of NLA”
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high