0

I'm assuming its obvious, but I'm completely missing it. I've got a PowerShell script I'm working on to automate user creation. One of my functions generates the email address. Right now, its not doing anything fancy, just joining the username and domain. I'll be adding more later to increment the value if in use. But I can't get the basic version to work.

function generateMailbox {
  param(
    [parameter(Mandatory=$true)]
    $userName,
    [parameter(Mandatory=$true)]
    $domain
  )

  $uniqueMailbox = $null

  #set initial value for $uniqueMailbox as the initial mailbox name
  $uniqueMailbox = "$userName@$domain"

  return $uniqueMailbox
}

So, pretty straightforward. BUT... the "@" is missing from the emails it generates. I put in a break point on the return and verified $uniquemailbox is missing that "@". So if $userName = "uname" and $domain = "domain.com", then $uniqueMailbox will be set to "unamedomain.com"

While in the debug mode, I can manually enter $uniqueMailbox = "$userName@$domain" and then the value returns correctly. For the life of me I can't see my mistake.

I did double check the $uniqueMailbox variable isn't used anywhere outside this function.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Dave
  • 167
  • 3
  • 18
  • 1
    I cannot reproduce this copy-pasting your function on a command prompt an invoking it as `generateMailbox "uname" "domain"`, the result is `uname@domain`. – Jeroen Mostert Apr 06 '21 at 15:21
  • 1
    I'm unable to reproduce this behavior, what version of PowerShell are you using? – Mathias R. Jessen Apr 06 '21 at 15:22
  • get-host info: 5 1 14393 3866 – Dave Apr 06 '21 at 15:24
  • Can't reproduce on PS 5.1 either. – zett42 Apr 06 '21 at 15:27
  • from the powershell prompt, I can run `generateMailbox -userName "jdoe" -domain "domain.com"` and the result is `jdoedomain.com`. – Dave Apr 06 '21 at 15:27
  • Maybe an issue with the font in your console. Try switching to another font like Lucida Console. – zett42 Apr 06 '21 at 15:29
  • Screenshot: https://imgur.com/a/VQIbqN0 – Dave Apr 06 '21 at 15:30
  • @zett42 it is actually set to Lucida Console. Also, if I manually run the command `$uniqueMailbox = "$userName@$domain"` it outputs fine in the same window. – Dave Apr 06 '21 at 15:31
  • What is output when you write `Write-Host '@'`? Also, what is output by `[System.Text.Encoding]::Default` from script and what encoding are you using to save the script file? Try to save as UTF-8. – zett42 Apr 06 '21 at 15:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230812/discussion-between-dave-and-zett42). – Dave Apr 06 '21 at 15:34
  • write-host returns a @ as expected. looks to be iso-8859. Screenshot showing some of the various output: https://imgur.com/a/XneoTOh – Dave Apr 06 '21 at 15:39
  • 1
    Weird.. what do you get when using `$uniqueMailbox = '{0}@{1}' -f $userName, $domain` ? – Theo Apr 06 '21 at 15:44
  • 1
    @mklement0 weirdly enough, it runs fine in normal PowerShell. Hadn't actually tried that. Only displays this behavior in ISE. – Dave Apr 06 '21 at 15:45
  • 1
    Proof once more that the ISE is actually kind of evil. There are lots of subtle behavioral changes, even if this one is not particularly subtle. – Jeroen Mostert Apr 06 '21 at 15:47
  • open a new runspance(ise tab) and run the same code again – Abraham Zinala Apr 06 '21 at 15:49
  • 2
    Re ISE in general: It is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell [Core] 6+. The actively developed editor that offers the best PowerShell development experience, across platforms, is [Visual Studio Code](https://code.visualstudio.com/), combined with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell) – mklement0 Apr 06 '21 at 15:50
  • 1
    @Dave Looks like the debugger is running from your screenshot - press [Shift]+[F5] (or in the top menu bar go Debug -> "Stop Debugger") – Mathias R. Jessen Apr 06 '21 at 15:52
  • @MathiasR.Jessen same thing happens. It was in debugger so I could pull the live values out and see if I could find the issue. – Dave Apr 06 '21 at 16:22
  • 1
    @mklement0 I do love VS Code. It is what I'm using for development. The machine I'm deploying it on just happened to have ISE on it for me to look at some stuff. Mind posting that as the answer so I can mark it? – Dave Apr 06 '21 at 16:23
  • Glad to hear it, @Dave. If you mean I should post the general ISE info as an answer: I think it's fine as a comment, given that it doesn't address your specific symptom. Given that some people still use the ISE (even though I wouldn't recommend it), it would be good to understand what the cause of your symptom is (and I have no idea). – mklement0 Apr 06 '21 at 16:26
  • Humor me and run this. What do you get? $username = 'TestUser' $domain = 'MyDomain.com' $uniqueMailbox = "$userName" + '@' + "$domain" – Aaron Apr 07 '21 at 04:20
  • If my line above works the type Get-Alias '@'. – Aaron Apr 07 '21 at 04:28

0 Answers0