I am creating the module to disable sconfig at startup for allusers on servercore version. I am running the below code:
#!powershell
# Copyright: (c) 2024, MyName
#AnsibleRequires -CSharpUtil Ansible.Basic
$spec = @{
options = @{}
supports_check_mode = $true
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$checkMode = $module.CheckMode
try {
#Get the information if server is 'server core' version
$servercore = (Get-ItemProperty -Path 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion').InstallationType -eq 'Server Core'
if ($servercore) {
# Check sconfig autolaunch status
$sconfig = Get-SConfig -ErrorAction SilentlyContinue
$sconfig = $sconfig.AutoLaunch
if ($sconfig) {
# If sconfig autolaunch status is set to true then change the status to false
Set-SConfig -AutoLaunch $false -AllUsers -WhatIf:$checkMode
$module.Result.message = "SConfig AutoLaunch has been set to false."
$module.Result.changed = $true
}
else {
$module.Result.message = "SConfig AutoLaunch has already been set to false."
}
}
else {
$module.Result.message = "Server is not server core version"
}
}
catch {
# Handle the errors while fetching server core version informationn
$module.FailJson("An error occurred while fetching the sconfig information: $_")
}
$module.ExitJson()
Now, let me explain the problem. If I go to directly on the server, it works perfectly fine. However when I run via ansible playbook. I get the below error:
"msg": "An error occurred while fetching the sconfig information: Cannot bind argument to
parameter 'Path' because it is null.[0].Exception.Message"
It seems that problem is lying with:
(Get-SConfig).AutoLaunch
This command gives output without any error. Since ansible uses winrm so I thought to use below command locally on server:
Invoke-Command {Get-SConfig} -ComputerName $env:ComputerName
It gives the same error ‘cannot bind the parameter’ but also gives output. If I use -ErrorAction ‘SilentlyContinue’, it suppresses the error so I only get the output.
In the error it refers to the ‘path’ that is at line number 176 of sconfig.psm1 file located under C:WindowsSystem32WindowsPowerShellv1.0ModulesMicrosoft.ServeCore.SConfig2.0.0.0. I am attaching the screenshot as well. In the above powershell script, even if I use ErrorAction ‘SilentlyContinue’. It doesn’t work. Not sure what is happening. I have tried this command in server Desktop exp. version by removing ‘server core’ check, it has same issue there also.
One more important aspect. If I remove try catch block in above script on either of the server version ‘servercore or desktop exp’, it works perfectly fine. I am using it on my office system but I have tested it on my personal lab with server core version, it has same result. I downloaded the servercore module but same issue. Please help me to resolve this issue. Let me know if anyone needs any further information.
Thanks
Jatinder Pal Singh (JPS)