function not recognizable inside scriptblock in PowerShell script

23 views Asked by At

I am writing scripts to automate installing server on my test machines that will be executed every day and I am using PowerShell to do that.

I have my file gecommon.psd1 where I have all my functions defined. That Module is imported before every step will be executed.

For example:

  1. restore database step (script will use Import-Module .\modules\GenerateEnvironment\gecommon.psd1 -Force)

  2. install server step (script will use Import-Module .\modules\GenerateEnvironment\gecommon.psd1 -Force)

and so one...

In that script I have defined logging functions such as WriteDebugLog

function WriteDebugLog {
    param(
        [string]$message
    )
    Write-DebugLog $message
}

and when I execute my script which uses function RemoveServerIfExists

function RemoveServerIfExists {
    param (
        [string]$serverToCheck
    )
    WriteDebugLog("Checking if Server is already installed on $serverToCheck...")

    $checkServer = {
        $installedServer = gp HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | where { $_.DisplayName -like 'Server*' }
        if ($installedServer) {
            WriteDebugLog("Uninstalling Server on $using:serverToCheck...")
            $installedServer | foreach { & msiexec /x $_.PSChildName /passive | Out-Null }
            return "Uninstalled " + $installedServer.DisplayVersion
        } else {
            return "Not found"
        }
    }
    
    $result = Invoke-Command -ComputerName $serverToCheck -ScriptBlock $checkServer
    if(-not ($result -eq "")) {
        WriteLog("Server on $serverToCheck $result")
    }
}

first called function WriteDebugLog("Checking if Server is already installed on $serverToCheck...") works properly,

but second one which is inside script block

WriteDebugLog("Uninstalling Server on $using:serverToCheck...")

gives me an error:

The term 'WriteDebugLog' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (WriteDebugLog:String) [], 

What is wrong here?

0

There are 0 answers