I have written a custom Write-Log Function. If I use the Write-Log function inside the Invoke, I am getting below error message "Error on remote execution: The term 'Write-Log' 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."
I'm using Powershell 5.1. I did try to use ${function:Write-Log} but it didn't work.
Function Write-Log([string]$ComputerList,$message, $level="INFO") {
$date_stamp = Get-Date -Format s
$log_entry = "$date_stamp - $level - $message"
$log_file = "$tmp_dir\upgrade_powershell.log"
Write-Verbose -Message $log_entry
Add-Content -Path $log_file -Value $log_entry
}
Function Start-Process ($ComputerList) {
Return Invoke-Command -computername $Computer -ScriptBlock {
Param($file)
$Application = $args[0]
$ApplicationName = $Application.Substring($Application.LastIndexOf('\')+1)
$ApplicationFolderPath = $Application.Substring(0,$Application.LastIndexOf('\'))
$ApplicationExt = $Application.Substring($Application.LastIndexOf('.')+1)
Write-Log -message "Installing $file on $($env:COMPUTERNAME)"
$p = Start-Process $file -Wait -Passthru
$p.WaitForExit()
$p.WaitForExit()
if ($p.ExitCode -ne 0) {
Write-Log -message "Failed installing with error code $($p.ExitCode)" -level "ERROR"
$Return = $($env:COMPUTERNAME)
}
else{
$Return = 0
if ($p.ExitCode -ne 0 -and $p.ExitCode -ne 3010) {
$log_msg = "$($error_msg): exit code $p.ExitCode"
Write-Log -message $log_msg -level "ERROR"
#throw $log_msg
return
}
if ($p.ExitCode-eq 3010) {
Reboot-AndResume
break
}}
}
}