Note: The following assumes that your JSON config file:
- contains an object whose property names match the parameter names of the target cmdlet,
New-ADUser
- and that the property values are limited to strings, numbers, Booleans, and nested objects that PowerShell can cast to the parameter's type, based on the constraints explained in this answer.
These constraints ensure that a hashtable representation of that object can be used as-is for parameter splatting.
If you're running PowerShell (Core) 7+, you can take advantage of ConvertFrom-Json
's
-AsHashtable
parameter.
# Load JSON from file 'config.json' into a [hashtable] instance.
$params = Get-Content -Raw config.json | ConvertFrom-Json -AsHashtable
# Use the hashtable for splatting.
New-ADUser @params
In Windows PowerShell (whose latest and final version is 5.1), you have to manually convert the [pscustomobject]
instance returned by ConvertFrom-Json
to a hashtable:
# Load JSON from file 'config.json' into a [pscustomobject] instance.
$paramsAux = Get-Content -Raw config.json | ConvertFrom-Json
# Convert the [pscustomobject] to a [hashtable] instance by
# making its properties (name-value pairs) hashtable entries.
$params = @{}
foreach ($prop in $paramsAux.psobject.Properties) {
$params[$prop.Name] = $prop.Value
}
# Use the hashtable for splatting.
New-ADUser @params
The sample config JSON you've provided in a comment indicates that the prerequisites stated at the top are not met, and that custom transformation of the config JSON is required in order to map onto the targeted New-ADUser
parameters:
- Update: The truly intended target cmdlet turned out to be
New-AzureADuser
, for whose parameters the properties of the sample JSON below are a direct match, so no custom transformation is needed; hopefully, the technique shown is still useful to future readers who do need custom transformations.
# Create a sample JSON config file.
@'
{
"GivenName": "Lili",
"SurName": "Waters",
"accountEnabled": true,
"displayName": "Lili Waters",
"mailNickname": "LiliW",
"userPrincipalName": "liliw@mailcom",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "xxyyzz"
}
}
'@ > config.json
# Load JSON from file 'config.json' into a [pscustomobject] instance.
$paramsAux = Get-Content -Raw config.json | ConvertFrom-Json
# Convert the [pscustomobject] to a [hashtable] instance by
# making its properties (name-value pairs) hashtable entries,
# applying custom transformations, as necessary.
$params = @{}
foreach ($prop in $paramsAux.psobject.Properties) {
switch ($prop.Name) {
# Map the JSON property names and values onto the appropriate
# New-ADUser parameters.
'accountEnabled' { $params['Enabled'] = $prop.Value }
'mailNickname' { $params['SamAccountName'] = $prop.Value }
'passwordProfile' {
$params['ChangePasswordAtLogon'] = $prop.Value.forceChangePasswordNextSignIn
$params['AccountPassword'] = ConvertTo-SecureString -Force -AsPlainText $prop.Value.password
}
# All other properties directly map onto New-ADUser parameters.
default { $params[$prop.Name] = $prop.Value }
}
}
# Use the hashtable for splatting.
# Note: To inspect the actual arguments being passed, use Write-Output in lieu
# of New-ADUser, or just output the hashtable, $params, by itself.
New-ADUser @params
Note: I'm unsure as to what parameter the mailNickname
property maps to; I've assumed -SamAccountName
above; adjust as needed.