Posts

Showing posts with the label powershell

Pester custom error text

Pester custom error text Does anyone know if Pester supports custom error texts? Lets say I've the following test: Describe "Get-HelloWorld" { It "outputs 'Hello world!'" { Get-HelloWorld | Should Be 'Hello world!' } } Is there a way that I can set some custom error message if Get-HelloWorld returns another string than "Hello World"? Get-HelloWorld Thx 1 Answer 1 The latest version of Pester supports -Because where you can add some custom text that explains the purpose of the test. -Because By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ConvertTo-JSON an array with a single item

ConvertTo-JSON an array with a single item I'm trying to create a JSON-serialized array. When that array contains only one item I get a string, not an array of strings (in JSON). Multiple Items (works as expected): PS C:> @("one", "two") | ConvertTo-JSON [ "one", "two" ] Single Item Array (not as expected): PS C:> @("one") | ConvertTo-JSON "one" Am I missing something? 3 Answers 3 Try without the pipeline: PS C:> ConvertTo-Json @('one', 'two') [ "one", "two" ] PS C:> ConvertTo-Json @('one') [ "one" ] Ahh yea. I see how using the pipeline would be ambiguous in this case. Thank you. You made me realize that it's not ConvertTo-JSON specific but a general powershell-array-pipline issue which lead me to: superuser.com/questio...

Need to create, use and store dynamic object names in Powershell

Need to create, use and store dynamic object names in Powershell I am loading in a text file and looping through each line, and trying to print a checkbox, in a powershell form. However, the way below, all checkboxes have the same variable/object name, which makes it impossible to tell them apart. I need a way to dynamically create $checkbox0 through $checkbox(# of lines in text file, which can change), and fill them in below, as well as store the names, so i can validate if they are clicked later $pFile = Get-Content "C:results.txt" $rowCounter = 0 foreach($line in $pFile){ $checkbox = New-Object System.Windows.Forms.CheckBox $checkbox.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size $checkbox.AutoSize = "true" $checkbox.TabIndex = $rowCounter $checkbox.Text = $line $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 25 $yValue = (20 * $rowCounter) $System_Dra...

Why ScriptBlock in PowerShell delays output until the ScriptBlock completes?

Why ScriptBlock in PowerShell delays output until the ScriptBlock completes? I have this code: Function-Step "Long operation" { MyLongRunningFunction } function Function-Step { param([string]$message,[ScriptBlock]$block) Write-Host $message Read-Host $block.Invoke() } MyLongRunningFunction writes information to the standard output as it runs, but it doesn't show in the console until the function finishes, suddenly. MyLongRunningFunction It seems that it's eating all the output of MyLongRunningFunction ends, and after that, it throws all the output at the same time. MyLongRunningFunction How can I avoid this behavior? Use Jobs? That's the most straightforward way. – TheIncorrigible1 Jul 2 at 1:01 $block.Invoke() -> & $block – PetSerAl ...

Retrieve value from command output

Retrieve value from command output I'm looking for a method which would allow me to retrieve a value from a command output. An example might be retrieving the IP address from an ipconfig output, or the encryption status from a manage-bde -status output. It's essentially the value after the colon ":" . I tried playing with the findstr options, but I could not retrieve "just" the value. In the ipconfig example, I want only the IP address, and in the "manage-bde" example, I want only the word "protection off". The above two commands are only examples. ipconfig manage-bde -status In addition, I need to figure out a method to perform a foreach in the event there are more than one IP/Nic, or more than one volume. foreach I could be either just one command, or output the results to a file, and then run something against the file. The answer to the basic question here is assign it to a variable. However you are look...

Edit HTML and put data stored in $password and then use it without writing it

Edit HTML and put data stored in $password and then use it without writing it I find and create template in HTML file and I would like to convert variable. Example: $test = 'francker' (Get-Content C:temppasssword2.html).Replace('$password', '$test') | Out-File C:check.html If I put $password inside of the file, the script will put $test but I want "francker" because I will save in another file and then send by email. $password $test I want to do this because I would like to send a new password by email. My template is ready and email function too. I know how to copy file in another place. Other information I have created GUI with windows form. Thank you Ansgar Wiechers .I put quote '$password' it's for that does not work . Also I discover one interesting thing's . I can change some data inside on this file and then use it without writing it . That's perfect because . '$test' -> $test . ...