• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 118
  • Last Modified:

Powershell Script is partially skipped

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
Loyall
Asked:
Loyall
1 Solution
 
Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
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
 
LoyallAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now