Creating and using Password Hashes and Secure Strings with Powershell

This is how you can generate a Secure String with powershell and use it in your scripts

secureString = Read-Host -AsSecureString
ConvertFrom-SecureString $secureString | out-file c:\temp\encrypted.txt
$newString = gc C:\temp\encrypted.txt | ConvertTo-SecureString

Load the Secure string from file and use it in your script:

$securePassword = Get-Content c:\temp\encrypted.txt | ConvertTo-SecureString

How to uninstall with msiexec using product id guid

You can find the product code by perusing the registry from this base key: HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall . Press F3 and search for your product name. (If it’s a 32-bit installer on a 64-bit machine, it might be under HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall instead).

Finally, you can find the product code also by using PowerShell:

get-wmiobject Win32_Product | Format-Table IdentifyingNumber, Name

when you found the Guid ID you can use msiexec to uninstall the application

msiexec /x {A4BFF20C-A21E-4720-88E5-79D5A5AEB2E8}

 

PowerShell – How to find details of Operating System

There were several instances where I had to find details of the operating system using PowerShell. Here, I am providing several PowerShell snippets that return various details of the Operating System.

Name of the Operating System

(Get-WmiObject Win32_OperatingSystem).Name

Is Operating System 32-bit or 64-bit

(Get-WmiObject Win32_OperatingSystem).OSArchitecture

Name of the Machine

(Get-WmiObject Win32_OperatingSystem).CSName

There are many more properties of the Operating System that are exposed. To obtain more details, run the following

Get-WmiObject Win32_OperatingSystem | Get-Member

 

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