why i see AppDomain & SharedPerformanceCounter instance on thread stack - .net-2.0

When I look run !dso command against a thread, I see following. What I don't understand is why do I see System.AppDomain and System.Diagnostics.SharedPerformanceCounter although in the code I am doing anything specific related to these.
0:090> !dso
OS Thread Id: 0xf74 (90)
ESP/REG Object Name
1975e3b4 0273eb28 System.Diagnostics.SharedPerformanceCounter
1975e46c 0273eb28 System.Diagnostics.SharedPerformanceCounter
1975e498 0273e4b8 System.String AppName[6276]
1975e49c 0273eb28 System.Diagnostics.SharedPerformanceCounter
1975e4a0 0273eab0 System.Diagnostics.PerformanceCounter
1975e4a4 0273e4b8 System.String AppName[6276]
1975e580 023e1320 System.AppDomain
1975e584 0273f000 System.UnhandledExceptionEventHandler
1975e714 0273f000 System.UnhandledExceptionEventHandler
1975e71c 023e1320 System.AppDomain
1975e900 0273f040 System.UnhandledExceptionEventHandler
1975e9d0 023e1320 System.AppDomain
1975ec60 203e5b9c MyNameSpace.MyClass
1975edd0 203e5b9c MyNameSpace.MyClass
1975f01c 203e5b9c MyNameSpace.MyClass
1975f03c 203e5b9c MyNameSpace.MyClass
1975f048 203fa954 System.ComponentModel.RunWorkerCompletedEventHandler
1975f04c 203f5b24 System.ComponentModel.BackgroundWorker
1975f05c 203fa930 System.ComponentModel.EventHandlerList
1975f060 0282eeec System.Threading.ContextCallback

Related

Flutter ZEBRA mobile printer ZQ521 stuck when print multiple image(ZPL)

