VMware Cloud Community
dwchan
Enthusiast
Enthusiast

How to past Boolean variables

I have a variable $enabled I am trying to pass to Invoke-Command (to create new user with the New-ADuser command)

If ($_.Enabled -ieq 'true') { $enabled = $True } Else { $enabled = $False }

I declare the $enabled in my param

$ConfigureADUsers = {
param(
[string]$GivenName,
.
[switch]$enabled

New-ADUser $sam -GivenName $GivenName -DisplayName $GivenName `
 -Enabled $enabled

I am getting the following error, no clue as to what I am missing as I even declare the value being pass is not a string

Cannot convert 'System.Management.Automation.SwitchParameter' to the type 'System.Nullable`1[System.Boolean]' required by
parameter 'Enabled'.
+ CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.NewADUser
+ PSComputerName : DC

 

0 Kudos
7 Replies
LucD
Leadership
Leadership

If you want to pass the Switch parameter as a Boolean on a parameter on another cmdlet, use

-Enabled $enabled.IsPresent

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dwchan
Enthusiast
Enthusiast

I am sorry but don't quite follow the syntax.  So do I define 

-Enabled $enabled.IsPresent

along within my intended command like

$ConfigureADUsers = {
param(
[string]$GivenName,
.
[switch]$enabled
)

New-ADUser $sam -GivenName $GivenName -DisplayName $GivenName `
 -Enabled $enabled.ISPresent
}

or do I pass it on as another parameter within my Invoke-Command?

Invoke-Command -Session $SessionID -ScriptBlock $ConfigureADUsers -ArgumentList $_.GivenName,$_.LastName,$_.Initials,$sam  -Enabled $enabled.ISPresent

0 Kudos
LucD
Leadership
Leadership

You defined the Switch in the code you pass to the code block that you call via Invoke-Command.

Then you pass it as a Boolean on the ArgumentList, and inside the code block you refer to it as $enabled.ISPresent.

An alternative is to define the parameter $enabled as a Boolean, then you don't need the $enabled.IsPresent, but you can just refer to it as $enabled.

Your choice


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
0 Kudos
dwchan
Enthusiast
Enthusiast

Sorry but I am still somewhat confused.  here is what I have setup so far, and I more less bypass the field $enabled by hardcoded as "true"

$ConfigureADUsers = {
param(
[string]$GivenName,
.
.
[string]$TargetOU,
[switch]$enabled,
[switch]$expires,
[switch]$CannotChangePassword
)
$exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)"
If(!$exists)
{
$SPassword = ConvertTo-SecureString -AsPlainText $Password -force
Write-Host -ForegroundColor Green "Creating new AD user: '$sam'..."

If (($LastName -eq $null) -Or ($LastName -eq ""))
{
New-ADUser $sam -GivenName `
-homeDrive $HomeDrive -Enabled $true -PasswordNeverExpires $true `
-CannotChangePassword $true
} Else {
.
.
}

$dn = (Get-ADUser $sam).DistinguishedName

If ([adsi]::Exists("LDAP://$($TargetOU + ',' + $addn)"))
{
Move-ADObject -Identity $dn -TargetPath $($TargetOU + ',' + $addn)
Write-Host -ForegroundColor Green "Moved newly created user '$sam' to target OU: $TargetOU,$addn"
} Else {
Write-Host -ForegroundColor Red "Targeted OU couldn't be found. Newly created user '$sam' wasn't moved..."
}
}
}
Invoke-Command -Session $SessionID -ScriptBlock $ConfigureADUsers -ArgumentList $_.GivenName,$_.LastName,$_.Initials,$sam,$addn,$JoinDomain,$_.OfficeName,$_.Description,$_.Password,$Email,$_.StreetAddress,$_.City,$_.State,$_.PostalCode,$_.Country,$_.Company,$_.Department,$_.EmployeeID,$_.Title,$_.OfficePhone,$_.HomePhone,$_.MobilePhone,$_.Fax,$_.Manager,$_.ProfilePath,$_.ScriptPath,$_.HomeDirectory,$_.HomeDrive,$_.TargetOU,$enabled,$expires,$CannotChangePassword

So are you suggesting that I replace '-Enabled $true' within my "New-ADUser" commandlet with '-Enabled $enabled.ISPresent'  I am assuming I am not using that with my 'Invoke-command'

 

0 Kudos
LucD
Leadership
Leadership

Instead of me writing many words about switches and scope, have a look at this example.

Calling the Test-Function is the same as calling a cmdlet with a Switch parameter.

$code = {
    param(
        [Switch]$SW
    )

    function Test-Function{
        param(
            [Switch]$fSW
        )

        Write-Host "Switch in function is $($fSW.IsPresent)"
    }

    Write-Host "The switch in the code block is $($SW.IsPresent)"

    Test-Function -fSW:$sw.IsPresent
}

Write-Host "Calling Invoke-Command with `$false"
Invoke-Command -ScriptBlock $code -ArgumentList $false

Write-Host "Calling Invoke-Command with `$true"
Invoke-Command -ScriptBlock $code -ArgumentList $true

And for completeness, it doesn't really matter if the Test-Function is defined inside or outside the code block.
The outside definition would correspond the closest to calling a cmdlet.

function Test-Function{
    param(
        [Switch]$fSW
    )

    Write-Host "Switch in function is $($fSW.IsPresent)"
}

$code = {
    param(
        [Switch]$SW
    )

    Write-Host "The switch in the code block is $($SW.IsPresent)"
    Test-Function -fSW:$sw.IsPresent
}

Write-Host "Calling Invoke-Command with `$false"
Invoke-Command -ScriptBlock $code -ArgumentList $false

Write-Host "Calling Invoke-Command with `$true"
Invoke-Command -ScriptBlock $code -ArgumentList $true


 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dwchan
Enthusiast
Enthusiast

Thank you for your patience and clarification, as now I know where you suppose to used the ISPresent at.  However, with that being said, it is not function as expected.  

in your example

Invoke-Command -ScriptBlock $code -ArgumentList $false

you pass the argument in explicitly as $false or $true, while I am trying to do the same in my code as a value with the value of either '$true' or '$false', it is default to $false every time for some reason.  For example, I read in a variable from XLS as $_.Enabled and than declare $enable as '$true' (i even did a write-host $enabled to ensure it is reading it in correct and the if than logic is correct)

if ($_.Enabled -ieq 'true') { $enabled = '$True' } Else { $enabled = '$False' }

I then include the $enabled in my param as [switch]$enabled

further down, my code call out as 'New-ADUser .... -homeDrive $HomeDrive -Enabled $enabled.ISPresent -PasswordNeverExpires $true..'

And lastly, I pass this in from my Invoke-Command

Invoke-Command -Session $SessionID -ScriptBlock $ConfigureADUsers -ArgumentList $enabled, .....

I will do some more testing and track this down, but I am missing something for some reason

 

 

0 Kudos
LucD
Leadership
Leadership

When you read properties from an XLS they are all of type String.
When you want to pass a Boolean, you will have to convert that String variable to the Boolean type.
Simple casting will not work, because [Boolean]'true' and [Boolean]'false', will both be converted to $true (since they are non-empty strings).
Only [Boolean]'' will convert to $false.
 
The solution is to use the System.Convert method.
Something like this
$code = {
  param(
      [Switch]$SW
  )

  function Test-Function{
      param(
          [Switch]$fSW
      )

      Write-Host "Switch in function is $($fSW.IsPresent)"
  }

  Write-Host "The switch in the code block is $($SW.IsPresent)"

  Test-Function -fSW:$sw.IsPresent
}

Write-Host "Calling Invoke-Command with `$false"
$t = 'false'
Invoke-Command -ScriptBlock $code -ArgumentList ([System.Convert]::ToBoolean($t))

Write-Host "Calling Invoke-Command with `$true"
$t = 'true'
Invoke-Command -ScriptBlock $code -ArgumentList ([System.Convert]::ToBoolean($t))


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos