Catching FULL exception message

Errors and exceptions in PowerShell are structured objects. The error message you see printed on the console is actually a formatted message with information from several elements of the error/exception object. You can (re-)construct it yourself like this: $formatstring = “{0} : {1}`n{2}`n” + ” + CategoryInfo : {3}`n” + ” + FullyQualifiedErrorId : {4}`n” … Read more

Parameter interpretation when running jobs

Variables defined in the global scope of your script are not available inside the scriptblock unless you use the using qualifier: $code = { Get-WmiObject -Class “Win32_ComputerSystem” -Namespace “root\cimv2” -ComputerName $using:h } or pass them in as arguments, like this: $code = { Param($hostname) Get-WmiObject -Class “Win32_ComputerSystem” -Namespace “root\cimv2” -ComputerName $hostname } $jobstate = Wait-Job … Read more

Why does the `using` scope work locally with Start-Job, but not Invoke-Command?

This is true when using “using” because the definition of using states, Beginning in PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command Anytime you use the $using, you have to provide -ComputerName or -Session arguments whether the target server is localhost or remote. Ex. $myServerName=”www.google.com” … Read more

How to send multipart/form-data with PowerShell Invoke-RestMethod

The accepted answer won’t do a multipart/form-data request, but rather a application/x-www-form-urlencoded request forcing the Content-Type header to a value that the body does not contain. One way to send a multipart/form-data formatted request with PowerShell is: $ErrorActionPreference=”Stop” $fieldName=”file” $filePath=”C:\Temp\test.pdf” $url=”http://posttestserver.com/post.php” Try { Add-Type -AssemblyName ‘System.Net.Http’ $client = New-Object System.Net.Http.HttpClient $content = New-Object System.Net.Http.MultipartFormDataContent $fileStream … Read more

What’s the difference between single quote and double quote to define a string in powershell

Double quotes allow variable expansion while single quotes do not: PS C:\Users\Administrator> $mycolor=”red” PS C:\Users\Administrator> write-output -inputobject ‘My favorite color is $mycolor’ My favorite color is $mycolor Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences (I know version 1.0 but the principle is still the same)