Im developing a Flutter app that need to print selected image with mobile printer (ZEBRA ZQ521).
Generated ZPL string are works on http://labelary.com/viewer.html.
But mobile printer (ZQ521) totally stuck when printing (sometimes goes smooth).
error : Caused by: java.io.IOException: Bluetooth LE Write timed out
is it my ZPL string are too long? please help.
my zpl stringļ¼š
^XA
~DGimg5.GRF,5126,22, FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFE7FFFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFFCFC78607FFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFF0FFCB303FFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFF3FFC33807FFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFE06338C803FFFFFC7FFFFFFFE
FFFFFFFFFFFFFFFFFFFE7FF3CFE01FFFFFE1FFFFFFFE
FFFFFFFFFFFFFFFFFFFC380FFFC01FFFFFF1FFFFFFFE
FFFFFFFFFFFFFFFFFFFCF338FFE00FFFFFF87FFFFFFE
FFFFFFFFFFFFFFFFFFFFFBF03FFFFFFFFFFC3FFFFFFE
FFFFFFFFFFFFFFFFFFC1F1F03FFFFFFFFFFC1FFFFFFE
FFFFFFFFFFFFFFFFFC000000187C19FFFFF80FFFFFFE
FFFFFFFFFFFFFFC000000000000000001CFFFFFFFFFE
FFFFFFFFFFFFFEC00000000000000000000FFFFFFFFE
FFFFFFFFFFFFFF80000000000000000000007FFFFFFE
FFFFFFFFFFFFE0000000000000000000000003FFFFFE
FFFFFFFFFFFFF80000000000000000000000007FFFFE
FFFFFFFFFFFFFFF000000000000000000000007FFFFE
FFFFFFFFFFFFFFE000000000000000000000003FFFFE
FFFFFFFFFFFFFFC000000000000000000000003FFFFE
FFFFFFFFFFFFFF8000000000000000000000003FFFFE
FFFFFFFFFFFFF80000000000000000000000007FFFFE
FFFFFFFFFFFFFFFF00000000000000000000007FFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC000007FFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF50007FFFFE
FFFFFFFFFFFFFFFF8100000000000FE7FFFFF07FFFFE
FFFFFFFFFFFF80000000000000000400000407FFFFFE
FFFFFFFFFFC000040300000000000C00000000FFFFFE
FFFFFFFFFFE1000006000000000000000000007FFFFE
FFFFFFFFFFC0000404400000000008000000007FFFFE
FFFFFFFFFFC0000000D80000000000000000003FFFFE
FFFFFFFFFFC0000000F80000000000000000003FFFFE
FFFFFFFFFFC91000007A0000000000000000003FFFFE
FFFFFFFFFFC91000009E0000000000000000003FFFFE
FFFFFFFFFFCF0000009E0000000000000000003FFFFE
FFFFFFFFFFCF000001B60000000000000000001FFFFE
FFFFFFFFFFCF000000970000080001000000001FFFFE
FFFFFFFFFFEF2000029F0000080001000000001FFFFE
FFFFFFFFFFFF2000039F0000080001001000001FFFFE
FFFFFFFFFFFEA00003530000080001003000001FFFFE
FFFFFFFFFFFF2000061B8000080009001800001FFFFE
FFFFFFFFFFEF2000060BA000080008001800001FFFFE
FFFFFFFFFFFF200006038000080008001800001FFFFE
FFFFFFFFFFFFA00002038000080008000000001FFFFE
FFFFFFFFFFFFE00000038000080005000000001FFFFE
FFFFFFFFFFFFE00000438000080005000000001FFFFE
FFFFFFFFFFFFE00000618040280025000000001FFFFE
FFFFFFFFFFFFE0000047B040280025C00000001FFFFE
FFFFFFFFFFFFE40001251040280025000000001FFFFE
FFFFFFFFFFFFE40001A51040280025000000001FFFFE
FFFFFFFFFFFFE40001A53040388024000000003FFFFE
FFFFFFFFFFFFE40005A41040388004000000003FFFFE
FFFFFFFFFFFFF00007A41040388004000000003FFFFE
FFFFFFFFFFFFF00007BD1040348004600000003FFFFE
FFFFFFFFFFFFFE0007BDBB4064801760000001FFFFFE
FFFFFFFFFFFFFF900735BFC0648ED778000083FFFFFE
FFFFFFFFFFFFF7FFFFFFFFC06880877FFEFFDFFFFFFE
FFFFFFFFFFFFFFF803FFFFFFFFFFFFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFE
FFFFF8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE07FFFFE
FFFFFFE3FFFFFFFFFFFFFFFFFFFFFFFFFFFD007FFFFE
FFFFFC7FFFFFFF83EFFFFFFFFFFFFFFFD911017FFFFE
FFF017FFFFFFFF8007FFFFFE75FFFFFFC201007FFFFE
FFFFFFFFFFFFFF0003FFFF581429FFFFC200007FFFFE
FFFFFFFFFFFFFF8003F87700102BFFFFC200007FFFFE
FFFFFFFFFFFFFFD803F033001021FFFEC20200FFFFFE
FFFFFFFFFFCFFF8003FF3B001001FFFEC20700FFFFFE
FFFFFFFFF80FFFD803FF3F10100FFFFEC20300FFFFFE
FFFFFFFF000FFFFF03F89F101001FFFF431100FFFFFE
FFFFFFF00007FFF803F89E50300BFFFF63B900FFFFFE
FFFFFC000007FFFD63F91F10210FFFFDD3BB01FFFFFE
FFFF00000007FFFFEBF81E00210BFFFDD3FF05FFFFFE
FFE000000007FFFFEBF89E00210FFFFFD3FE05FFFFFE
F00000000007FFFFE3FD9E00210FFFFFFFFE0DFFFFFE
000000000007FFFEE7FF9E50210FFFFFFFFE0BFFFFFE
000000000007FFFFEFFF9E50210FFFFFFFFE0BFFFFFE
000000000007FFFFF7FF9E50210FFFFFA7FF1BFFFFFE
000000000007FFFFF7DF9E40210FFFFFB7FF1BFFFFFE
000000000007FFFFEBFDAC20200FFFFFFBFE57FFFFFE
000000000007FFFFFBFDEC22200FFFFFFBFF7FFFFFFE
000000000007FFFFFFF5FC22200FFFFFFFFFFFFFFFFE
000000000007FFFFFFF5FC62201FFFFFFFFFFFFFFFFE
000000000007FFFFFFF5FC62201FFFFFFFFEFFFFFFFE
000000000003FFFFFFFDFC0A205FFFFFFFFEFFFFFFFE
000000000003FFFFFFFDFC4A205FFFFFFFFFFFFFFFFE
000000000003FFFFFFFDFDE2207FFFFFFFFFFFFFFFFE
000000000007FFFFFFFFEEDA606FFFFFDFFDDFFFFFFE
000000000003FFFFFFFFFEDA603FFFFFFFFFFFFFFFFE
000000000003FFFFFFFFFFDA603FFFFFFFFFFFFFFFFE
000000000003FFFFFFFFFFDA603FFFFFFFFFFFFFFFFE
000000000003FFFFFFFFDFDA603FFFFFBFFFFFFFFFFE
000000000003FFFFFFFFFEDA629FFFFFFFFFFFFFFFFE
000000000003FFFFFFFFFEDA46DFFFFFFFF7FFFFFFFE
000000000003FFFFFFFFFFFA67DFFFFFFFF7FFFFFFFE
000000000003FFFFFFFFFFFC57FFFFFFFFFFFFFFFFFE
000000000003FFFFFFFFFFFED7FFFFFFFFFFFFFFFFFE
000000000003FFFFFFFFFFFFD3FFFFFFFFEFFFFFFFFE
000000000001FFFFFFFFFFFFD3FFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFD77FFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFE5F7FFFFFFFBFFFFFFFFE
000000000001FFFFFFFFFFFE5DFFFFFFFFBFFFFFFFFE
000000000001FFFFFFFFFFFFC9FFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFD5FFFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFDFFFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFCFFFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFCFFFFFFFFEFFFFFFFFFE
000000000001FFFFFFFFFFFFCF7FFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFDFFFFFFFFFFFFFFFFFFE
000000000001FFFFFFFFFFFFDFFFFFFFFFFFFFFFFFFE
000000000000FFFFFFFFFFFFDFFFFFFFFFFFFFFFFFFE
000000000000FFFFFFFFFFFFDBFFFFFFFFF07FFFFFFE
000000000000FFFFFFFFFFFFFFFFFFFFFFF007FFFFFE
000000000000FFFFFFFFFFFFFFFFFFFFFFE0003FFFFE
000000000000FFFFFFFFFFFFFFFFFFFFFF800003FFFE
000000000000FFFFFFFFFFFFFFFFFFFFFF8000003FFE
000000000000FFFFFFFFFFFFFFFFFFFFFF00000003FE
000000000000FFFFFFFFFFFFFFFFFFFFFE00000003FE
000000000000FFFFFFFFFFFFFFFFFFFFF800000003FE
000000000000FFFFFFFFFFFFFFFFFFFFE400000003FE
000000000000FFFFFFFFFFFFFFFFFFFFE000000003FE
000000000000FFFFFFFFFFFFFFFFFFFFC000000007FE
000000000000FFFFFFFFFFFFFFFFFFFFC000000007FE
000000000000FFFFFFFFFFFFFFFFFFFF8000000007FE
000000000000FFFFFFFFFFFFFFFFFFFF0000000027FE
000000000000FFFFFFFFFFFFFFFFFFFE0000000027FE
000000000000FFFFFFFFFFFFFFFFFFF60000000067FE
0000000000007F07FFFFFFFFFFFFFFFC00000000FFFE
0000000000007F03FFFFFFFFFFFFFFF0000000009FFE
0000000000007E00FE7EFFFFFFFFFFE0000000019FFE
0000000000007C00387F7FFFFFFFFF0000000000BFFE
0000000000007C00001FEFFFFFFFFE00000000027FFE
0000000000007C000003FFFFFFFFF000000000067FFE
0000000000007C000000FFFFFFFF8000000000007FFE
000000000000780000001FFFFFF8000000000000FFFE
0000000000007000000007FFFE20000000000002FFFE
0000000000007200000000FE0000000000000003FFFE
00000000000060000000043F0000000000000003FFFE
000000000001F20000001C0FE000000000000007FFFE
000000000001CF0000007081FC00000000000007FFFE
000000000006BF600000E0007F0000000000000FFFFE
000000000006EFF0000300000FE000000000001FFFFE
0000000000017FFC0006000003F800000000001FFFFE
000000000003FFFF00180000087E00000000003FFFFE
0000000000017FFB80600000000F80000000003FFFFE
0000000000005FFA00C000000007F0000000007FFFFE
0000000000003FE8020000000000F8000000007FFFFE
0000000000000FF00C00000000003E00000000FFFFFE
00000000000003C01000000000000F80000000FFFFFE
000000000300000060000000000003E0000003FFFFFE
0000000003000001C1010000000000F8000013FFBFFE
0000000006000003000000000000007C000003FFFFFE
000000001800000E000000000000001E000007FFFFFE
0000000030000038000000000000000F80000FFFFFFE
00000000E00000600000000000000003C0000FFFFFFE
00000001803801C00000000000000001A0001FFFFFFE
0000000600FF0700000000000000000060003FFFFFFE
0000000EFFEFFC00000000000000000030003FFFFFFE
00000031FFFFF00000000000000000001C007FFFFFFE
000000C09FFFFC0000000000000000000E007FEFFFFE
000003FFFFFFFF8000000000000000000700FFFFFFFC
000007FFFFFFFFE000000000000000000781FFFFFFFC
00001CFFFFFFFFF0000000000000000003C1FFFFFFFE
000031FFFFFFFFFC000000000000000003C3FFFFFFFE
0000E7FFFFFFFFFE000000000000000001E3FFFFFFFC
00038FFFFFFFFFFF000000000000000008F7FFFFFFFC
000E1FFFFFFFFFFFC00000000000000005F7FFFFFFFE
00183FFFFFFFFFFFE000000000000000007FFEFFFFFE
00707FFFFFFFFFF7F800000000000000007FFFFFFFFE
01C07FFFFFFFFFE7F800000000000000007FF5FBFFFE
0700FFFFFFFFFFE7FE00000000000000007FF0FFFFFE
0C00FFFFFFFFFFF9FF00000000000000007FE83FFFFE
30007FFFFFFFFF00FF00000000000000007FE05FFFFE
E0007FFFFFFFFF40FF8000000000000000FFD07FFFF6
C0007FFFFFFFFF01FFC000000000000000FFE07FFFF6
80003FFFFFFFFFE27FE000000000000000FFC0FFFFEE
80001FFFFFFFFC383FE006000000000001FFC13FFFEE
00001FFFFFFFFC393FF000000000000001FFE017FFDE
00008FFFFFFFFFF3BFF800000000000003FFFC06FFDE
00000FFFFFFFFFFFFFF800000000000007FFFE06FFDE
000003FFFFFFFFFFFFFC0000000000000FFFFE0DFFDE
000000FFFFFFCFFFFFFC0000000000000FFFFF0DFFBE
0000000FFFFFFFFFFFFC0000000000001FDFFFCDFFBE
00000203FFFFE7FFFFFF8040000000003F9FFFFBFFBE
00000000FFFFF7FFFFFF00C0000000003F1FFFFBFFBE
000000003FFFFFFFFFFE000003000000FF1FFFF3FF7E
000000001FFFFFFFFFFE000000000000FE1FFFF7FF7E
000000000FFFFFFFFFFE000000000000FC1FFFE7FF68
0000000003FFFFFFFFFE000000000001FC1FFFEFFF00
0000000001FFFFFFFFFC000000800001F81FFFCFFE00
0000010001FFFFFFFFFC000401800003F01FFFCFFE00
0000000100FFFFFFFFFC000000000003E03FFF9FFE00
00000000007FFFFFFFF800000000000FE03FFF9FFE00
00000000003FFFFFFFF800000000003FC07FFF9FFC00
00000000001FFFFFFFF800000000003FC05FFF3FFC00
00000000001FFFFFFFE000000000007FC09FFF3FFC00
00000000000FFFFFFFE000000000007FE69FFF7FFC00
000000100007FFFFFFC000000000007FE3BFFE7FF800
800000100007FFFFFF000000000000FFE93FFE7FF800
000000000003FFFFFE000000400000FFF07FFCFFF800
000000000000FFFFF8000000000001FFE8FFFCFFF800
0000000000047FFFE0000000000003FFCF7FF8FFF000
0000000000001FFF80000000000003FFDFFFF9FFF000
000000000000016000000000100001FFDFFFFBFFF000
000000000000004000000000100001FFBFFFF3FFF000
000000000000000010000000000003FBFFFFF3FFE000
000000000000000000000000000003FB7FFFE7FFE000
000000000000000000000000000007F0FFFFE7FFE000
00000000000000000000000000000780FFFFC7FFE000
00000000000000000000000000002F80FFFFCFFFC000
0000000002000000000000000000FF00DFFFCFFFC000
0000000000000000000200000000FF00C7FF9FFFC000
0000000000000000000000000000FE0101FF9FFF8000
0008000040000000000000000001FC0100FF3FFF8000
0000000000000000000000000001FC03007F3FFF8000
000000000000C000000000000001F802007E3FFF8000
0000000000000000010000000001F006007E3FFF0000
0000000000000000010000000001F00400FC7FFF0000
0000000000000000000000000001E00C00FCFFFF0000
000000000000000000000000003BE01800FCFFFF0000
700000000000000000000000007FC01800F8FFFE0000
700000000000000000000000007FC01000F9FFFE0000
600000000000000000000000007F80200071FFFE0000
00000000000000000000000600FFC0600079FFFC0000
00000000000000000000000701FFF040007BFFFC0000
00000000000000000003000303FFF88000FBFFFC0000
00010000000000000003000007FFFE8000FBFFF80000
0000000000000000000100101FFFFF8001DB9FF80000
0000010000000000000118031FFFFF8001FB8FF80000
0000000000000000000018073FFFFFE003FB83F00000
000000200000000000000004BFFFFFF803BB03F00000
0000000000000000000000887FFFFFFC07FB80F00000
^FO150,150^XGR:img5.GRF,1,1^FS
^XZ
BTLE on zebra printers is meant to be used just to configure the printer, not to print labels, so it has a low timeout and a slow transmission speed.
Not a Flutter expert at all, but all the mobile printers should have the BT Classic, which should allow you to print labels, so you might check if you can switch to that on the printing call you're doing from your app.
You can also check what BT type the printer is set on by sending the following command
! U1 getvar "bluetooth.le.controller_mode"
and you can set it to use Classic BT sending this command
! U1 setvar "bluetooth.le.controller_mode" "classic"
(Note that all these commands need a new line after the command itself).
Have a look here to see how to send this command

