PowerShell – Get Disconnected Mailbox in Exchange Server

If you delete a AD user account it will automatically disconnect the connected mailbox from the AD account and add it to the Disconnected Mailbox list.

Mailbox  will be listed until retention period of 30 days(unless otherwise set)

Get-MailboxStatistics -server <servername> | where { $_.DisconnectDate -ne $null } | select DisplayName,MailboxGuid,Database,DisconnectDate

Gets the list of Disconnected mailbox in the specified Exchange 2007 Server

Get-MailboxStatistics  | where { $_.DisconnectDate -ne $null } | select DisplayName,MailboxGuid,Database,DisconnectDate

 

Create FTP user (Powershell GUI)

Here is a small PowerShell script that I have written today, this is a GUI interface script that will help you create a FTP user with folder and will configure the permissions fore IIS:

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

##########################################################
##							##
##		--	SIGNATURE --			##
##							##
$Company =	'PKM-Technology'			##
$PoweredBye =	'Pouyan Khabazi'			##
$Date =		'30-05-2013'				##
$Version =	'V1'					##
##							##
##							##
##########################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')

$FieldBackGround = [System.Drawing.Color]::'Transparent'
$objForm = New-Object System.Windows.Forms.Form
	$objForm.Text = $Company + ' Create FTP User ' + $Version + ' | (' + $PoweredBye + ')'
	$objForm.Size = New-Object System.Drawing.Size(400,400)
	$objForm.MinimizeBox = $false
	$objForm.MaximizeBox = $false
	$objForm.ControlBox = $false
	$objForm.StartPosition = 'CenterScreen'
	$objForm.FormBorderStyle = 'FixedDialog'
	$objForm.KeyPreview = $True
	$objForm.AllowTransparency = $true
##Styling
$Font = New-Object System.Drawing.Font('Times New Roman','9')

$LabelUserName = New-Object System.Windows.Forms.Label
	$LabelUserName.Location = New-Object System.Drawing.Size(20,150) 
	$LabelUserName.Size = New-Object System.Drawing.Size(80,20)
	$LabelUserName.BackColor = $FieldBackGround
	$LabelUserName.Text = 'User name: '
	$objForm.Controls.Add($LabelUserName)
$TextboxUserName = New-Object System.Windows.Forms.TextBox
	$TextboxUserName.Location = New-Object System.Drawing.Size(100,150) 
	$TextboxUserName.Size = New-Object System.Drawing.Size(200,25)
	$TextboxUserName.Text = ''
	$objForm.Controls.Add($TextboxUserName)

$LabelFullName = New-Object System.Windows.Forms.Label
	$LabelFullName.Location = New-Object System.Drawing.Size(20,180) 
	$LabelFullName.Size = New-Object System.Drawing.Size(80,20)
	$LabelFullName.BackColor = $FieldBackGround
	$LabelFullName.Text = 'Full name: '
	$objForm.Controls.Add($LabelFullName)
$TextboxFullName = New-Object System.Windows.Forms.TextBox
	$TextboxFullName.Location = New-Object System.Drawing.Size(100,180) 
	$TextboxFullName.Size = New-Object System.Drawing.Size(200,25)
	$TextboxFullName.Text = ''
	$objForm.Controls.Add($TextboxFullName)

$LabelPassword = New-Object System.Windows.Forms.Label
	$LabelPassword.Location = New-Object System.Drawing.Size(20,210) 
	$LabelPassword.Size = New-Object System.Drawing.Size(80,20)
	$LabelPassword.BackColor = $FieldBackGround
	$LabelPassword.Text = 'Password: '
	$objForm.Controls.Add($LabelPassword)
$TextboxPassword = New-Object System.Windows.Forms.TextBox
	$TextboxPassword.Location = New-Object System.Drawing.Size(100,210) 
	$TextboxPassword.Size = New-Object System.Drawing.Size(200,25)
	$TextboxPassword.Text = ''
    $TextboxPassword.PasswordChar = '*'
	$objForm.Controls.Add($TextboxPassword)

