Fix: Calling a function with multiple parameters in PowerShell, merges all parameters in the first parameter as an array

 

When you call a PowerShell function, that expects multiple parameters, with parenthesis, all parameters will be merged in the first parameter, as an array.

The solution is, to call the function with named parameters.

So NOT JoinText($p1, $p2, $p3) but JoinText -p1 $p1 -p2 $p2 -p3 $p3

 

script1.ps1

function JoinText([string]$p1, [string]$p2, [string]$p3) {

return “$p1 $p2 $p3”

}

 

script2-wrong.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText($p1, $p2, $p3)

write-host $result

 

script2-right.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText -p1 $p1 -p2 $p2 -p3 $p3

write-host $result

Calling a function from another PowerShell script file in the same folder with “relative path”

 

If you want to execute a function from another PowerShell script file, you can “dot-source” the file.

The “.” is a special operator in PowerShell that can load another PowerShell script file en import all code in it.

 

Assume both PowerShell scripts are in the same folder, we can use the code in script1.ps1 in script2.ps1 in the following way:

 

PowerShell script1.ps1

 

function ReturnGivenText {

Param(

        [Parameter(Mandatory=$True)]

        [string]$text

    )

    return $text

}

 

PowerShell script2.ps1

$text = ReturnGivenText -text “Hello world”

write-host $text

 

This will output:

Hello world

 

Note: I use the PowerShell >3 global variable $PSScriptRoot, to make the relative path, absolute, so we can use the “.” operator.

 

Fix: Power BI POST request returns a 500 Internal Server Error

When posting to the Power BI api uri: https://api.powerbi.com/v1.0/myorg/groups in PowerShell version 5.1.17134.228, I got the error:

The remote server returned an error: (500) Internal Server Error.

 

I invoked the Power BI rest api with the PowerShell script you can find below.

The error was fixed, when I added -ContentType ‘application/json’ parameter to the InvokeRestMethod

 

 

PowerShell script

 

$clientId = “my-client-id”

$PowerBIUserName = “MyUserName”

$PowerBIUserPassword = “MyUserPassword”

$creds = New-Object “Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential” -ArgumentList $PowerBIUserName,$PowerBIUserPassword

$resourceAppIdURI = “https://analysis.windows.net/powerbi/api”

$authority = “https://login.microsoftonline.com/common/oauth2/authorize”;

$authContext = New-Object “Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext” -ArgumentList $authority

 

# Acquire token

$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds)

 

# Create a new group

$auth_header = @{

‘Authorization’=$token.CreateAuthorizationHeader()

}

$uri = “https://api.powerbi.com/v1.0/myorg/groups”

$body = “{`”name`”:`”myNewGroup`”}”

$response = Invoke-RestMethod -Uri $uri -Headers $auth_header -Method POST -Body $body -ContentType ‘application/json’

$target_group_id = $response.id

 

 

 

 

How to fix: VSCode: Cannot launch program ‘…/app/ts’ because corresponding JavaScript cannot be found.

 

I compiled a very simple node.js express application with tsc (TypeScript) on the command line and wanted to debug it with VSCode, but when I pressed F5 I got the error: Cannot launch program ‘…/app/ts’ because corresponding JavaScript cannot be found.

 

I expected the paths in the launch.json to be wrong, but that was not the case.

The problem was that I did not generate *.js.map files.

After I changed the tsconfig.json file, to generate *.js.map files, I could debug the application.

 

tsconfig.json

{

“compileOnSave”: false,

“compilerOptions”: {

“target”: “ES2015”,

“module”: “commonjs”,

“sourceMap”: true,

“strict”: true

}

}

 

 

Preventing multiple WDS disconnects in Chrome on Windows 10, by trusting the self-signed https certificate, generated by Angular CLI ng serve –ssl true

 

If you use ng serve with the parameter –ssl true, to serve your Angular application.

It will generate a self-signed https certificate, so you can run the Angular application on localhost using HTTPS.

By default this certificate is NOT trusted by your browser, so you will get a red cross, left to the url.

Because this certificate is not trusted, you might get multiple WDS disconnect errors in the chrome developer tools console.

Sometimes ending in a never-ending loop.

If you want to trust the self-signed https certificate:

  • click on the red cross (a popup opens)
  • choose Certificate
  • Click on Details
  • Click on Copy to file

 

Press next

 

Choose DER encoded binary X.509 (.CER)

Enter C:\Temp\localhost.cer

 

Press Finish

 

Go to C:\Temp with the explorer and double click on the localhost.cer file.

 

 

Press Install Certificate…

 

Choose “Local Machine” if you want the certificate be trusted for all user of this computer.

 

 

To remove the certificate on Windows 10

  • Open Start, type certmgr.msc
  • Click on “Trusted Root Certification Authorities” > Certificates
  • Right click on localhost and choose Delete

 

How to exit long output from a git command (like git branch -a, git log, git diff)

 

When you have a lot of git branches, the output of git branch -a will be shown in the “less” program.

The first “x” branches are shown and at the bottom the pager cursor will be shown:

 

Now when you press “q”, the less program will quit and you will be returned to the command prompt.

Pressing “h” will show help.

Pressing “arrow down” will scroll one row.

Pressing “page down” will scroll a screen.

When you reach the end of the output, the text “END” will be shown:

 

Press “q” to return to the command prompt

 

See: https://stackoverflow.com/questions/9483757/how-to-exit-git-log-or-git-diff

Fixing – events.js:183 throw er; // Unhandled ‘error’ event, when npm installing PhantomJS

 

When running npm install inside my project folder, I got the error events.js:183 throw er; // Unhandled ‘error’ event

This was caused by an other terminal running my application.

After killing al terminals and browsers this error was fixed.

 

Error

PS C:\Dev\MyProject1> npm install

 

> entry@0.0.0 postinstall C:\Dev\MyProject1

> webdriver-manager update & node node_modules/phantomjs-prebuilt/install.js

 

events.js:183

throw er; // Unhandled ‘error’ event

^

 

Error: read ECONNRESET

at TLSWrap.onread (net.js:622:25)

Found PhantomJS at C:\Dev\MyProject1\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe …verifying

PhantomJS is previously installed at C:\Dev\\MyProject1\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe

Fix: Visual Studio Test project – The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config

 

After installing the NugetPackage EntityFramework in a .NET 4.5.2 Visual Studio Test project, using an EntityFramework DbContext would throw an exception during test run:

 

The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

 

This can be fixed, by touching the System.Data.Entity.SqlServer.SqlProviderServices.Instance in your test project, e.g. by adding a property that returns the System.Data.Entity.SqlServer.SqlProviderServices.Instance to the Test class.

 

[TestClass]

public class MyTest

{

 

public System.Data.Entity.SqlServer.SqlProviderServices SqlServerInstance { get { return System.Data.Entity.SqlServer.SqlProviderServices.Instance; } }

}

 

 

More information can be found at:

http://robsneuron.blogspot.com/2013/11/entity-framework-upgrade-to-6.html