Invoke-Command and creating local files on remote computers

%3CLINGO-SUB%20id%3D%22lingo-sub-3051461%22%20slang%3D%22en-US%22%3EInvoke-Command%20and%20creating%20local%20files%20on%20remote%20computers%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3051461%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20using%20Invoke-Command%20to%20execute%20commands%20on%20remote%20computers.%26nbsp%3B%20Throughout%20the%20course%20of%20the%20commands%2C%20I%20would%20like%20to%20generate%20logs%20locally%20on%20the%20remote%20computers.%26nbsp%3B%20The%20commands%20that%20perform%20the%20actions%20I%20need%20work%20fine%3B%26nbsp%3B%20however%2C%20the%20commands%20that%20I%20have%20to%20generate%20the%20logs%20are%20not.%26nbsp%3B%20Any%20help%20is%20appreciated.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ScriptBlock%20%3D%20%7B%0A%20%20%20%20function%20Get-TimeStampLocal%20%7B%0A%20return%20%22%5B%7B0%3AMM%2Fdd%2Fyy%7D%20%7B0%3AHH%3Amm%3Ass%7D%5D%22%20-f%20(Get-Date)%0A%20%7D%0A%24Logfile%20%3D%20%22c%3A%5Cimage%5CRemedComplete.txt%22%0A%23Check%20for%20and%20delete%20logfile.%0AIf%20(Test-Path%20%24Logfile)%7B%0A%20Remove-Item%20%24Logfile%0A%20%7D%20%20else%20%7B%0A%20%20%20%20%7D%0AWrite-Output%20%22%24(Get-TimeStamp)%20Beginning%20application%20Remediation.%22%20%20%26gt%3B%26gt%3B%20%24Logfile%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0AWrite-Output%20%22%24(Get-TimeStamp)Beginning%20System%20remediation%20for%20%22%20%26gt%3B%26gt%3B%20%24Logfile%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0A%26lt%3B%20Commands%20that%20work%20%26gt%3B%0AWrite-Output%20%22%24(Get-TimeStamp)Remediation%20Complete%22%20%26gt%3B%26gt%3B%20%24Logfile%0A%7D%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%0AInvoke-Command%20-ComputerName%20%24vm%20-ScriptBlock%20%24ScriptBlock%20-AsJob%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3051461%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New 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?