Solved

Powershell Script is partially skipped

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

Accepted Solution

by:
Rich Weissler earned 500 total points
Comment Utility
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
Comment Utility
Hi Rich.

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now