Invoke-Command and creating local files on remote computers

Copper Contributor

I am using Invoke-Command to execute commands on remote computers.  Throughout the course of the commands, I would like to generate logs locally on the remote computers.  The commands that perform the actions I need work fine;  however, the commands that I have to generate the logs (write-output) are not.  Any help is appreciated.

 

 

 

 

$ScriptBlock = {
    function Get-TimeStampLocal {
	return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
	}
$Logfile = "c:\image\RemedComplete.txt"
#Check for and delete logfile.
If (Test-Path $Logfile){
	Remove-Item $Logfile
	}  else {
   	}
Write-Output "$(Get-TimeStamp) Beginning application Remediation."  >> $Logfile
< Commands that work >
< Commands that work >
< Commands that work >
Write-Output "$(Get-TimeStamp)Beginning System remediation for " >> $Logfile
< Commands that work >
< Commands that work >
< Commands that work >
Write-Output "$(Get-TimeStamp)Remediation Complete" >> $Logfile
}										
			
Invoke-Command -ComputerName $vm -ScriptBlock $ScriptBlock -AsJob

 

 

 

 

2 Replies
You could use Start-Transcript $logfile and Stop-Transcript perhaps?
Did the Transcript option work for you?