Start-Transcript: This host does not support transcription - powershell

I want to start a transcript on a Windows Server 2008 R2
Start-Transcript -path C:\Temp\test.txt
"Hello!"
Stop-Transcript
But PowerShell returns the following error message:
Start-Transcript : This host does not support transcription.
How it is possible to activate transcript?

Windows PowerShell v4 ISE and lower do not support transcription. You must use the command line to run the commandlet.
From PowerShell v5 Start-Transcript is supported natively in ISE.

COMPLETE ANSWER (PowerShell ISE 2.0/4.0)::
Having yet another look at this today on another server, I noticed that the latest PowerShell ISE (which also does not allow Start-Transcript) does not have an Output pane, and instead uses the new ConsolePane. Thus the function now is as follows:
Function Start-iseTranscript
{
Param(
[string]$logname = (Get-logNameFromDate -path "C:\fso" -postfix " $(hostname)" -Create)
)
$transcriptHeader = #"
**************************************
Windows PowerShell ISE Transcript Start
Start Time: $((get-date).ToString('yyyyMMddhhmmss'))
UserName: $env:username
UserDomain: $env:USERDNSDOMAIN
ComputerName: $env:COMPUTERNAME
Windows version: $((Get-WmiObject win32_operatingsystem).version)
**************************************
Transcript started. Output file is $logname
"#
$transcriptHeader >> $logname
$psISE.CurrentPowerShellTab.Output.Text >> $logname
#Keep current Prompt
if ($Global:__promptDef -eq $null)
{
$Global:__promptDef = (gci Function:Prompt).Definition
$promptDef = (gci Function:Prompt).Definition
} else
{
$promptDef = $Global:__promptDef
}
$newPromptDef = #'
if ($Host.Version.Major -eq 2)
{
if ($Global:_LastText -ne $psISE.CurrentPowerShellTab.Output.Text)
{
Compare-Object -ReferenceObject ($Global:_LastText.Split("`n")) -DifferenceObject ($psISE.CurrentPowerShellTab.Output.Text.Split("`n"))|?{$_.SideIndicator -eq "=>"}|%{
$_.InputObject.TrimEnd()}|Out-File -FilePath ($Global:_DSTranscript) -Append
$Global:_LastText = $psISE.CurrentPowerShellTab.Output.Text
}
} elseif ($Host.Version.Major -eq 4)
{
if ($Global:_LastText -ne $psISE.CurrentPowerShellTab.ConsolePane.Text)
{
Compare-Object -ReferenceObject ($Global:_LastText.Split("`n")) -DifferenceObject ($psISE.CurrentPowerShellTab.ConsolePane.Text.Split("`n"))|?{$_.SideIndicator -eq "=>"}|%{
$_.InputObject.TrimEnd()}|Out-File -FilePath ($Global:_DSTranscript) -Append
$Global:_LastText = $psISE.CurrentPowerShellTab.ConsolePane.Text
}
}
'# + $promptDef
$Global:_LastText = $psISE.CurrentPowerShellTab.Output.Text
New-Item -Path Function: -Name "Global:Prompt" -Value ([ScriptBlock]::Create($newPromptDef)) -Force|Out-Null
}
Taking over the prompt is incredibly useful for this, however keeping two copies of the Output buffer is not ideal. I've also added in TrimEnd() as PSISE 2.0 likes to append spaces to fill the entire horizontal line width. Not sure if PSISE 4.0 does this too, but it's no problem now anyway.
NEW ANSWER (PowerShell ISE 2.0)::
I have just recently returned to this problem, and there is a way of forcing every update in PowerShell ISE to log out as a command is executed. This relies on the log path being saved in a Global Variable called _DSTranscript. This variable is passed to the Start-iseTranscript function. I have then Hijacked the Prompt function to execute a compare between _LastText and the hostUI Output Text, and append the differences out to the log. It now works a treat.
Function Start-iseTranscript
{
Param(
[string]$logname = (Get-logNameFromDate -path "C:\fso" -postfix " $(hostname)" -Create)
)
$transcriptHeader = #"
**************************************
Windows PowerShell ISE Transcript Start
Start Time: $(get-date)
UserName: $env:username
UserDomain: $env:USERDNSDOMAIN
ComputerName: $env:COMPUTERNAME
Windows version: $((Get-WmiObject win32_operatingsystem).version)
**************************************
Transcript started. Output file is $logname
"#
$transcriptHeader >> $logname
$psISE.CurrentPowerShellTab.Output.Text >> $logname
#Keep current Prompt
if ($__promptDef -eq $null)
{
$__promptDef = (gci Function:Prompt).Definition
$promptDef = (gci Function:Prompt).Definition
} else
{
$promptDef = $__promptDef
}
$newPromptDef = #'
if ($global:_LastText -ne $psISE.CurrentPowerShellTab.Output.Text)
{
Compare-Object -ReferenceObject $global:_LastText.Split("`n") -DifferenceObject $psISE.CurrentPowerShellTab.Output.Text.Split("`n")|?{$_.SideIndicator -eq "=>"}|%{ $_.InputObject.TrimEnd()}|Out-File -FilePath ($Global:_DSTranscript) -Append
$global:_LastText = $psISE.CurrentPowerShellTab.Output.Text
}
'# + $promptDef
New-Item -Path Function: -Name "Global:Prompt" -Value ([ScriptBlock]::Create($newPromptDef)) -Force|Out-Null
}
ORIGINAL ANSWER::
PowerShell ISE does not natively support Transcription. There is a Scripting Guy blog about how to achieve this. Unfortunately this needs to be the last thing that is run in the script. This means that you need to remember to run it before closing the window. I wish this worked better, or there was a way to force it to run on window closure.
This is the function that produces close to the same result as the Start-Transcript feature:
Function Start-iseTranscript
{
Param(
[string]$logname = (Get-logNameFromDate -path "C:\fso" -name "log" -Create)
)
$transcriptHeader = #"
**************************************
Windows PowerShell ISE Transcript Start
Start Time: $(get-date)
UserName: $env:username
UserDomain: $env:USERDNSDOMAIN
ComputerName: $env:COMPUTERNAME
Windows version: $((Get-WmiObject win32_operatingsystem).version)
**************************************
Transcript started. Output file is $logname
"#
$transcriptHeader >> $logname
$psISE.CurrentPowerShellTab.Output.Text >> $logname
} #end function start-iseTranscript