What does the `-Sanitize` switch of the `Clear-Disk` cmdlet do in PowerShell?

The Clear-Disk cmdlet seems to have a currently undocumented -Sanitize switch:
PS> Get-Command Clear-Disk | Select-Object -ExpandProperty ParameterSets | ? {$_.Name -like 'ByName'} | Select-Object -ExpandProperty Parameters | ? {$_.Name -like 'Sanitize'}
Name : Sanitize
ParameterType : System.Management.Automation.SwitchParameter
IsMandatory : False
IsDynamic : False
Position : -2147483648
ValueFromPipeline : False
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments : False
HelpMessage :
Aliases : {}
Attributes : {InputObject (cdxml), ByNumber, ByPath, ByName...}
What does it do? Does it trigger the ATA/SCSI command SANITIZE?
[Edit: this is incomplete; after all that it looks like Sanitize is its own separate, undocumented parameter]
There are a lot of cmdlets in PowerShell which were automatically generated wrappers around WMI. That mention of cdxml in your question is a giveaway that Clear-Disk is one of those.
Run Get-Command Clear-Disk | Format-List * and skim the results, it has:
OutputType: Microsoft.Management.Infrastructure.CimInstance#
ROOT/Microsoft/Windows/Storage/MSFT_Disk
So Clear-Disk gives the MSFT_Disk WMI class a nice PowerShell cmdlet wrapper. MSFT_Disk is documented here. This seems to be out of date.
Clear-Disk is part of the Storage module, and that is in
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Storage>
In there is Disk.cdxml which is the mapping from WMI to Cmdlet, which contains:
<DefaultNoun>Disk</DefaultNoun>
[...]
<CmdletMetadata Verb="Clear" ConfirmImpact="High" />
<Method MethodName="Clear">
NB. that PowerShell cmdlets are Verb-Noun, so this is the section for verb Clear on default noun disk to make Clear-Disk, and it maps to the method Clear() on the MSFT_Disk class, I think. Inside that section is:
<Parameters>
<!-- RemoveData -->
<Parameter ParameterName="RemoveData">
<Type PSType="System.Management.Automation.SwitchParameter" />
<CmdletParameterMetadata />
</Parameter>
<!-- RemoveOEM -->
<Parameter ParameterName="RemoveOEM">
<Type PSType="System.Management.Automation.SwitchParameter" />
<CmdletParameterMetadata />
</Parameter>
<!-- Sanitize -->
<Parameter ParameterName="Sanitize">
<Type PSType="System.Management.Automation.SwitchParameter" />
<CmdletParameterMetadata />
</Parameter>
<!-- SourceCaller -->
<Parameter ParameterName="cim:operationOption:SourceCaller" DefaultValue="Microsoft.PowerShell">
<Type PSType="System.String" />
</Parameter>
</Parameters>
</Method>
The MSFT_Disk documentation from earlier has a section documenting the Clear() Method.
That takes four parameters:
UInt32 Clear(
[in] Boolean RemoveData,
[in] Boolean RemoveOEM,
[in] Boolean ZeroOutEntireDisk,
[out] String ExtendedStatus
);
And the blob of XML seems to map those to PowerShell parameter names. I first thought these matched up and the ZeroOutEntireDisk was a rename of the third parameter, but that does not seem correct; Martin Smith has found a source which has both parameters mentioned.
ZeroOutEntireDisk [in] is documented as:
TRUE if this parameter instructs this method to zero out the entire disk in addition to removing all partition information. If this parameter is FALSE or NULL, only the first and last megabytes of the disk are zeroed.
C:\Windows\SysWOW64\wbem\en-GB\storagewmi.mfl describes Sanitize:
"If TRUE, this parameter instructs Clear to send command to the disk to physically obliterate data."
wbemtest is the WMI Tester, separate from PowerShell, and you can connect to the root\Microsoft\Windows\Storage namespace, Open Class the MSFT_Disk class, edit the Clear Method and get to the parameters there, and the MOF definition for the input parameters is:
[abstract]
class __PARAMETERS
{
[In, ID(0): DisableOverride ToInstance] boolean RemoveData;
[In, ID(1): DisableOverride ToInstance] boolean RemoveOEM;
[In, ID(2): DisableOverride ToInstance] boolean ZeroOutEntireDisk;
[In, ID(3): DisableOverride ToInstance] boolean Sanitize;
[In, ID(4): DisableOverride ToInstance] boolean RunAsJob;
};
I don't know how the CDXML definition matches these. How it can call a method using fewer parameters than the method takes, whether it can match them in order or by name, what cim:operationOption:SourceCaller does, and whether this method even works or not (anyone tried?)

