We help IT Professionals succeed at work.
Get Started

Powershell - Script weirdness

Last Modified: 2015-12-19
So I have the code below, generated a couple years back with help from the EE community.  However it's acting weird.. I'm trying to convert this into an EXE for distribution to the rest of the IT department.  If I run the code from within the ISE it performs exactly as it should... completing 100% successfully.
However if I run the converted (ps2exe.ps1) executable it completes successfully but never populates a few specific attributes: streetaddress, city, zip and scriptpath.  Also if I call the new_user.ps1 from a Powershell window (opened as admin) it does the exact same thing.  Performs all functions except the streetaddress, city, zip and scriptpath.

#3/3/15 Benjamin Hart, Unified Brands, Inc
#Created with Powershell ISE
#This powershell script will create a domain user object using a format of lastname, firstname, a SAM of first initial + last name
#It will also populate displayname, a default password, office and both proxyaddresses, the primary as used in your org and the 
#Dover required O365 one.  It will also verify the primary proxy address is not already used.
#With set-aduser you can alter almost any attribute of the user.

$theOU = read-host "Enter the OU name"
$Surname = read-Host "Enter the surname"
$GivenName = read-host "Enter first name"
$DisplayName = "$Surname, $GivenName"
$Password = "December1"
$name = $GivenName.substring(0,1)+$Surname
$proxyaddress = read-host "Enter the proxy address in full"

Import-Module activedirectory
import-module servermanager

	#Edit the SearchBase to match your organization
	$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=Domain,DC=Root01,DC=org'
#Below verifies the Proxyaddress is not already present	
Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress}
	$found=Get-ADuser  -filter * -Properties ProxyAddresses|
        $_.proxyaddresses | 
             Where-Object{ $_ -eq $ProxyAddress }}

while (Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress})
  $proxyaddress = read-host "$proxyaddress is already in use, please try another one"
Write-Host "$proxyaddress is not used yet."

#Edit your locations if you choose to use this part
Switch ($Office)  {
    "Michigan" {
        $Street = "123 Main st"
        $City = "Weidman"
        $State = "Michigan"
        $Zip = "48898"
        $scriptpath = "\\difc\netlogon\milogin1.bat"
    "Mississippi"  {
        $Street = "123 Main st"
        $City = "Jackson"
        $State = "Mississippi"
        $Zip = "39272"
        $scriptpath = "\\difc\netlogon\adlogin.bat"
    "Oklahoma"  {
        $Street = "123 Main st"
        $City = "Pryor"
        $State = "Oklahoma"
        $Zip = "74361"
        $scriptpath = "\\difc\netlogon\oklogin.bat"
    "Georgia"  {
        $Street = "123 Main st"
        $City = "Conyers"
        $State = "Georgia"
        $Zip = "30013"
#Edit the below to match your domain(s)
$DoverProxyAddress = "$($givenname.substring(0,1))$surname-$("unifiedbrands")-$("net")@company.mail.onmicrosoft.com"
$Description = read-host "Enter persons description"
$jobtitle = read-host "Enter the Job Title"
#Edit the below to match your locations
$office = read-host "Enter the user's location, Michigan, Mississippi, Georgia, Oklahoma or Remote"
$department = read-host "Enter the users Department"

New-ADUser -path $myOU -samaccountname $name -name $displayname -DisplayName $DisplayName -Surname $Surname -givenname $givenname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -enabled:$false
set-aduser $name -emailaddress $proxyaddress -Description $Description -Title $jobtitle -Office $office -UserPrincipalName $proxyaddress -Department $department -Company "Company, Inc"
set-aduser $name -StreetAddress $Street -city $city -state $state -PostalCode $zip
set-aduser $name -ScriptPath $scriptpath 
Set-ADUser $name -Replace @{employeeType="EMPLOYEE"}
set-aduser $name -add @{proxyaddresses = ("SMTP:")+"$proxyaddress"}
set-aduser $name -add @{ProxyAddresses = ("smtp:")+"$doverproxyaddress"}

get-aduser $name 

Open in new window

Watch Question
Top Expert 2014
This problem has been solved!
Unlock 2 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE