Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 183 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell: How do I pass variables to switch parameters when invoking powershell at the command line?

#1
Normally, if you want to defer the specification of a switch parameter to some variable, you can pass an expression to the switch parameter, as seen with the WhatIf parameter.

test.ps1

param ( [string] $source, [string] $dest, [switch] $test )
Copy-Item -Path $source -Destination $dest -WhatIf:$test

This allows you great flexibility when working with switches. However, when you call powershell with cmd.exe or something, you wind up with something like this:

D:\test>powershell -file test.ps1 -source test.ps1 -dest test.copy.ps1 -test:$true

D:\test\test.ps1 : Cannot process argument transformation on
parameter 'test'. Cannot convert value "System.String" to type "System.Manageme
nt.Automation.SwitchParameter", parameters of this type only accept booleans or
numbers, use $true, $false, 1 or 0 instead.
At line:0 char:1
+ <<<<
+ CategoryInfo : InvalidData: (:) [test.ps1], ParentContainsError
RecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,test.ps1

However, the same result appears when passing `-test:true`, and `-test:1`. Why doesn't this work? Shouldn't Powershell's type conversion system automatically recognize these strings as being convertible to bool or switch, and convert them?

Does this mean that when calling powershell scripts from some other system (such as a build system) it's necessary to construct complex flow control structures to determine whether or not to include a switch in the command string, or omit it? This seems tedious and error prone, which leads me to believe it's not the case.
Reply

#2
Use the IsPresent property of the switch.
Example:

function test-switch{
param([switch]$test)
function inner{
param([switch]$inner_test)
write-host $inner_test
}
inner -inner_test:$test.IsPresent
}
test-switch -test:$true
test-switch -test
test-switch -test:$false

True
True
False


BTW, I used functions rather than a script so it would be easier to test.
Reply

#3
This behaviour has been filed as a bug on [connect][1]. This is a workaround:

powershell ./test.ps1 -source test.ps1 -dest test.copy.ps1 -test:$true

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through