Solved

Powershell Script is partially skipped

Posted on 2015-02-23
2
107 Views
Last Modified: 2015-02-23
Hi,

I made a script that handles exterminated users.
The biggest part of the script works great, but some specific part is not processed.
It's like Powershell just runs over it.
I think i am making some very basic mistake, but can't figure out what.

I copied the script down below.
The "problem" part is marked with  "############"

Import-module ActiveDirectory
cls
do	#Afsluiten
{
write-host ""
	do #Again
	{
	$Input = Read-Host "`nUsername is"
		$User = Get-ADUser -LDAPFilter "(sAMAccountName=$Input)"
		If ($User -eq $Null) 
			{
				Write-Host "`nNo one with this username: $Input" -Foregroundcolor Red
			}

		Else
		{ 
			#Set user variabelen
			$Name = Get-Aduser $Input -property 'name' | Select -ExpandProperty Name
			$Sam = Get-Aduser $Input -property 'Samaccountname' | Select -ExpandProperty Samaccountname
			$GivenName = Get-Aduser $Input -property 'Givenname' | Select -ExpandProperty Givenname
			$SurName = Get-Aduser $Input -property 'Surname' | Select -ExpandProperty Surname
				
 			write-host "================================================"
			Write-Host "`nUsername:$Input" -Foregroundcolor Yellow
			write-host "`nName:$GivenName $Surname" -Foregroundcolor Yellow
			write-host ""
			write-host "================================================"
				#Delete confirmation
				$Confirm = Read-Host "Are you sure you want to cleanup this account ? (J/N)"
				if($Confirm -eq "N")
					{
						$Answer = Read-Host "Do ypou want to terminate this script ? (J/N)"
							If ($Answer -eq "J")
								{
									write-host "The script is terminated" -Foregroundcolor Cyan
									Exit
								}
					}
				
				elseif($Confirm -eq "J")	
				{
					#Set Exchange Variabelen
					$identity = "domain.lan\$Sam"
					$internalmessage = get-content "C:\Manage\Terminated_Users\internalmessage.txt"
					
					#Set Homedrive variabelen
					$Homedrive = ("\\srv001\Home$\" + $sam)
					$Homeexists = Test-Path $Homedrive
					$Date = Get-Date -format Y
					$Archief = ("\\srv001\Home$\_disabled" + $Date)
					$Archiefexists = Test-Path $archief
					
						#EXCHANGE Zet Autoreply aan.
						Write-host "`n[INFO] Turn AutoReply on"
						Set-MailboxAutoReplyConfiguration -Identity $identity -AutoReplyState Enabled -InternalMessage $internalmessage -ExternalMessage $internalmessage
					
						#EXCHANG Disable mailbox
						#Write-host "`n[INFO] Disable Mailbox" 
						#Disable-Mailbox -Identity $identity -Confirm:$false
					
						#EXCHANGE Verberg gebruiker in adresboek
						Write-host "`n[INFO] Hide user in Adressbook"
						Set-Mailbox -Identity domain.lan\$Sam -HiddenFromAddressListsEnabled $true	
						
###############################################################################						
						If ($Homeexists -eq $False)
							{
								Write-host "[WARNING]`nNo such folder: $Homedrive." -ForeGroundColor Red
								ElseIf ($archiefexists -eq $False)
								{
									Write-Host "Archief: $archief is being created." -ForeGroundColor Darkyellow
									mkdir $archief
									Move-Item $Homedrive $archief -recursive
									Write-Host "[INFO] Homedrive of $GivenName $Surname is archived" -ForeGroundColor Yellow
								} 
									Else 
									{ 
										Move-Item $Homedrive $archief -recursive
										Write-Host "[INFO] Homedrive of $GivenName $Surname is archived" -ForeGroundColor Yellow
									}
							}			
###############################################################################					
					write-host "================================================"
					write-host "`nAccount $GivenName $Surname is disabled" -Foregroundcolor Green
					write-host "`n"
			
				} 
		}
		$Again = Read-Host "Disable another user ? (J/N)"
			If ($Again -eq "N")
				{
				write-host "The script is terminated" -Foregroundcolor Cyan
				Exit
				}
		
	}
    while ($Again -eq "J")
}
while ($Answer -eq "N")

Open in new window

0
Comment
Question by:Loyall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

Accepted Solution

by:
Rich Weissler earned 500 total points
ID: 40625817
If I'm reading the code correctly...
You're saying -- if $Homeexists is false, execute that block of code.  So if it's true, it skips to the end of the block.  Now, inside that block, you have an elseif... which I assume you want to check if $Homeexists is true... but you're skipping it, 'cause it's inside the code block.
... or am I misreading?
0
 
LVL 2

Author Comment

by:Loyall
ID: 40625915
Hi Rich.

You are right !
So silly. Errors are always obvious when you see them :-)
Thanks !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question