Either accept you can't, or use a host that does support transcripts (like the console host: PowerShell.exe).

The powershell.exe will also generate this error if there is a problem writing to the log file. For example, if the log file was created by an administrator and the user doesn't have permissions to overwrite the log.
Start-Transcript : The host is not currently transcribing.
At D:\Test1.ps1:9 char:1
+ Start-Transcript -Path "$Source\logs\Test.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StartTranscriptCommand
A good solution is to try using -Append or to make the log file unique by generating a date/time stamp.
Start-Transcript -Path "$Source\logs\Test.txt" -Append
This way a proper error message is generated.
Access to the path 'D:\Test\logs\Test.txt' is denied.
-Force has the same effect as -Append and will generate a permissions error.

Following the tip from #richard here I created a snippet that allows usage of Transaction logs where I need them (scheduled tasks), so I ended having in Windows 2008R2 the following code that can be run from powershell ISE or as a standalone script.
When run in ISE, the log information will be printed on screen
When run as a script, the log information will be saved to a file
if ($Host.Name -eq "Windows PowerShell ISE Host") {
$ISE=$true
} else {
$ISE=$false
}
if (-Not $ISE) {
$Date = Get-Date -f HHmmss_ddyyyy
Start-Transcript -Path "C:\Temp\$Date.log"
}
//////////
code here ...
//////////
if (-Not $ISE) {
Stop-Transcript
}

Tagging on to the amazing answer and work by #dwarfsoft:
if ($Host.Name -match 'ISE' -and $Host.version.Major -lt 4)
{
#Start-Transcript will not work here. Use Start-iseTranscript by #dwarfsoft above
Start-iseTranscript
}
else
{
#Start Transcript Will work here
Start-Transcript
}