WINWORD Error when run in Task Scheduler "ExportAsFixedFormat"

I have a PowerShell script that leverages MS Word to convert txt files to .pdf. When executing the script in the PowerShell window there is no problem - the script works fine and does what it is suppose to do. However when executing this script from within the Task Scheduler MS Word fails when exporting the document as .pdf. To be clear, the script is executing as a Scheduled Task, I know this because I am sending all the script output to a transcript file. In the transcript file I see the following error right at this line:
$doc.ExportAsFixedFormat( $outputFullPath , $wdExportFormatPDF )
This is the error.
PS>TerminatingError(Convert-Txt2Pdf): "Exception calling "ExportAsFixedFormat" with "2" argument(s): "Word has encountered a problem.""
Error Caught
Convert-Txt2Pdf : Exception calling "ExportAsFixedFormat" with "2" argument(s): "Word has encountered a problem."
At C:\Source\PowerShell\RunScripts\Run-Txt2Pdf2SP.ps1:55 char:9
+ Convert-Txt2Pdf `
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Convert-Txt2Pdf], MethodInvocationException
+ FullyQualifiedErrorId : COMException,Convert-Txt2Pdf
Inner Exception
at Microsoft.Office.Interop.Word.DocumentClass.ExportAsFixedFormat(String OutputFileName, WdExportFormat ExportForma
t, Boolean OpenAfterExport, WdExportOptimizeFor OptimizeFor, WdExportRange Range, Int32 From, Int32 To, WdExportItem It
em, Boolean IncludeDocProps, Boolean KeepIRM, WdExportCreateBookmarks CreateBookmarks, Boolean DocStructureTags, Boolea
n BitmapMissingFonts, Boolean UseISO19005_1, Object& FixedFormatExtClassPtr)
at CallSite.Target(Closure , CallSite , Object , Object , Object )
I have found nothing in the Event Viewer and have no idea what the problem is. What could be causing this error?
Thanks

PowerShell crashes on `if ( $? = $false )`

I'm playing with PowerShell and I've encountered a crash that I can easily reproduce on my computer.
I'm not sure if the code is correct. However, running the following piece makes powershell.exe and powershell_ise.exe crash. I guess that my use of if ( $? = $false ) is wrong, but crash should not happen in such case. Removing If statement helps to avoid the crash.
Is there anything I'm missing?
I'm running Windows 10 Pro and PowerShell 5.1.14393.206.
Update 1
OK, thanks to #Martin I know that I mistakenly used = instead of -eq. But why this crash happens?
Update 2
Filed a bug report to PowerShell UserVoice: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/16977433-assigning-a-value-to-false-crashes
Update 3
It seems to be a known bug: https://github.com/PowerShell/PowerShell/issues/2243 that should be fixed soon https://github.com/PowerShell/PowerShell/pull/2320
Test-Path "C:\test"
if ( $? = $false ) {
Out-Host "Hello World"
}
Fault bucket 127386360339, type 5
Event Name: PowerShell
Response: Not available
Cab Id: 0
Problem signature:
P1: powershell.exe
P2: 10.0.14393.206
P3: stem.Management.Automation.PSInvalidCast
P4: stem.Management.Automation.PSInvalidCast
P5: ation.LanguagePrimitives.ThrowInvalidCastException
P6: ation.LanguagePrimitives.ThrowInvalidCastException
P7: Pipeli..ution Thread
P8:
P9:
P10:
Your code is wrong. You are assigning $false to the question mark variable which is a read-only variable. You probably want to replace the = with -eq:
Test-Path "C:\test"
if ( $? -eq $false ) {
Out-Host "Hello World"
}

Throw an exception in powershell with nesting original error

I'm a C# developer who is trying to build something useful using PowerShell. That's why I'm keep trying to use well-known idioms from .NET world in PowerShell.
I'm writing a script that has different layer of abstractions: database operations, file manipulation etc. At some point I would like to catch an error and wrap it into something more meaningful for the end user. This is a common pattern for C#/Java/C++ code:
Function LowLevelFunction($arg)
{
# Doing something very useful here!
# but this operation could throw
if (!$arg) {throw "Ooops! Can't do this!"}
}
Now, I would like to call this function and wrap an error:
Function HighLevelFunction
{
Try
{
LowLevelFunction
}
Catch
{
throw "HighLevelFunction failed with an error!`nPlease check inner exception for more details!`n$_"
}
}
This approach is almost what I need, because HighLevelFunction will throw new error and the root cause of the original error would be lost!
In C# code I always can throw new exception and provide original exception as an inner exception. In this case HighLevelFunction would be able to communicate their errors in a form more meaningful for their clients but still will provide inner details for diagnostic purposes.
The only way to print original exception in PowerShell is to use $Error variable that stores all the exceptions. This is OK, but the user of my script (myself for now) should do more things that I would like.
So the question is: Is there any way to raise an exception in PowerShell and provide original error as an inner error?
You can throw a new exception in your catch block and specify the base exception:
# Function that will throw a System.IO.FileNotFoundExceptiopn
function Fail-Read {
[System.IO.File]::ReadAllLines( 'C:\nonexistant' )
}
# Try to run the function
try {
Fail-Read
} catch {
# Throw a new exception, specifying the inner exception
throw ( New-Object System.Exception( "New Exception", $_.Exception ) )
}
# Check the exception here, using getBaseException()
$error[0].Exception.getBaseException().GetType().ToString()
Unfortunately when throwing a new exception from the catch block as described by this answer, the script stack trace (ErrorRecord.ScriptStackTrace) will be reset to the location of the throw. This means the root origin of the inner exception will be lost, making debugging of complex code much harder.
There is an alternative solution that uses ErrorRecord.ErrorDetails to define a high-level message and $PSCmdlet.WriteError() to preserve the script stack trace. It requires that the code is written as an advanced function cmdlet. The solution doesn't use nested exceptions, but still fulfills the requirement "to catch an error and wrap it into something more meaningful for the end user".
#------ High-level function ----------------------------------------------
function Start-Foo {
[CmdletBinding()] param()
try {
# Some internal code that throws an exception
Get-ChildItem ~foo~ -ErrorAction Stop
}
catch {
# Define a more user-friendly error message.
# This sets ErrorRecord.ErrorDetails.Message
$_.ErrorDetails = 'Could not start the Foo'
# Rethrows (if $ErrorActionPreference is 'Stop') or reports the error normally,
# preserving $_.ScriptStackTrace.
$PSCmdlet.WriteError( $_ )
}
}
#------ Code that uses the high-level function ---------------------------
$DebugPreference = 'Continue' # Enable the stack trace output
try {
Start-Foo -ErrorAction Stop
}
catch {
$ErrorView = 'NormalView' # to see the original exception info
Write-Error -ErrorRecord $_
''
Write-Debug "`n--- Script Stack Trace ---`n$($_.ScriptStackTrace)" -Debug
}
Output:
D:\my_temp\ErrorDetailDemo.ps1 : Could not start the Foo
+ CategoryInfo : ObjectNotFound: (D:\my_temp\~foo~:String) [Write-Error], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,ErrorDetailDemo.ps1
DEBUG:
--- Script Stack Trace ---
at Start-Foo, C:\test\ErrorDetailDemo.ps1: line 5
at , C:\test\ErrorDetailDemo.ps1: line 14
Our high-level error message 'Could not start the Foo' hides the error message of the underlying exception, but no information is lost (you could access the original error message through $_.Exception.Message from within the catch handler).
Note: There is also a field ErrorDetails.RecommendedAction which you could set as you see fit. For simplicity I didn't use it in the sample code, but you could set it like this $_.ErrorDetails.RecommendedAction = 'Install the Foo'.