0

Having trouble with two VNC servers switching off MS Logon Groups being forced. I'm troubleshooting the issue, and one thing I want to do is monitor the config .ini file. I'm relatively new to PowerShell and can't quite get this to work.

Basically, I want the script to check the contents of the configuration file (ultravnc.ini) and see if "MSLogonRequired=1" is a string in that file. If not, I want to append the date to a log file. Eventually I'll do some more with this, but this is my basic need. It's not currently working.

# Variables 
$outputFile = "vncMSLogonErrors.txt" 
$vncConfig = "C:\Program Files (x86)\uvnc bvba\UltraVNC\ultravnc.ini"
$checkString = "MSLogonRequired=1"


# Get VNC Config File, check for MS Logon setting, write date to file if missing
Get-Content $vncConfig
If (-not $checkString)
   {Add-Content $outputFile -Value $(Get-Date)}
mklement0
  • 382,024
  • 64
  • 607
  • 775
dan-j
  • 1
  • use `Get-Content` to load the file, iterate thru it, if the check string is not found then add it. – Lee_Dailey Apr 18 '19 at 16:54
  • `if((Get-Content $vncConfig -raw) -notmatch $checkString){Set-content $outputFile -Value $(Get-Date) -Append}` –  Apr 18 '19 at 17:01

2 Answers2

1
# Variables 
$outputFile = "vncMSLogonErrors.txt" 
$vncConfig = "C:\Program Files (x86)\uvnc bvba\UltraVNC\ultravnc.ini"
$checkString = "MSLogonRequired=1"

if ((get-content $vncconfig) -notcontains $checkString)) { Add-Content $outputFile -Value $(Get-Date) }
mklement0
  • 382,024
  • 64
  • 607
  • 775
Shamus Berube
  • 466
  • 3
  • 12
  • See `Get-Help about_Comparison_Operators` cite: `Description: Containment operator. Tells whether a collection of reference values includes a single test value. Always eturns a Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values.` –  Apr 18 '19 at 17:06
  • @LotPings: If you're looking for a line that exactly matches `MSLogonRequired=1` (except for case variations), this approach works fine. – mklement0 Apr 18 '19 at 17:31
  • 1
    @mklement0 Agreed, the emphasis lying on exactly, just a trailing space wouldn't match exactly. –  Apr 18 '19 at 17:52
1

Shamus Berube's helpful answer is conceptually simple and works well, if you can assume:

  • that the line of interest is exactly MSLogonRequired=1, with no variations in whitespace.

  • that if the INI file is subdivided into multiple sections (e.g, [admin]), that the key name MSLogonRequired is unique among the sections, to prevent false positives.

It is therefore generally preferable to use a dedicated INI-file-parsing command; unfortunately:

  • PowerShell doesn't come with one, though adding one is being debated
  • in the meantime you can use the popular PsIni third-party module (see this answer for how to install it and for background information):

Using the PsIni module's Get-IniContent function:

Note: Based on the UltraVNC INI-file documentation, the code assumes that the MSLogonRequired entry is inside the [admin] section of the INI file.

# Variables 
$outputFile = "vncMSLogonErrors.txt" 
$vncConfig = "C:\Program Files (x86)\uvnc bvba\UltraVNC\ultravnc.ini"

# Check the VNC Config File to see if the [admin] section's 'MSLogonRequired'
# entry, if present, has value '1'.
if ((Get-IniContent $vncConfig).admin.MSLogonRequired -ne '1') {
   Add-Content $outputFile -Value (Get-Date) 
}
mklement0
  • 382,024
  • 64
  • 607
  • 775