$ButtonCreate = New-Object Windows.Forms.Button
	$ButtonCreate.Location = New-Object System.Drawing.Size(100,320) 
	$ButtonCreate.Size = New-Object System.Drawing.Size(100,25) 
	$ButtonCreate.text = 'Create user'
	$ButtonCreate.add_click({
        $userName = $TextboxUserName.Text
        $fullName = $TextboxFullName.Text
        $password = $TextboxPassword.Text
        $ftpFolder = "C:inetpubftproot" + $userName

        $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"
        #Local user if user exict
        $x = $Computer.psbase.Children | Where-Object {$_.psbase.schemaclassname -eq "user"} | select Name
         foreach ($user in $x) {
          if ($user.Name -eq $userName){
            #Write-Host User $user.name already exists
            $d = [Windows.Forms.MessageBox]::Show('User ' + $userName + ' already exists, please select another username!', [Windows.Forms.MessageBoxButtons]::OK)
            Return
          }
        }
        # Local user account creation:
        $TestUser = $Computer.Create("User", $userName)
        $TestUser.SetPassword($password)
        $TestUser.SetInfo()
        $TestUser.FullName = $fullName
        $TestUser.SetInfo()
        $TestUser.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
        $TestUser.SetInfo()
        # Add user to local group
        $group = [ADSI]"WinNT://./FTP_USERS,group"
        $group.Add("WinNT://$userName,user")
        # Create the FTP folder
        New-Item -ItemType directory -Path $ftpFolder -Force
        # Set permission on the folder
        $inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
        $propagation = [system.security.accesscontrol.PropagationFlags]"None"
        $acl = Get-Acl $ftpFolder
        $accessrule = New-Object system.security.AccessControl.FileSystemAccessRule($userName, "Modify", $inherit, $propagation, "Allow")
        $acl.AddAccessRule($accessrule)
        Set-Acl -AclObject $acl $ftpFolder

        $d = [Windows.Forms.MessageBox]::Show('User '+ $userName +' is created successfully! 
Do you want to close application?', 'Exit', [Windows.Forms.MessageBoxButtons]::YesNo)
		if ($d -eq [Windows.Forms.DialogResult]::Yes){
			$objForm.Close()
		} else {}
    })
    $objForm.controls.add($ButtonCreate)

##CLOSE BUTTON
$CancelButton = New-Object System.Windows.Forms.Button
	$CancelButton.Location = New-Object System.Drawing.Size(220,320)
	$CancelButton.Size = New-Object System.Drawing.Size(75,25)
	$CancelButton.Text = 'Close'
	$CancelButton.Add_Click({
		$d = [Windows.Forms.MessageBox]::Show('Are you shore you want to close the application?', 'Exit', [Windows.Forms.MessageBoxButtons]::YesNo)
		if ($d -eq [Windows.Forms.DialogResult]::Yes){
			$objForm.Close()
		} else {}	
	})
	$objForm.Controls.Add($CancelButton)

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Well you can use the script to start the GUI both you can also convert this to a EXE file, to convert the script to a EXE you can  download PS2EXE

Now you can run the coede below to confert the PS1 file to a EXE file

  • Start powershell in version 2 (if you are running version 3)
    powershell.exe -version 2
  • Browse to the folder where you unzipt the PS2EXE and run the code
    cd c:PS2EXE
    ps2exe.ps1 -inputFile C:ScriptsFTPuser.PS1 -outputFile C:ScriptsFTPuser.exe -noConsole

~pouyan

PowerShell – WmiObject and a corrupt repository

If repository is found to be inconsistent, for Vista and newer, run from elevated command prompt:

Winmgmt /salvagerepository

Note this command will take the content of the inconsistent repository and merge it into the rebuilt repository if it is readable

If the above doesn’t work, then run:

Winmgmt /resetrepository

Note this will reset repository to the initial state when the OS was first installed

~pouyan

Using PowerShell as the Shell on Windows Server Core

With PowerShell becoming one of the most important tools in a Windows administrator’s tool bag as well as really becoming the best prompt to use on Windows Server Core, I thought it was about time I documented how to change the command prompt that opens when you log into Server Core with a PowerShell prompt.

To complete this requires two steps:

  1. Enable/Install PowerShell on the Windows 2008 R2 Server Core computer
  2. Update the HKLMSoftwareMicrosoftWindows NTCurrentVersionWinLogonShell registry key replacing cmd.exe with PowerShell.exe

To enable PowerShell run SCONFIG from the command prompt and then select menu options 4 then 2. This will require the computer to be rebooted

Once rebooted login and in the CMD prompt type PowerShell. At the PowerShell prompt type:

SET-ITEMPROPERTY "HKLM: SOFTWAREMICROSOFTWINDOWS NTCURRENTVERSIONWINLOGON" SHELL POWERSHELL.EXE

Having completed above log off and back on and a powershell prompt will open instead.

~Pouyan

Set Computer Name using PowerShell

Yesterday I faced a problem in which I need to change the computer name using PowerShell. First I was looking for some cmdlet which can do this job for me but there is not any builtin cmdlet for it.
This method is using WMI to do the task.
Get-WmiObject Win32_ComputerSystem

I am using Get-WMI to give me all the value of Win32_ComputerSystem.

You can see in the Output that the “Name” value contain our Computer name.
$computerName = Get-WmiObject Win32_ComputerSystem

After putting the command in to the $computerName variable, you can use the rename method to rename the computer.

$computername.Rename($name)

The Win32_ComputerSystem WMI class contain the method of .Rename() in which we need to provide a desired computer name in brackets.

~Pouyan