Is it possible to save an expression such as this.
Out-File -FilePath "C:\StartupInfo.txt" -Append
To variable, I was hoping to do something like this
$logfile = Out-File -FilePath "C:\StartupInfo.txt" -Appened
I would use the variable to log the work in a text file that is done through out the script.
Write-OutPut "$ComputerName work completed." | $logFile
Related
My purpose is to write some string into a file, and the file path is something like %SystemDrive%\temp.txt. The pipeline looks like
Write-Output "test" | Out-File -FilePath %SystemDrive%\temp.txt
I can get %SystemDrive% by (Get-ChildItem -Path Env:\SystemDrive).Value, but how can I put it into the pipeline?
You could use Join-Path wrapped in parenthesis to set the FilePath for Out-File.
Write-Output "test" | Out-File -FilePath (Join-Path -Path (Get-ChildItem -Path Env:\SystemDrive).Value -ChildPath "\temp.txt")
The path that this yields on my system is:
C:\temp.txt
You can add the -WhatIf switch to the end of the command to see that it works, without actually writing the file.
I'd opt for creating a temp folder on C then output the txt file to that folder.
Get-"something"| Out-File -FilePath C:\temp\temp.txt
I have some console outputs which also are logged in a txt file.
Write-Host "$Time : $file failed/skipped. Technical information: $ExceptionMessage" | Out-file -FilePath $logPath -Append
Now I want that this message appears in red on the cmd console. So I tried this with -ForegroundColor but it did not work out:
Write-Host "$Time : $file failed/skipped. Technical information: $ExceptionMessage" -ForegroundColor Red | Out-file -FilePath $logPath -Append
It seems like Out-File das not like the -ForegroundColor property.
Is there a way to log console output which is also colored?
I am using Write-Host without Out-File now but with Start Transcript at begin of my code. Working fine now.
I am writing a PowerShell script to gather general information on our servers. I wrote the script so that it outputs to a file called output.txt via PowerShells Start-Transcript cmdlet. Output works fine. However I just want the output in the file and not displayed on the console.
I have been looking and attempting to see if Start-Transcription can suppress the console output but I have not found anything.
This is a very cut down version of the code I am using-
Start-Transcript -path "Path\To\Output\File.txt"
$servers = Get-Content -path "Path\To\Servers\List\file.txt"
foreach ($server in $servers)
{
net view
net use
net session
}
Stop-Transcript
It all outputs to the file correctly but I just would like it to NOT display the script/command results in the console. If that is possible.
Would this work?
$FilePath = 'Path\To\Output\File.txt'
net view | Out-File -FilePath $FilePath
net use | Out-File -FilePath $FilePath -NoClobber -Append
net session | Out-File -FilePath $FilePath -NoClobber -Append
Or bundle it:
Invoke-Command {net view ; net use ; net session} | Out-File -FilePath $FilePath -NoClobber -Append
EDIT based on comment (but written freely from memory on an iphone so maybe minor mistakes):
To run this remotely against a list of servers you first enable Powershell remoting on the servers, many ways to do it and here is one to run in a local powershell session on each server (Runas Admin):
winrm quickconfig
Then, assuming they all have the same login, you can:
$Cred = Get-Credential
$Servers = ’server1.example.com’,’server2.example.com’
Invoke-Command -ComputerNames $Servers -Credential $Cred -ScriptBlock {
Do stuff
Do some other stuff
} | Out-File -FilePath $FilePath -NoClobber -Append
Results are returned as an array so if you want to separate the output per server you could try:
$a = Invoke-Command [...]etc but skip the |Out-File
then do some loop which in essence does this part, giving you the manual way here:
$a[0] | Out-File -FilePath $FilePath1 -NoClobber -Append #result from first computer
$a[1] | Out-File -FilePath $FilePath2 -NoClobber -Append #result from second computer
...
and an example loop:
$a | Foreach-Object {$_ | Out-File -FilePath $Path -Append -NoClobber}
And to read the servernames from a text file, one servername per line:
$Servers = Get-Content -Path ’C:\temp\example.txt’
Invoke-Command -ComputerName $Servers [...]etc
When I search this site, others have suggested using options like -NoNewWindow However, that doesnt seem to help.
I am writing a script that installs SCCM. It is quite a long install (about 5 min) and even though I have the -Wait, it literally continues on throughout the remainder of the script.
Start-Process -FilePath C:\temp\SCCM_Client\ccmsetup.exe -ArgumentList SMSSITECODE=PCM -Wait
Write-Host "Verify SCCM client install after reboot"`r`n -ForegroundColor Green
It runs the ccmsetup.exe then after about 5 seconds or so, it continues on to the next line. I check task manager and ccmsetup.exe is CLEARLY still running.
So you guys think Im having a problem because this is an installer and not a program? (this command works just fine if I Start-process notepad.exe; it wont continue on until I close notepad) That's the only thing I can think of that's different
Thanks for any help!
I had this problem, ccmsetup.exe spawns another process which does the work. So it's behaving as expected because the ccmsetup.exe spawned by powershell has finished.
To get around this, my solution was to monitor the ccmsetup logs for an exit code.
Something like below.
$count = 1
$LogFilePath = 'C:\Windows\ccmsetup\Logs\ccmsetup.log'
do
{
Write-Output "Uninstalling Software Center - attempt $count"
Start-Process -FilePath C:\temp\SCCM_Client\ccmsetup.exe -ArgumentList SMSSITECODE=PCM
$count++
Start-Sleep 30
while ((Get-Content -Path $LogFilePath -Tail 1 | Select-String -Pattern "CcmSetup is exiting with return code" -SimpleMatch) -eq $null)
{
#Check every 10 seconds for an exit code
Start-Sleep 10
}
} until((Get-Content $LogFilePath -Tail 1 -Wait | Select-String -pattern "CcmSetup is exiting with return code 0" -SimpleMatch -Quiet) -or $count -gt 3)
I'm not in work at the moment, so don't have access to the actual portion of code - I'll try to update this tomorrow with the final version I used.
The Start-sleep 30 was required to prevent it checking the log prematurely and using an old exit code.
My solution to this particular problem was to use SCCM to build a batch file that actually works and runs separately from SCCM. It's an utterly insane solution.. but welcome to coding, I guess.
Write-Output "MsiExec.exe /x{5974413A-8D95-4D64-B9EE-40DF28186445} /qn" | Out-File -Encoding ASCII -FilePath mcafee-removal.bat
Write-Output "MsiExec.exe /x{377DA1C7-79DE-4102-8DB7-5C2296A3E960} /qn" | Out-File -Encoding ASCII -FilePath mcafee-removal.bat -Append
Write-Output "MsiExec.exe /x{820D7600-089E-486B-860F-279B8119A893} /qn" | Out-File -Encoding ASCII -FilePath mcafee-removal.bat -Append
Write-Output "MsiExec.exe /x{B16DE18D-4D5D-45F8-92BD-8DC17225AFD8} /qn" | Out-File -Encoding ASCII -FilePath mcafee-removal.bat -Append
Write-Output """%programfiles%\McAfee\Agent\x86\frminst.exe"" /remove=agent /silent" | Out-File -Encoding ASCII -FilePath mcafee-removal.bat -Append
Start-Process -FilePath "cmd.exe" -ArgumentList '/c .\mcafee-removal.bat'
The batch file behaves exactly the way you'd expect it to. SCCM's weird C++ implementation of powershell parsing ... yeah, not so much.
Hope this helps someone someday.
The following line of my code simply does not write to file the Out-File command:
Move-Item $item.Path $CaminhoCompleto -Force -WhatIf -Verbose |
Out-File -Filepath $SaidaTXT -Append -NoClobber
On the screen it shows correctly, but the file is empty.
-WhatIf messages are written directly to the console and can't be piped or redirected without running the statement in a different PowerShell process. You can capture the output with Start-Transcript, though.
Start-Transcript -Path $SaidaTXT -Append
Move-Item ...
Stop-Transcript