powershell-7.0

Function ported from Powershell 5 to Powershell 7 fails with “object reference not set to an instance of an object”

不想你离开。 提交于 2021-02-17 05:39:38
问题 Following code works as expected in powershell 5.1: function Get-Say([String] $Message, [String] $Voice = "Microsoft Irina Desktop") { Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer $speak.SelectVoice($Voice) $speak.Speak($Message) } However in powershell 7.1.0 (preview 5) execution is failing with "object reference not set to an instance of an object" and I'm actually lost. Googling does not help much since it looks like this error can be

Function ported from Powershell 5 to Powershell 7 fails with “object reference not set to an instance of an object”

萝らか妹 提交于 2021-02-17 05:39:12
问题 Following code works as expected in powershell 5.1: function Get-Say([String] $Message, [String] $Voice = "Microsoft Irina Desktop") { Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer $speak.SelectVoice($Voice) $speak.Speak($Message) } However in powershell 7.1.0 (preview 5) execution is failing with "object reference not set to an instance of an object" and I'm actually lost. Googling does not help much since it looks like this error can be

How to convert an object to a string?

心不动则不痛 提交于 2021-02-17 05:33:25
问题 I want to assign the name of the current directory to construct the path of a parallel directory (to run some diff commands). However when I do this: New-Item -ItemType Directory -Name my_test_dir; Set-Location my_test_dir; $a = $( Get-Item . | Select-Object Name ); write-host( "x${a}x" ); I get x@{Name=my_test_dir}x instead of what I expected : xmy_test_dirx So, how do I "unbox" the name of the directory? PS - for ease of testing I use: mkdir my_test_dir; cd my_test_dir; $a = $( Get-Item . |

Error handling within Foreach-object Parallel block - Powershell 7

泄露秘密 提交于 2021-01-27 12:34:58
问题 What would be the best way to capture error within below Foreach-Object Parallel block as there would be three separate threads/runspaces running and executing code written in the block and multiple errors/exception can occurs at the same time? Would it be possible to capture all the errors in a list/variable and show at the end of the execution of the script? 1..3 | ForEach-Object -ThrottleLimit 3 -Parallel { #Some code here that throws error } 回答1: The -ErrorVariable param will give you an

What is the equivalent of 'nohup' in linux PowerShell?

*爱你&永不变心* 提交于 2021-01-01 06:40:42
问题 I very well know existence of duplicate question. But that question is marked answered and I don't think it at all answers the original question. The $ nohup command keeps executing command even if the parent process (shell) of nohup dies. More info here: What's the difference between nohup and ampersand. In case of Win32 api, I see line When the system is terminating a process, it does not terminate any child processes that the process has created. Terminating a process does not generate