powershell

Show content of hashtable when Pester test case fails

让人想犯罪 __ 提交于 2021-02-05 07:55:23
问题 Problem When a Hashtable is used as input for Should , Pester outputs only the typename instead of the content: Describe 'test' { It 'test case' { $ht = @{ foo = 21; bar = 42 } $ht | Should -BeNullOrEmpty } } Output: Expected $null or empty, but got @(System.Collections.Hashtable). Expected output like: Expected $null or empty, but got @{ foo = 21; bar = 42 }. Cause Looking at Pester source, the test input is formatted by private function Format-Nicely , which just casts to String if the

Different behaviors while passing parameters to PowerShell function

假如想象 提交于 2021-02-05 07:49:11
问题 I am trying to understand passing parameters to functions in PowerShell and it shows different behavior while passing single/multiple parameters. can someone explain why it is happening and what is correct way to do it? Function Add-Numbers($Num1,$Num2){ return ($Num1 + $Num2); } Function SquareIt($Num){ return ($Num * $Num); } # only this adds two numbers correctly $result1 = Add-Numbers 10 20 write-host "Result1: $result1"; #Passing single paramter works this way $result2 = SquareIt(15)

Remove comma when between quotes from batch or PoSh

折月煮酒 提交于 2021-02-05 07:33:11
问题 I have a CSV file with a content like: A,B,C D,"E,F",G H,I,"J,K,L" I need to remove the commas when between quotes (also remove the quotes, but that is not so important): A,B,C D,EF,G H,I,JKL I looked at PoSh -replace operator but I can't get it to capture multiple group values: PS >"D,`"E,F`",G" -replace "`"((?:[^,`"]+)\,?)+`"", '$1' D,F,G as you can see when the group is repeated, only the last value captured is preserved. Is there a way to do the transformation I want? https://regex101.com

How to set the default output of a PowerShell class

纵饮孤独 提交于 2021-02-05 06:54:14
问题 I am trying to create a PowerShell [DataTable] class derived from the standard [Data.DataTable] class to ease the creation of data tables: (see also: #11987 Accelerated [DataTable] with easy constructor and AddRow method) class DataTable : Data.DataTable { [ValidateNotNullOrEmpty()][Data.DataTable]$DataTable DataTable([Array]$ColumnNames) { $This.DataTable = New-Object Data.DataTable ForEach ($ColumnName in $ColumnNames) { If ($ColumnName -is [System.Collections.IDictionary]) { ForEach($Key

How to set the default output of a PowerShell class

一个人想着一个人 提交于 2021-02-05 06:54:05
问题 I am trying to create a PowerShell [DataTable] class derived from the standard [Data.DataTable] class to ease the creation of data tables: (see also: #11987 Accelerated [DataTable] with easy constructor and AddRow method) class DataTable : Data.DataTable { [ValidateNotNullOrEmpty()][Data.DataTable]$DataTable DataTable([Array]$ColumnNames) { $This.DataTable = New-Object Data.DataTable ForEach ($ColumnName in $ColumnNames) { If ($ColumnName -is [System.Collections.IDictionary]) { ForEach($Key

Difference between PowerShell's echo and CMD's echo

安稳与你 提交于 2021-02-05 06:45:46
问题 I get the following in PowerShell: D:\> echo "Apple Pie" | git hash-object --stdin 157cb7be4778a9cfad23b6fb514e364522167053 D:\> "Apple Pie" | git hash-object --stdin 157cb7be4778a9cfad23b6fb514e364522167053 but in CMD.exe: C:\>echo "Apple Pie" | git hash-object --stdin bb3918d5053fea31fc9a58fae1e5bdeabe3ec647 In a PluralSight video, I see a different value from what seems to be a Mac console: What is the exact value piped from echo in each case? I get a different hash if I go to one of those

PowerShell date format not accepted in SQL

蹲街弑〆低调 提交于 2021-02-05 06:37:11
问题 I'm having kind of a strange problem. When the server's Region and Language settings is set to English (United States) there are no issues with objects containing the date and time. But when I change it to my countries local Dutch (Belgium) I am experiencing problems in some of my PowerShell scripts. Is there somewhere a variable in PowerShell that needs to be set to fix this? Or do I have to default to English date formats on the server all the time? We use for example the SQL module

How do I get unique values from this array in PowerShell?

♀尐吖头ヾ 提交于 2021-02-05 06:25:37
问题 Why is the code below returning $null ? I'm trying to store just unique values. $DailyPathsToDelete = @("C:\temp\IMG000483\","C:\temp\IMG000483\") $DailyPathsToDelete = Select-Object $DailyPathsToDelete -Unique 回答1: You can try : $unique = $DailyPathsToDelete | Get-Unique 回答2: Short answer: To get all unique paths, you should pipe $DailyPathsToDelete to Select-Object and set the Unique switch. $DailyPathsToDelete = $DailyPathsToDelete | Select-Object -Unique Longer answer: 1. Why it's not

“System.IO.IOException: The operation completed successfully” exception

家住魔仙堡 提交于 2021-02-05 06:10:59
问题 I'm getting this exception System.IO.IOException: The operation completed successfully. in the following chunk of code. This code runs in a windows service. foreach (var error in _currentPowerShell.Streams.Error) { if (!string.IsNullOrEmpty(error.FullyQualifiedErrorId)) { if (!(error.CategoryInfo.Activity == "Get-Alias")) throw error.Exception; } } It doesn't make sense at all since I'm not doing any IO operation! 回答1: I think I've seen that before, when using 'overlapped' streams. It's a

“System.IO.IOException: The operation completed successfully” exception

旧街凉风 提交于 2021-02-05 06:09:50
问题 I'm getting this exception System.IO.IOException: The operation completed successfully. in the following chunk of code. This code runs in a windows service. foreach (var error in _currentPowerShell.Streams.Error) { if (!string.IsNullOrEmpty(error.FullyQualifiedErrorId)) { if (!(error.CategoryInfo.Activity == "Get-Alias")) throw error.Exception; } } It doesn't make sense at all since I'm not doing any IO operation! 回答1: I think I've seen that before, when using 'overlapped' streams. It's a