Is it possible to determine if certain cmdlet has one exact parameter?
For example if I work with Exchange server I know that web-access for devices is present since 2013 version. So before this version there are no related parameters in cmdlets.
Is it possible to take a cmdlet, for example New-Mailbox and check if it has one exact parameter (that parameter would not exist for 2010 version and would for 2013+)?
The question is quite old, but still.. :)
Try the code below to get list of available CmdLet parameters
$params = (Get-Command New-Mailbox).ParameterSets | Select -ExpandProperty Parameters
$params | ForEach {$_.Name}
Pavel's answer is fine. This way is slightly shorter and easier to read:
(Get-Command cmdletName).Parameters['parameterName']
This example uses this to check the New-Mailbox cmdlet for the EnableRoomMailboxAccount parameter, which was added in Exchange Server 2013 (the scenario described in the question):
if((Get-Command New-Mailbox).Parameters['EnableRoomMailboxAccount']) {
New-Mailbox -UserPrincipalName confroom1010#contoso.com `
-Alias confroom1010 `
-Name "Conference Room 1010" `
-Room `
-EnableRoomMailboxAccount $true `
-RoomMailboxPassword (ConvertTo-SecureString -String P#ssw0rd -AsPlainText -Force)
}
else {
New-Mailbox -UserPrincipalName confroom1010#contoso.com `
-Alias confroom1010 `
-Name "Conference Room 1010" `
-Room
}
The PowerShell $args variable is an array of the parameters used in the call. You can use $args.Count to verify the desired parameter is there. You can also test against the value of the first parameter by using $args[0].
Mike
Related
Powershell novice here. I need a script to create bulk AD groups and set the email address for the group. We do not use exchange. I have not been able to find good examples when not using exchange.
$Example = get-content c:\temp\Example.txt
foreach($Example in $Example){
New-ADGroup -Name "$Example.###" -SamAccountName "$Example.###" -Email "$Example.####Anywhere.com" -ParentContainer "OU=THERE,OU=Organization,DC=HERE,DC=NET" -GroupType "Security" -GroupScope "Global"
}
New-AdGroup doesn't have a parameter Email. You will have to use
-OtherAttributes #{mail = "$Example.####Anywhere.com"}
PS. If the dot after the variable leads to problems, you can also format like
-OtherAttributes #{mail = ('{0}.####Anywhere.com' -f $Example)}
I am getting the below error while trying to add a computer object in AD by using Powershell.
New-ADComputer -Name <Computer Name> -Path 'OU=Devices,DC=enterprise,DC=com' -Enabled $True
Error: New-ADComputer : A required attribute is missing
See example 1 at New-ADComputer. I guess the parameter -SamAccountName is missing.
New-ADComputer -Name "USER02-SRV2" -SamAccountName "USER02-SRV2" -Path "OU=ApplicationServers,OU=ComputerAccounts,OU=Managed,DC=USER02,DC=COM"
I am using the following powershell code for creating new mailboxes in my organization.
$users = Import-CSV C:\mailboxes.csv
$users| foreach {
$Password = convertto-securestring $_.password -asplaintext -force
new-mailbox -name $_.name -alias $_.alias -FirstName $_.Firstname -LastName $_.Lastname -userPrincipalName $_.userPrincipalName -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.database -RetentionPolicy "b3a83dc4-e471-4d05-b357-25535aa027af" -OrganizationalUnit $_.OrganizationalUnit -Password $Password –ResetPasswordOnNextLogon:$false
}
Is there a way to insert a static text/value to this "zip code" and "po box" boxes, on the new active directory user, created along with this mailboxes?
for example , zip code should contain: "0101010101" and P.O Box should contain "000"
Your assistance is most appreciated
One option is to use Set-ADUser from the ActiveDirectory module. At the beginning of your script (before any loops), you can run the following if you have the module available to your current session.
Import-Module ActiveDirectory
After your New-Mailbox command, you can add the Set-ADUser command:
Set-ADUser -Filter "UserPrincipalName -eq '$($_.userprincipalname)'" -PostalCode "01010101" -POBox "000"
Sometimes AD replication can cause inconsistencies with multiple commands against AD objects. To get around that, you would typically use the -Server parameter to consistently target a domain controller that will see all of your read and write operations. The alternative (a slower one) is to run the AD user modifications after all of the mailboxes have been created and data has replicated to the AD Site you would be targeting.
AdminOfThings - Thanks for your reply.
So tell me,
Considering your last comment about the AD User modification conflict that i might occur,
i`m thinking some sort of "time delay" code might resolve such issues.
would it be logical to add something like "Start-Sleep" command to add a delay between
the "new-mailbox" and "Set-ADUser" commands as you suggested?
if so can you...write down how my script should like exactly, adding all things together please?
Thanks.
Need helping parsing variable from CSV.
I am trying to create a script, below. What I am trying to accomplish is resetting a service account's password based on a CSV file, then afterwards I need it to use Invoke-Command to edit the registry with the password. This is for auto-logins.
I can get the script to reset the password, and put a variable in the registry, but it puts the entire line in the directory, not just the password. I need to figure out how to parse it out to just pass along the password.
The part in question is the code inside the Invoke-Command scriptblock.
I was trying this command originally using $account.password, but the variable was not being pushed along, someone mentioned the $using command, which at least pushed the variable along, but its all of the variables, computer name, accountname, and password.
Import-Module ActiveDirectory
$Resetpassword = Import-Csv "c:\UserList.csv"
foreach ($Account in $Resetpassword) {
$Account.sAMAccountName
$Account.Password
$Account.computer
Set-ADAccountPassword -Identity $Account.sAMAccountName -NewPassword (ConvertTo-SecureString $Account.Password -AsPlainText -force) -Reset
Invoke-Command -computername $Account.computer -UseSSL -Scriptblock {
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" "Test" -Value "$using:Account.Password" -Type string
}
}
Using quotes around the variable will cause issues here. Just use $using:Account.Password without quotes. PowerShell is trying to stringify the $Account object. This issue compounds when you try to access a property of an object because the variable will be expanded and then the .propertyname will be concatenated to the the output. You can see this behavior if you type Write-Host $account.password at your console.
Update your Invoke-Command to the following:
Invoke-Command -computername $Account.computer -UseSSL -scriptblock {
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" "Test" -Value $using:Account.Password -type string
}
Below is an example of the issue:
$a = [pscustomobject]#{name = "strings everywhere"}
"$a"
#{name=strings everywhere}
Write-Host "$a.name"
#{name=strings everywhere}.name
To get around the symptom, you need to either use the variable syntax ($a) or use the subexpression operator ($()), which allows variable expansion to happen within the operator before a potential string conversion applies. The same goes for accessing properties on the variable.
$a.name
strings everywhere
$($a.name)
strings everywhere
Write-Host "$($a.name)"
strings everywhere
Using Set-ADAccountPassword normally you don't get any output. The get-help of Set-ADAccountPassword says there is a -PassThru parameter to "Return the new or modified object" however I can't get any output at all.
Set-ADAccountPassword -Identity <username> -Reset -NewPassword -PassThru (ConvertTo-SecureString -AsPlainText "TempP#$$W0rd" -Force)
The command works, but there is no output. I'd like to get it working singularly first, and then eventually use Get-ADUser to pipe an OU of users to Set-ADAccountPassword and display the list of objects that were modified. I just can't understand why -PassThru appears to do nothing.
Thank you
Place -passthru at the end of the command and watch out for those quotes around TempP#$$W0rd. The double quotes allow for variable expansion in the string. $$ is an automatic variable representing the last token in the last line powershell received. This may make your password something completely different than what you think it is.
Example
PS:>Get-ChildItem C:\
PS:>"TempP#$$W0rd"
TempP#C:\W0rd
Single quote it instead. I'm not at a computer with the AD module on it but this should work.
Set-ADAccountPassword -Identity <username> -Reset -NewPassword (ConvertTo-SecureString -AsPlainText 'TempP#$$W0rd' -Force) -PassThru
Here's a good article that explains -PassThru
I've been battling this all morning and can't seem to get any output from -PassThru either. I'd be interested to see what the difference is between those it works for and those it doesn't. I'm running this with PS4.0 on a Windows Server 2008 R2 Domain.
Set-ADAccountPassword -Identity $UserDN -Credential $AdminCred -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $Pass -Force) -PassThru
I ended up re-imaging my machine from Win7 which then had ps v3 and later v4 installed on it, to Win8 which obviously comes with ps v4. Ran the same script and it worked.
Perhaps somewhere along the upgrade path something broke.