In addition to the ISE (which I note the original poster LaPhi doesn't even mention), the other thing that can cause this error is if you're trying to use Start-Transcript within an Invoke-Command scriptblock. For instance if you're running the script on your client machine, and then connecting to the Windows Server 2008 R2 box via Invoke-Command so Start-Transcript outputs to the server.
When run in the local session Start-Transcript works as expected, however when using Invoke-Command the script is run within a remote session on that computer, and remote sessions have certain limitations, one of which is not supporting transcription.

Related

How can I send a node of xml to a script in Powershell?

I am making a powershell script that will be a shecdualed task on the server and that performs following things:
look in the export folder if there are export files older than 14 days and remove them ifso.
Calculated the date of yesterday and place it in a variable
Get a script, load it into a variable and adapt some arguments in it.
Extract a node out of the script into a variable and use it in the Cterm.exe application that starts afterwards.
Everything works, except the last step. The Cterm program starts, but asks a username and password. This is specified in the last variable with node of adapted script. It seems like he don't get that information and gets stuck. I want to send the content of the node to that last variable, but I get an error.
This is the error when I perform the code stepwise and another PowerShell session:
Method invocation failed because [System.String] does not contain a method named 'SelectSingleNode'.
At line:1 char:1
+ $inputscript = $tempfile.SelectSingleNode("//Text")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
This is the complete script:
#Place arguments in defined variables:
$filelocation="C:\temp" #E on the servers
$exportdestination="C:\temp"
$tempfile= ("D:\temp\temp-file.txt")
Write-Output $filelocation
$instanceName="GCS"
#Check if there are files (in the output folder) older then 14 days and remove them:
$limit = (Get-Date).AddDays(-15)
# Delete files older than the $limit.
$removedfile= Get-ChildItem -Path $exportdestination -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }
try {
$removedfile | remove-Item -Force
}
catch {
{Write-Output "error: Not all the files could be removed!"}
}
#Export the file with the data of yesterday using the RAT script:
$yesterday=(Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
write-output "hello world"
$script= [xml](Get-Content C:\CTWS_export_infinity_v1.6.04.dms)
write-output "hello world"
$script -Replace ':startDate', $yesterday -Replace ':endDate', $yesterday -Replace ':filename', ($exportdestination+"\"+$instanceName)| Out-File -FilePath $tempfile
write-output "hello world"
#| Set-Content C:\CTWS_export_infinity_v1.6.04_$yesterday.dms
#$scripttemp = C:\CTWS_export_infinity_v1.6.04_$yesterday.dms
#Start-Process -FilePath "D:\CobasInfinity\HealthShare\bin\CTerm.exe" -ArgumentList #("/console=cn_ap:$instanceName $filelocation\temp-file.txt _SYSTEM INFINITY") -wait
$inputscript = $tempfile.SelectSingleNode("//Text")
& "D:\CobasInfinity\HealthShare\bin\CTerm.exe" /console=cn_ap:$instanceName $inputscript
#Remove-Item -Path $tempfile
How can I solve that final error?
I tried to save the content as an xml in the variable,
I tried to select one node of that variable,
I used 'hello world' prints to follow the progress of the script,
I tried to check the script by doing it step by step in another PowerShell session to see where to problem is.
How can I solve that final error?

Get-WindowsUpdateLog stream re-direction

Has anyone noticed how the Get-WindowsUpdateLog cmdlet cannot be redirected to any streams?
Furthermore, storing the output into a variable, piping it, or any type of re-direction leads to the cmdlet to only be executed.
Any help redirecting/silencing the output of this command would be appreciated.
What I've tried:
Get-WindowsUpdateLog | Out-Null
Get-WindowsUpdateLog > $null
$sink = Get-WindowsUpdateLog
Everything I could find failed to suppress the output of the CmdLet Get-WindowsUpdateLog. As you say, the displayed information in the console is not properly following the output streams as we know them in PowerShell.
The only workaround I found is using Jobs:
$Job = Start-Job -ScriptBlock {Get-WindowsUpdateLog}
$Job | Wait-Job | Remove-Job
This way all output is handled within the job and we don't retrieve the result. It's also unnecessary to retrieve it as the result is simply a text file placed in the -Path parameter.
As an addendum to DarkLite's answer, we can also utilise the following code to check if Get-WindowsUpdateLog cmdlet worked properly :
# Generate a unique filename.
$o_filename = "C:\temp\" + "WindowsUpdateLog" + "_" + ( Get-Date -UFormat "%H_%M_%S" ) + ".txt"
$o_job = Start-Job -ScriptBlock { Get-WindowsUpdateLog -LogPath "$( $args[0] )" } `
-ArgumentList $o_filename -ErrorAction SilentlyContinue
Start-Sleep -Seconds 5
$o_job | Remove-Job -ErrorAction SilentlyContinue
if( ! ( Test-Path $o_filename ) ) {
# Return an exception
}
else {
# Do some work on the generated log file
}
I think the lesson here is don't use Out-Default in a script. https://keithga.wordpress.com/2018/04/03/out-default-considered-harmful/ I don't see that command in powershell 6. But it is in powershell 7!

Copy-Item from USB Does Not Work as a Job

I'm copying files to a USB utilizing the path of the USB (assuming one drive plugged in) rather than the drive letter. I need to do it this way since I'm using this to clone 52 USBs at once.
This is where the prefix for the path is found:
$usb = Get-Volume | where {$_.DriveType -eq "Removable" }
$usb.Path
Which when concatenated with the rest of the file name becomes something like:
\\?\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\RDC_2.1.1_ALL.dmg
When I call Copy-Item outside of a job everything works and copies properly. When I call Copy-Item within a PowerShell job it fails and complains about the path. This doesn't make any sense to me.
Can someone tell me why my function copyFilesSync works properly, while copyFiles doesn't work?
For simplicity, I have cut the script back to just the elements that reproduce this issue. There is no practical difference between $filestoCopyJSON1 and $filestoCopyJSON2. Swapping the actual files doesn't make a difference. I just needed two file names so I could run the two functions back-to-back and they were not copying the same file:
$filestoCopyJSON1 = '[
{
"FromFile": "C:\\dev\\VMware-player-12.1.1-3770994.exe",
"ToFile": "\\\\?\\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\\VMware-player-12.1.1-3770994.exe"
}
]'
$filestoCopyJSON2 = '[
{
"FromFile": "C:\\dev\\RDC_2.1.1_ALL.dmg",
"ToFile": "\\\\?\\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\\RDC_2.1.1_ALL.dmg"
}
]'
function copyFiles($filestoCopy){
Write-Host "Copying $($filestoCopy.Count)"
$copyblock = {
$copy = $args[0]
Write-Host "$($copy.FromFile)" "$($copy.ToFile)"
Copy-Item "$($copy.FromFile)" "$($copy.ToFile)"
$resultHash = Get-FileHash -LiteralPath "$($copy.ToFile)"
}
foreach ($filetoCopy in $filestoCopy) {
Start-Job -ScriptBlock $copyblock -ArgumentList #($filetoCopy)
}
Write-Host "Waiting on copying..."
Get-Job | Wait-Job
Write-Host "Files Copied"
}
function copyFilesSync($filestoCopy){
foreach ($filetoCopy in $filestoCopy) {
Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
$resultHash = Get-FileHash -LiteralPath "$($filetoCopy.ToFile)"
}
}
Remove-Job -Name *
Write-Host "Sync File Copy"
copyFilesSync -filestoCopy $(ConvertFrom-JSON -InputObject $filestoCopyJSON1)
Write-Host "Async File Copy"
copyFiles -filestoCopy $(ConvertFrom-JSON -InputObject $filestoCopyJSON2)
Receive-Job -Name *
This results in the following output:
PS C:\WINDOWS\system32> C:\dev\Test.ps1
Sync File Copy
Async File Copy
Copying 1
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
207 Job207 BackgroundJob Running True localhost ...
Waiting on copying...
207 Job207 BackgroundJob Completed True localhost ...
Files Copied
C:\dev\RDC_2.1.1_ALL.dmg \\?\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\RDC_2.1.1_ALL.dmg
Illegal characters in path.
+ CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : localhost
The first file from the Copy-Item that's not in a job completes successfully. The async Copy-Item run through the job fails with the errors as have been seen.
I believe this is a bug in Powershell Jobs or Copy-Item. I'm not really sure which. I was able to bypass this by utilizing Background Runspaces instead of Jobs. As a result the following async code runs just fine:
function copyFiles($filestoCopy){
Write-Host "Copying $($filestoCopy.Count)"
$copyblock = {
$filestoCopy = $args[0]
foreach($filetoCopy in $filestoCopy){
Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
$resultHash = Get-FileHash -LiteralPath "$($filetoCopy.ToFile)"
if($filetoCopy.FromFileHash -ne $resultHash.hash){
Write-Host "File $($copy.ToFile) Failed.. trying again"
Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
}
}
}
$newPowerShell = [PowerShell]::Create().AddScript($copyblock).AddArgument($filestoCopy)
$job = $newPowerShell.BeginInvoke()
Write-Host "Waiting on copying..."
While (-Not $job.IsCompleted) {}
$result = $newPowerShell.EndInvoke($job)
$newPowerShell.Dispose()
Write-Host "Files Copied"
}

get-content : Cannot find path 'PATH_TO_FILE_' because it does not exsist

I am writing a PS Script to try and locate a folder on a remote server. Here is the code:
# Set file to be tested for, put everything after c:\
# “c:\Users\Default” is the example path
$filetofind = "EventLogs"
# Hostnames TXT Location
$hostnamestxt = "C:\Scripts\Powershell_Remote_File_Query\hosts.txt"
# Destination file for Online Machines
$onlinetxt = "C:\Scripts\Powershell_Remote_File_Query\Machines_with_file.txt"
# Destination file for Offline Machines
$offlinetxt = "C:\Scripts\Powershell_Remote_File_Query\Offline_Machines.txt"
##########################################################
# Begin Executing Script – Do Not Edit Below This Line
##########################################################
$computers = get-content “$hostnamestxt”
write-host “———————————————-”
write-host “Scanning hostnames from $hostnamestxt…”
write-host “———————————————-”
foreach($computer in $computers)
{
ping -n 1 $computer >$null
if($lastexitcode -eq 0)
{
if(test-path “\\$computer\c$\$filetofind”)
{
echo “$computer” | Out-File -Append “$onlinetxt”
write-host “File FOUND on $computer”
}
else
{write-host “File NOT found on $computer”}
}
else
{
echo “$computer” | Out-File -Append “$offlinetxt”
write-host “$computer is OFFLINE/DID NOT RESPOND TO PING”
}
}
write-host “———————————————-”
write-host “Script has completed please check output.”
write-host “Hosts with file output location – $onlinetxt”
write-host “Hosts that were unpingable output location – $offlinetxt”
write-host “———————————————-“
I get this error when running the script:
get-content : Cannot find path 'C:\Scripts\Powershell_Remote_File_Query\“€' because it does not exist.
At C:\Scripts\Powershell_Remote_File_Query\Powershell_Remote_File_Query_Script.ps1:24 char:18
+ $computers = get-content “$hostnamestxtâ€
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Scripts\Powe...ile_Query\“€:String) [Get-Content], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Both the script itself and the txt file are in the same directory. Please forgive me I'm new to Powershell and not a programmer by any means.
Instead of doing this...
$hostnamestxt = "C:\Scripts\Powershell_Remote_File_Query\hosts.txt"
$computers = get-content “$hostnamestxt”
Try just doing this...
$computers = get-content "C:\Scripts\Powershell_Remote_File_Query\hosts.txt"
I understand what you are trying to do should result to the same thing, yet, let's just try to eliminate that extra thing, and give it a shot to see if you get a different result.
Also, if that file you are reading is on a remote host, then you should be using the UNC file share path, not your local C: drive. Just assuming a bit here.

Pipe all Write-Output to the same Out-File in PowerShell

As the title suggests, how do you make it so all of the Write-Outputs - no matter where they appear - automatically append to your defined log file? That way the script will be nicer to read and it removes a tiny bit of work!
Little example below, id like to see none of the "| Out-File" if possible, yet have them still output to that file!
$Author = 'Max'
$Time = Get-Date -Format "HH:mm:ss.fff"
$Title = "Illegal Software Removal"
$LogName = "Illegal_Remove_$($env:COMPUTERNAME).log"
$Log = "C:\Windows\Logs\Software" + "\" + $LogName
$RemoteLog = "\\Server\Adobe Illegal Software Removal"
Set-PSBreakpoint -Variable Time -Mode Read -Action { $global:Time = Get-Date -format "HH:mm:ss.fff" } | Out-Null
If((Test-Path $Log) -eq $False){ New-Item $Log -ItemType "File" -Force | Out-Null }
Else { $Null }
"[$Time][Startup] $Title : Created by $Author" | Out-File $Log -Append
"[$Time][Startup] Configuring initial variables required before run..." | Out-File $Log -Append
EDIT: This needs to work on PS v2.0, I don't want the output to appear on screen at all only in the log. So I have the same functionality, but the script would look like so...
"[$Time][Startup] $Title : Created by $Author"
"[$Time][Startup] Configuring initial variables required before run..."
You have two options, one is to do the redirection at the point the script is invoked e.g.:
PowerShell.exe -Command "& {c:\myscript.ps1}" > c:\myscript.log
Or you can use the Start-Transcript command to record everything (except exe output) the shell sees. After the script is done call Stop-Transcript.