Function in a module needs to run elevated

I’m new to scripting in general and have a question. I need to call a function in an in-house module that only needs to run elevated the first time. It creates an EventLogSource. Where do I need to put my elevation script block? In the script calling the function or the module itself? Here’s an example of what needs to run elevated. The function that is called runs this snippet is in a module that was built in-house.

try {
if (-not (Test-Path -Path $logSourcePath)) {

if not already registered as an event log source

New-EventLog -LogName Application -Source $LogSourceName

It sounds like you always need elevation to run new-eventlog if the path does not exist. Something like this should work. It will prompt UAC if it needs to create the log path.

if (-not (Test-Path -Path $logSourcePath)) {
    Start-Process -FilePath "pwsh" -ArgumentList "-NoProfile -Command New-EventLog -LogName Application -Source '$LogSourceName'" -Verb RunAs -Wait
}

1 Like

Thank you SO much. That worked in my first couple of simple tests. Now on to a more realistic test.

Again, you’re a life saver.

1 Like