Solved

Powershell Script is partially skipped

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
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…

730 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