DRIVE EFFICIENCY THROUGH AUTOMATED IT.
SAVE COST THROUGH CONSOLIDATION OF IT.
WANT TO KNOW MORE ABOUT STRATEGIC CONSULTING CLICK HERE.
MICROSOFT / RISUAL HYPER-V CLOUD EVENT 22ND MARCH 2011 CLICKHERE.

Archive

Posts Tagged ‘Powershell’

Exchange 2010 – Script to find unhealthy content indexes

April 2nd, 2012 paulw Comments off

I was asked by an Exchange Administrator who was new to Exchange 2010 to see if I could set up a simple script that would check the state of the Exchange databases that may affect it failing over in the event of an outage.

The following script simply checks the state of each database copy and the state of the context index. This can be copied and pasted into a text file and saved as a PS1 file and run in an Exchange Management PowerShell window:

###############################################################

write-host "Mailbox Databases that are not healthy or mounted" -foregroundcolor yellow

$mounted = get-mailboxdatabase | Get-MailboxDatabaseCopyStatus | Where-Object {$_.Status -ne "Healthy"}
$failed = $mounted | Where-Object {$_.Status -ne "Mounted"}

$failed | ForEach-Object {$db_count++}

$db_total = $db_count -1

If ($db_total -eq 0)
{
Write-Host `n
write-host "All database states are ok" -foregroundcolor green
Write-Host `n
}
ElseIf ($db_total -gt 0)
{
Write-Host `n
write-host "Databases need reseeding, mounting or resuming replication" -foregroundcolor red
$failed
Write-Host `n
}

Write-Host `n
Write-Host `n

write-host "Mailbox Databases that have unhealthy content indexes" -foregroundcolor yellow

$indexes = get-mailboxdatabase | Get-MailboxDatabaseCopyStatus | Where-Object {$_.ContentIndexState -ne "Healthy"}

$indexes | ForEach-Object {$index_count++}

$index_total = $index_count -1

If ($index_total -eq 0)
{
Write-Host `n
write-host "All database indexes are ok" -foregroundcolor green
Write-Host `n
}
ElseIf ($index_total -gt 0)
{
Write-Host `n
write-host "Database context indexes are failed" -foregroundcolor red
$indexes
Write-Host `n
}

###############################################################

The script will simply output like this if there is no problems:

image

If there are any problems, it will list out the indexes or databases that are not reporting as they should be and you can take the corrective actions accordingly.

Cheers

Paul

Using PowerShell to create printers

February 14th, 2012 paulw Comments off

