Powershell transmitter parameter for itself

I have this powershell script that calls itself (since stage 1 of the script is to load the assemblies in the GAC, so I need to update the AppDomain). How to pass switch parameter to self. At the moment I am doing this:

if ($provisionsites -eq $true) { powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites } else { powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env } 

It seems too verbose to me. If I try this:

 powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites:$provisionsites 

Failure:

Cannot convert the value "System.String" to type "System.Management.Automat ion.SwitchParameter

+4
source share
1 answer

Presumably, calling powershell.exe is not needed at all. Replace it with the & operator, i.e. Call the script in the same session and avoid parameter conversions and related problems. Problems are solvable, but best avoided first. That is do

 & $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites:$provisionsites 

As for the problems. $provisionsites converted to a string ( True or False , not $true or $false ) before being transferred to an external application. So the actual arguments to the result look like -provisionsites:True . Then, in a new powershell session, it passes such an argument to script Deploy.ps1 . It fails because True or False strings are not expected, a Boolean value is expected.

A possible workaround would be to add the $ escape code

 powershell $currentDirectory/Deploy.ps1 ... -provisionsites:`$$provisionsites 

But consider removing the powershell call and its problems.

+3
source

Source: https://habr.com/ru/post/1441464/


All Articles