'Variable being passed in as null - Powershell

So I have an issue that has been bugging me for a few hours now.

I have two functions, Write-Log, and LogProfileRemoval. In Write-Log, I pass in the two arguments as shown here.

LogProfileRemoval('$LogEventDetail', 100000)

But when I check the variables of LogProfileRemoval they are shown like this

$LogEventDetail = '$LogEventDetail' 100000 
$LogMethod = $null

I am aware that I have quotes around the variable $LogEventDetail, that was part of my testing to figure this out. Really that variable could be anything and it still concats those two variables into one and leaves the 2nd parameter as a null value.

What am I doing wrong here.

Thanks

function LogProfileRemoval($LogEventDetail, $LogMethod)
{
   Switch ($LogMethod)
    {
        'EventLog' {LogToEventLog($LogEvent)}
    }
}
function Write-Log($logDetail, $logEvent=2)
{
    $LogEventDetail = New-Object EventLog -Property @{EventTimeStamp=(Get-Date);EventType=$logEvent;EventDetail=$logDetail}
    $LogMethod = 1
    LogProfileRemoval('$LogEventDetail', 100000)
}


Solution 1:[1]

So by not following best practices, was the issue. Weird becuase I have always wrote my powershell scripts like this. Always been a not fan of the Param way of doing it. I changed it to best practice way (sorta) and it worked great.

I would like to know why it didn't work though but overall It just goes to show me to quick being lazy and do it the right way.

Code working shown below

    function LogProfileRemoval
    {
        param( 
                $LogEventDetail,
                $LogMethod
            )
    
       Switch ($LogMethod)
        {
            'EventLog' {LogToEventLog($LogEvent)}
        }
    }
    function Write-Log($logDetail,$logEvent=2)
    {
        $LogEventDetail = New-Object EventLog -Property @{EventTimeStamp=(Get-Date);EventType=$logEvent;EventDetail=$logDetail}
        $LogMethod = 1
        LogProfileRemoval -LogEventDetail $LogEventDetail -LogMethod 'EventLog'
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Garen