3

I have the following PS script

param (
    # FQDN or IP address of the Domain Controller
    [Parameter(Mandatory=$True)]
    [string]$ADaddress,

    # Active directory domain name
    # example: directory.local
    [Parameter(Mandatory=$True)]
    [string]$ADDomainName,

    # Domain admin
    # example: administrator@directory.local
    [Parameter(Mandatory=$True)]
    [string]$domainAdmin,

    # Domain admin password
    [Parameter(Mandatory=$True)]
    [string]$domainAdminPassword,

    # User to be added
    # example: testUser
    [Parameter (Mandatory=$True)]
    [string]$newUsername,

    # Password of th user to be added
    # example: 1!2#4%6
    [Parameter (Mandatory=$True)]
    [string]$newPassword,

    # SAM account name of the user to added
    # example: testuser
    [Parameter (Mandatory=$True)]
    [string]$newSamAccountName,

    # Display name of the user to added
    # example: "Test user for test purposes"
    [Parameter (Mandatory=$True)]
    [string]$newUserDisplayName
)

$domainAdminSecurePassword = $domainAdminPassword | ConvertTo-SecureString -asPlainText -Force
$domainAdminCredential = New-Object System.Management.Automation.PSCredential($domainAdmin, $domainAdminSecurePassword)
$newUserSecurePassword = $newPassword | ConvertTo-SecureString -asPlainText -Force
$UPN= $newUsername+"@"+$ADDomainName


Invoke-Command -ComputerName $ADaddress -Credential $domainAdminCredential `
    -ScriptBlock {`
        param($newUsername, $newUserSecurePassword, $newSamAccountName, $newUserDisplayName, $UPN) `
        new-aduser -name $newUsername -AccountPassword $newUserSecurePassword -Enabled $true -SamAccountName $newSamAccountName -DisplayName $newUserDisplayName -UserPrincipalName $UPN -PasswordNeverExpires $true`
    } `
    -ArgumentList $newUsername, $newUserSecurePassword, $newSamAccountName, $newUserDisplayName, $UPN

Tho problem I get when invoking this script is:

Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]' required by parameter 'PasswordNeverExpires'.

I tried passing 1 instead, passing [bool]$true but the result remains the same. I am new to PS and I'm lost here. Can anyone shine some light on what the problem may be?

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51
  • 1
    Hmm, maybe escape the dollar sign in `$true`? Also, please use `(Get-Credential)` as a means to get domain admin user and password, and not pass these as command line parameters, if possible. – Vesper Jun 08 '15 at 07:13
  • 1
    If so, maybe add a line `$PNE=$true` and add `$PNE` in the parameter list to your scriptblock? This way you will put a boolean as a parameter into it and should not have issues with type conversion. – Vesper Jun 08 '15 at 07:22
  • Yet again, the error occurres. The strange thing for me is that it doesn't blow up on `-Enabled $true` but blows up on the `-PasswordNeverExpires $true` which are on the same line and part of the same command – Mario Stoilov Jun 08 '15 at 07:27
  • 1
    Also, does rearranging the order change the error? Like if -PNE is not the last parameter on that line? – Ryan Bemrose Jun 08 '15 at 07:30

2 Answers2

5

Alright, I found what the problem was. Changed: -PasswordNeverExpires $true`

to

-PasswordNeverExpires $true ` (added a space after true)

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51
  • 1
    Dear me, I was suggesting this very trick at first, but then thought otherwise and edited the comment. Pity :) – Vesper Jun 08 '15 at 11:54
0

replacing $true with a variable did it for me. So this:

$command = 'New-CMApplicationDeployment -Name $Name -CollectionName $Col -OverrideServiceWindow $true  -Comment $Com -AvailableDateTime $Adt -DeployAction Install -DeployPurpose Available -UserNotification DisplaySoftwareCenterOnly'
Invoke-Expression -Command "& $command"

became:

$t = $true
$command = 'New-CMApplicationDeployment -Name $Name -CollectionName $Col -OverrideServiceWindow $t  -Comment $Com -AvailableDateTime $Adt -DeployAction Install -DeployPurpose Available -UserNotification DisplaySoftwareCenterOnly'
Invoke-Expression -Command "& $command"

Its dumb but it worked.

john k
  • 6,268
  • 4
  • 55
  • 59