Solved

Powershell Script is partially skipped

Posted on 2015-02-23
2
105 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…

808 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