Recently I had the need to create shared printers on a Windows Server 2008 R2 print server. The naming convention had to be strict and it was taking some time to set up the printers manually. After looking on the web I came across this site (http://poshcode.org/1462) which gave me the following code to set up the printers via PowerShell calling out the Win32_Printer WMI:

 

function CreatePrinterPort {
$server = $args[0]
$port = ([WMICLASS]“\.ROOTcimv2:Win32_TCPIPPrinterPort”).createInstance()
$port.Name= $args[1]
$port.SNMPEnabled=$false
$port.Protocol=2
$port.HostAddress= $args[2]
$port.Put()
}

function CreatePrinter {
$server = $args[0]
$print = ([WMICLASS]“\.ROOTcimv2:Win32_Printer”).createInstance()
$print.drivername = $args[1]
$print.PortName = $args[2]
$print.Shared = $true
$print.Published = $false
$print.Sharename = $args[3]
$print.Location = $args[4]
$print.Comment = $args[5]
$print.DeviceID = $args[6]
$print.Put()
}

$printers = Import-Csv “printers.csv”

foreach ($printer in $printers) {
CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername
}

I first populated the CSV file with the following headers:

Printserver, Driver, Portname, IPAddress, Sharename, Location, Comment, Printername

I then put in the values required for each of the printers that I wanted to set up and in many of the fields used copy and paste to save time.

I then ran the script under a PowerShell that was run as Administrator and the following is outputted when creating the printer:

image

The script will create the TCPI/IP port as needed and set up the printer according to the settings that you put in the CSV file.

One thing to note is the TCP/IP port that gets created in this script has been hardcoded to 2 which means create a LPT port. Changing this to 1 will create a RAW port.

Stay tuned for more blogs on using the Win32_Printer command.

Cheers

Paul

Running Exchange PowerShell as Scheduled Task

December 1st, 2011 paulw Comments off

If you want to run any of the Exchange PowerShell commands from a standard PowerShell environment then you simply need to add in the following line in order to run it as if it is an Exchange Management Console:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

For example, the script below will output the size of the Exchange Databases to a file and can be run as a scheduled task:

 

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Get-MailboxDatabase –Status | fl name, databasesize | out-file C:dbsize.txt

 

What we can do then is save the above script to a .ps1 file and then edit the action to start PowerShell.exe and put the arguments as the script that you want to run:

image

This can be set up with the usual Scheduled Task settings and with the PowerShell snap-in for Exchange added in should be able to use the Exchange PowerShell commands.

Cheers

Paul

Importing Users using PowerShell

November 3rd, 2011 paulw Comments off

We had a request from on of our clients where they wanted to create new user accounts for around 50 new employees. In order to do this we created a simple PowerShell script that used a populated CSV file to create the users in a certain OU and with a default password.

The CSV file had the following headings:

image

After saving it to a location we ran the following PowerShell script that created the users:

 

import-module activedirectory
$inputFile = Import-CSV  C:usersToBeCreated.csv

foreach($line in $inputFile)
{
new-aduser -SamAccountName $line.UserName -Name $line.FullName -AccountPassword (ConvertTo-SecureString -AsPlainText "Password" -Force) -Enabled $true -Path "OU=Domain Users,DC=TEST,dc=LOCAL" -DisplayName $line.FullName -GivenName $line.FirstName -Surname $line.SurName -UserPrincipalName $line.UserPrincipalName -ChangePasswordAtLogon $True
}

 

You can copy and paste the script above where you will only need to change the bold text which is the CSV location, the temporary password for the new users and the OU that you want to put the users into.

As long as you use the same headings in your CSV file then this should work ok. You can, of course add in more details that are accepted by the new-aduser command which are outlined in the URL below:

http://technet.microsoft.com/en-us/library/ee617253.aspx

Cheers

Paul

Categories: Uncategorized Tags:

Remote Reboot via PowerShell

August 19th, 2011 Jovan Davis Comments off

Below is PowerShell command which can be run to force a reboot on a remote machine:

(gwmi win32_operatingsystem -ComputerName ComputerName -cred (get-credential)).Win32Shutdown(6)

Categories: Uncategorized Tags: ,

Record your PowerShell session

August 10th, 2011 Daniel Davies Comments off

There is a quick way to record your session in PowerShell to a text file so you have a record of every command you have typed.

In PowerShell run the following command (Change location to wherever you would like)

Start-Transcript c:Powershell.txt –Append

Now use PowerShell as you usually would, once you have finished with PowerShell run the following command “Stop-Transcript”

If you now open the “PowerShell.txt” you will see everything you have just done in your PowerShell session.

**********************
**********************
Windows PowerShell Transcript Start
Start time: 20110810173323
Username  : Test
Machine      : Test (Microsoft Windows NT 6.1.7600.0)
**********************
Transcript started, output file is c:MySession.txt
[PS] C:>Get-Mailbox –Identity Dan

Name                     Alias                ServerName
—-                         —–                 ———-
Dan                        Dan                  Test

[PS] C:>Get-User -Identity risual

Name               RecipientType
—-                   ————-
Dan                  UserMailbox

[PS] C:>Stop-Transcript

Categories: Uncategorized Tags: ,

Remote Shutdown via PowerShell

July 11th, 2011 Jovan Davis Comments off

Below is PowerShell command which can be run to force shutdown on a remote machine:

(gwmi win32_operatingsystem -ComputerName ComputerName -cred (get-credential)).Win32Shutdown(5)

Categories: Uncategorized Tags: ,

Exchange 2010 Mailbox Sizes in One Command

June 22nd, 2011 paulw Comments off

Here is a good command to use if you want to find out the size of all your mailbox databases in Exchange 2010:

Get-MailboxDatabase –Status | fl name, databasesize

It should output something similar to this:

image

You can simply copy and paste the above command into an Exchange 2010 PowerShell window and run. Hope this helps.

Paul

Enable/Disable a Service via PowerShell

June 13th, 2011 Daniel Davies Comments off

Here’s a few quick PowerShell commands that will allow you to Disable or Enable a Service and Start or Stop a particular service.

To Enable a particular service run the following command. (Please Edit ServiceName to the desired service)

Set-Service ServiceName -StartupType Automatic

To Disable a particular service run the following command.

Set-Service ServiceName -StartupType Disabled

To Stop a particular service run the following command.

Stop-Service ServiceName

To Start a particular service run the following command.

Start-Service ServiceName

You can also combine the commands in a ps1 file , for example if you want to Disable and Stop a Service you would run the following.

“Set-Service ServiceName -StartupType Disabled
Stop-Service ServiceName”

Categories: Uncategorized Tags: ,

Force Logoff on Remote Machine Via PowerShell

June 8th, 2011 Daniel Davies Comments off

Just a quick command here to logoff remote machines via PowerShell

“(gwmi win32_operatingsystem -ComputerName ComputerName -cred (get-credential)).Win32Shutdown(4)”

This will log every user off the machine specified above.

Categories: Uncategorized Tags: ,