I have the following code and variables declared. How do I make sure that $shared_mailbox_alias becomes lowercase when expanded to the Mailbox creation cmdlet below? Since it's based on $Project_code which is typed in capital letters:
$Project_number = Read-Host -Prompt 'What is the Project Number ie. 02XXX'
$Project_code = Read-Host -Prompt 'What is the Project Code (in capital letters) ie. CRE'
$Project_name = Read-Host -Prompt 'What is the Project extended Name ie. Victoria Station Platform'
$shared_mailbox_displayname = "File: $Project_name"
$shared_mailbox_name = "$Project_code"
$shared_mailbox_alias = "file.$Project_code"
New-Mailbox -Shared -DisplayName $shared_mailbox_displayname -Name $shared_mailbox_name -Alias $shared_mailbox_alias -PrimarySmtpAddress $shared_mailbox_primarysmtp
My goal is to have an alias of "file.abc" rather than "file.ABC", and I cannot change the way it is been given as input (ABC) since I have other cmdlets that create folders with that syntax.
Cheers
Solved, thanks for the input, this is the syntax I used:
$shared_mailbox_alias = "file.$($Project_code.ToLower())"
Related
I've created a powershell script that removes all licences and group memberships from a person when he/she is leaving the company and adds an automatic reply to their mailbox. Everything else is going fine, except when i added Scandinavian letters into the hardcoded auto-reply.
bold The error is only for hard coded data. Everything that is prompted is read correctly
if ( "y" -eq $Automaattivastaus) {
$Username = Read-Host -Prompt "Nimi automaattivastaukseen"
$Paivamaara = Read-Host -Prompt "Milloin henkilön työsuhde on päättynyt?"
$Vastaanottaja = Read-Host -Prompt "automaattivastaus, Mihin sähköpostiin ottaa yhteyttä"
set-MailboxAutoReplyConfiguration -identity $Email -AutoReplyState Enabled -InternalMessage "Henkilön $Username palvelussuhde on päättynyt $Paivamaara. Pyydän kääntymään hänen työtehtäviensä hoitoon liittyvissä asioissa sovelletaan Lakia yksityisyyden suojasta työelämässä (759/2004)."
"snippet from auto reply Pyydän kääntymään hänen työtehtäviensä hoitoon liittyvissä asioissa."
As you can see the characters break. If anyone would have the solution how to fix this I would be grateful.
Connect-ExchangeOnline -UserPrincipalName "my.account#test.com" -ShowProgress $true
Add-Type -AssemblyName system.web
$Username = "Täältä pesee! Härifrån tvättas!"
$Paivamaara = "15.06.2021"
$Vastaanottaja = "Öland"
$Email = "test.account#test.com"
$Automaattivastaus = "Henkilön $Username palvelussuhde on päättynyt $Paivamaara. Pyydän kääntymään hänen työtehtäviensä hoitoon liittyvissä asioissa
($Vastaanottaja) puoleen. Mikäli viestisi on luonteeltaan yksityisluontainen, pyydän ottamaan yhteyttä suoraan henkilöön $Username. Yksityisluontaiset viestit tuhotaan ilman niiden avaamista. Työhön liittyvien viestien selvittämiseen ja avaamiseen
sovelletaan Lakia yksityisyyden suojasta työelämässä (759/2004)."
$t = [web.httputility]::Htmldecode($Automaattivastaus)
set-MailboxAutoReplyConfiguration -identity $Email -AutoReplyState Enabled -InternalMessage ([web.httputility]::Htmldecode($t)) -ExternalMessage ([web.httputility]::Htmldecode($t))
I don't have Exchange available, so I can't check this. Anyway, since ExternalMessage wraps the message automatically within HTML tags, try and convert the string into HTML escaped form. That will escape åäö and other characters that need escaping in HTML. Like so,
# Load assembly that contains the utility class
add-type -AssemblyName system.web
# A demo string with Finnish and Swedish letters
$s = "Täältä pesee! Härifrån tvättas!"
# Encode the string.
$t = [web.httputility]::HtmlEncode($s)
$t
Täältä pesee! Härifrån tvättas!
# The encoding is reversable, obviously.
[web.httputility]::Htmldecode($t)
Täältä pesee! Härifrån tvättas!
Edit:
Call the ENcode version, it will convert ääkköset into escped forms. the DEcode will reverse the encoding. It is not needed here, and was included as for illustration purposes only. Like so,
$Automaattivastaus = "Henkilön $Username palvelussuhde on päättynyt $Paivamaara..."
$encodedAnswer = [web.httputility]::HtmlEncode($Automaattivastaus)
set-MailboxAutoReplyConfiguration -identity $Email -AutoReplyState Enabled -InternalMessage $encodedAnswer -ExternalMessage $encodedAnswer
What if you would enclose the message in quotation marks and set the encoding?
$Username = Read-Host -Prompt "Nimi automaattivastaukseen"
$Paivamaara = Read-Host -Prompt "Milloin henkilön työsuhde on päättynyt?"
$Vastaanottaja = Read-Host -Prompt "automaattivastaus, Mihin sähköpostiin ottaa yhteyttä"
$message = "<html><head><meta charset='UTF-8'></head>
<body>
Henkilön $Username palvelussuhde on päättynyt $Paivamaara. Pyydän kääntymään hänen työtehtäviensä hoitoon liittyvissä asioissa sovelletaan Lakia yksityisyyden suojasta työelämässä (759/2004).
</body>
</html>"
set-MailboxAutoReplyConfiguration -identity $Email -AutoReplyState Enabled -InternalMessage $message
I'm no expert in either PowerShell or Exchange but this might help you.
Good Afternoon,
Just want to start by saying I am totally new with PS, and am really sorry if my approach to this is long winded or inaccurate.
I have a list of computernames that declared as individual Variables that my colleagues have provided me :
$Jon = "Abc1234"
$Mike = "Abc6789"
another 10 hostnames
I then ask the user if they want to send the files to another PC :
$Targetuser = Read-Host 'Who would you like to send it to?'
What I would like is the output from the above to change to the hostname, but I am unsure of how to do this.
Essentially, if the output was Mike, to change the $targetuser variable to $Abc6789
Thanks in advance
Dynamically named variables is almost always a bad idea. Instead, use a dictionary type, like a hashtable:
# Define user->host dictionary
$Hostnames = #{
Jon = "Abc1234"
Mike = "Abc6789"
}
# Ask user for target user
$TargetUser = Read-Host 'Who would you like to send it to?'
# Keep asking until the enter an actual user name
while(-not $Hostnames.ContainsKey($TargetUser)){
Write-Host "No hostname found for user '${targetUser}'!"
Write-Host "Choose one of:"
Write-Host ($Hostnames.Keys -join "`n")
$TargetUser = Read-Host 'Who would you like to send it to?'
}
# Get the associated hostname
$TargetHost = $Hostnames[$TargetUser]
# copy file to $TargetHost
I have a snippet in powershell that reads questions from a flat file and prompts the user to provide a boolean or string response (i.e. Do you like flying cars? Why do you like skydiving?). All answers are written to a .xls for consumption to a DB later.
I can make the script repeat question for users that do not select a boolean answer (i.e. A, B, C or "Yes", "No"). However getting a user to provide a string (short) answer) is a bit more trickier.
$Question7 = Get-Content -path $PSScriptRoot\src\Question7.txt -raw
Write-Host $Question7 -ForegroundColor Yellow
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
Writ-Host "Answer: $reason_for_hobby" -ForegroundColor Green
Add-Member -inputObject $infoObject -memberType NoteProperty -name "HBYREASON" -
value $reason_for_hobby
I am trying to figure out how to force users to provide at least a 215 character response, and to repeat the question if one is not provided.
Thanks and stay safe.
You can achieve this with a simple while-loop (While $reason_for_hobby has less than 215 characters repeate):
while ($reason_for_hobby.Length -lt 215){
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
}
However better would be a do-while-loop if the variable $reason_for_hobby is not initialized before (do this once and repeate while $reason_for_hobby has less than 215 characters):
do{
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
}while ($reason_for_hobby.Length -lt 215)
I am writing a simple script that takes an already created user and updates an attribute based on what the admin put in.
The code works just fine if I replace extensionAttribute with for example title or something like that, but it won't with extensionAttributes.
I have tried a few things and other extensionAttributes but the code is so simple and it works with other Attributes. I am guess extensionAttributes require a bit more in the code that I am missing.
$name = Read-Host "AD Logon Name"
$key = Read-Host "Azure Key"
Set-ADUser $name -extensionAttribute6 $key -PassThru
Set-ADUser : A parameter cannot be found that matches parameter name 'extensionAttribute6'
Even though it exists it is not finding it.
Set-ADUser has a limited set of parameters covering the most commonly used attributes in AD. However, given the sheer amount of existing attributes and the fact that the AD schema is extensible, an attempt to have all attributes represented as parameters just wouldn't be feasible.
For attributes that are not represented as parameters use the parameter -Add or -Replace with a hashtable argument.
Set-ADUser $name -Replace #{'extensionAttribute6' = $key} -PassThru
Old thread, but this worked for me:
Import-Csv -Path "C:\data\12345.csv" |ForEach-Object {
Set-ADUser $_.samAccountName -replace #{
description = "$($_.description)"
extensionAttribute1 = "$($_.extensionAttribute1)"
extensionAttribute3 = "$($_.extensionAttribute3)"
initials = "$($_.initials)";
#additionalAttributeName = "$($_.additionalAttributeName)"
#additionalAttributeName = "$($_.additionalAttributeName)"
#additionalAttributeName = "$($_.additionalAttributeName)"
#additionalAttributeName = "$($_.additionalAttributeName)"
#additionalAttributeName = "$($_.additionalAttributeName)"
}
}
The top row of your .csv file would look like the following for this example:
samAccountname,description,extensionAttribute1,extensionAttribute3,initials
Previously my script asked what the preferred username will be. Now I want to generate the username based on initials and surname.
However the alias for the e-mail is a bit different and that's the part where it doesn't work as I want to.
When an user has three initials the alias will be set to: jh.d.smith#domain.com, instead of jhd.smith#domain.com.
I don't know how to set this the proper way. Tried different methods.
$IMsg = 'Please enter initials'
$Initials = '{0}.' -f ((Read-Host -Prompt $IMsg).ToUpper().ToCharArray() -join '.')
$Firstname = Read-Host -Prompt 'Please enter firstname'
$Firstname = (Get-Culture).textinfo.totitlecase($Firstname.tolower())
$Lastname = Read-Host -Prompt 'Please enter surname'
$Lastname = $Lastname.ToLower() -replace ($Lastname.split(' ')[-1]), (Get-Culture).textinfo.totitlecase(($Lastname.split(' ')[-1]))
$UserID = $Initials.Replace('.','') + $lastname ## The username has to be without a dot, for example: jhdsmith.
$aliasid = $UserID.Insert(2,'.') ## This is for the e-mail alias.
Enable-Mailbox -Identity "$UserID#int.domain.com" -Alias $AliasID -Database DOMAIN-MAIL\DOMAIN-Store\Maildatabase01 | Out-Null
I think the easiest way is to do the following at the alias part:
$aliasid = $Initials.Replace('.','') + "." + $Lastname
I would also trim spaces like the following, in case someone has a lastname with spaces etc:
$UserID = (($Initials.Replace('.','') + $lastname).replace(" ","")).ToLower() ## The username has to be without a dot, for example: jhdsmith.
$aliasid = (($Initials.Replace('.','') + "." + $Lastname).Replace(" ","")).ToLower() ## This is for the e-mail alias.
This is beyond what you were after, but hopefully will be a good learning exercise :) If you deal with things logically, you don't need to use Replace(), ToCharArray() or Split()...
Starting point here is by ensuring that you input variables are all lowercase, alpha chars (a-z only), this can be achieved like this:
$Initials_input = (Read-Host -Prompt 'Please enter initials').tolower() -replace "\W"
The special bit here is -replace "\W" this uses regex word-characters to remove any non-word character (numbers/spaces/special chars) leaving only A-Z or a-z.
Then to make it easy to convert to TitleCase or UpperCase later on I have also used .tolower().
Once you know the state of all your inputs, you can simply build the strings to your desired format and convert to the required case.
Note that I don't change the input variables, they stay as-is. This way they are a known quantity, and you don't need to undo changes that are required by one variable to create another:
$Initials_input = (Read-Host -Prompt 'Please enter initials').tolower() -replace "\W"
$Firstname_input = (Read-Host -Prompt 'Please enter firstname').tolower() -replace "\W"
$Lastname_input = (Read-Host -Prompt 'Please enter surname').tolower() -replace "\W"
$Firstname = (Get-Culture).textinfo.totitlecase($Firstname_input)
$Lastname = (Get-Culture).textinfo.totitlecase($Lastname_input)
$Initials = $Initials_input.ToUpper()
$UserID = $Initials_input + $Lastname_input
$AliasID = $Initials_input + '.' + $Lastname_input
As you can see there's much less code, and it's easy to see what format your outputs will be.
Example input:
$Initials_input = 'jxz'
$Firstname_input = 'john'
$Lastname_input = 'smith'
Gives outputs:
$Firstname = John
$Lastname = Smith
$Initials = JXZ
$UserID = jxzsmith
$AliasID = jxz.smith