Posted in Windows Powershell | No Comment | 20,720 views | 27/05/2013 10:35
I’ll show you how to get total elapsed time for processes in same output with using Label and Expression function of PowerShell. You can see the command below:
(Get-Process | ft Name,@{label="Elapsed Time";expression={[System.Math]::Round(((Get-Date)-$_.StartTime).TotalSeconds)}}) |
(Get-Process | ft Name,@{label="Elapsed Time";expression={[System.Math]::Round(((Get-Date)-$_.StartTime).TotalSeconds)}})
You can execute additional commands with Expression function in same PowerShell command.
Posted in Exchange Server, Windows Powershell | No Comment | 2,934 views | 24/05/2013 14:02
You can get your db statistics like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| # PoSHStats Pipeline
function PoSHStats-Pipeline
{
# Create Timer
Begin { $i=0; $Timer = [Diagnostics.StopWatch]::StartNew() }
# Process Items
Process { $i++; $_ }
# Informational Output
End { Write-Debug "Processed $i items in $($Timer.Elapsed.TotalSeconds) seconds." }
}
$ExchangeServer = "EXMB04"
# Get MailboxDatabase
if ($ExchangeServer.IsMailboxServer -eq $True)
{
Get-MailboxDatabase -Status | where {$_.Server -eq "$ExchangeHost"} | PoSHStats-Pipeline | %{
# Database Statistics
$DBName = $_.Name
$DBSize = $_.DatabaseSize.ToGB()
$DBLastFullBackup = $_.LastFullBackup.ToString()
$DBWhiteSpace = $_.AvailableNewMailboxSpace.ToMB()
# Mailbox Statistics
$MBStats = Get-MailboxStatistics -Database $DBName | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
$MBCount = $MBStats.Count
$MBAvg = ([math]::round(($MBStats.Average), 0))
# Mailbox Database Disk Usage
$DBDrive = $_.EdbFilePath.DriveName
$DBDiskInfo = Get-WmiObject -ComputerName $ExchangeHost -Class Win32_LogicalDisk | Where {$_.DeviceID -eq $DBDrive}
$DBDiskSize = ([math]::round(($DBDiskInfo.Size / 1GB), 0))
$DBDiskFreeSpace = ([math]::round(($DBDiskInfo.FreeSpace / 1GB), 0))
# Exchange Database Disk Usage
[int]$ExchangeDatabaseSize = [int]$ExchangeDatabaseSize + [int]$DBSize
[int]$ExchangeDatabaseWhiteSpace = [int]$ExchangeDatabaseWhiteSpace + [int]$DBWhiteSpace
# Database White Space
[int]$DatabaseWhiteSpace = ([math]::round(($DBWhiteSpace / 1KB), 0))
# Exchange Server Disk Usage
$TestDrive = $DiskDrives.Contains("$DBDrive")
if ($TestDrive -ne $True)
{
$DiskDrives.Add("$DBDrive") | Out-Null
[int]$ExchangeDatabaseDiskSize = [int]$ExchangeDatabaseDiskSize + [int]$DBDiskSize
[int]$ExchangeDatabaseDiskFreeSpace = [int]$ExchangeDatabaseDiskFreeSpace + [int]$DBDiskFreeSpace
}
# Host Total Database Size
[int]$HostTotalDatabaseSize = [int]$HostTotalDatabaseSize + [int]$DBSize
# Host Total Database Count
[int]$HostTotalDatabaseCount++ | Out-Null
# Host Total Mailbox Count
[int]$HostTotalMailboxCount = [int]$HostTotalMailboxCount + [int]$MBCount
# Host Total Mailbox Average
[int]$HostTotalMailboxAverage = [int]$HostTotalMailboxAverage + [int]$MBAvg
}
} |
# PoSHStats Pipeline
function PoSHStats-Pipeline
{
# Create Timer
Begin { $i=0; $Timer = [Diagnostics.StopWatch]::StartNew() }
# Process Items
Process { $i++; $_ }
# Informational Output
End { Write-Debug "Processed $i items in $($Timer.Elapsed.TotalSeconds) seconds." }
}
$ExchangeServer = "EXMB04"
# Get MailboxDatabase
if ($ExchangeServer.IsMailboxServer -eq $True)
{
Get-MailboxDatabase -Status | where {$_.Server -eq "$ExchangeHost"} | PoSHStats-Pipeline | %{
# Database Statistics
$DBName = $_.Name
$DBSize = $_.DatabaseSize.ToGB()
$DBLastFullBackup = $_.LastFullBackup.ToString()
$DBWhiteSpace = $_.AvailableNewMailboxSpace.ToMB()
# Mailbox Statistics
$MBStats = Get-MailboxStatistics -Database $DBName | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
$MBCount = $MBStats.Count
$MBAvg = ([math]::round(($MBStats.Average), 0))
# Mailbox Database Disk Usage
$DBDrive = $_.EdbFilePath.DriveName
$DBDiskInfo = Get-WmiObject -ComputerName $ExchangeHost -Class Win32_LogicalDisk | Where {$_.DeviceID -eq $DBDrive}
$DBDiskSize = ([math]::round(($DBDiskInfo.Size / 1GB), 0))
$DBDiskFreeSpace = ([math]::round(($DBDiskInfo.FreeSpace / 1GB), 0))
# Exchange Database Disk Usage
[int]$ExchangeDatabaseSize = [int]$ExchangeDatabaseSize + [int]$DBSize
[int]$ExchangeDatabaseWhiteSpace = [int]$ExchangeDatabaseWhiteSpace + [int]$DBWhiteSpace
# Database White Space
[int]$DatabaseWhiteSpace = ([math]::round(($DBWhiteSpace / 1KB), 0))
# Exchange Server Disk Usage
$TestDrive = $DiskDrives.Contains("$DBDrive")
if ($TestDrive -ne $True)
{
$DiskDrives.Add("$DBDrive") | Out-Null
[int]$ExchangeDatabaseDiskSize = [int]$ExchangeDatabaseDiskSize + [int]$DBDiskSize
[int]$ExchangeDatabaseDiskFreeSpace = [int]$ExchangeDatabaseDiskFreeSpace + [int]$DBDiskFreeSpace
}
# Host Total Database Size
[int]$HostTotalDatabaseSize = [int]$HostTotalDatabaseSize + [int]$DBSize
# Host Total Database Count
[int]$HostTotalDatabaseCount++ | Out-Null
# Host Total Mailbox Count
[int]$HostTotalMailboxCount = [int]$HostTotalMailboxCount + [int]$MBCount
# Host Total Mailbox Average
[int]$HostTotalMailboxAverage = [int]$HostTotalMailboxAverage + [int]$MBAvg
}
}
This is the preview code of PoSHStats Exchange Server module.
Posted in Windows Powershell | No Comment | 2,010 views | 24/05/2013 10:15
In this example, you can’t open notepad.exe more than once. New notepad.exe processes will be killed.
1
2
3
4
5
6
7
8
9
| $ShouldProcess = $true
While ($ShouldProcess)
{
$Processes = Get-Process -Name Notepad -EA SilentlyContinue
if ($Processes.Count -gt "1")
{
Get-Process -Name Notepad | Sort-Object StartTime | Select-Object -Skip 1 | Stop-Process
}
} |
$ShouldProcess = $true
While ($ShouldProcess)
{
$Processes = Get-Process -Name Notepad -EA SilentlyContinue
if ($Processes.Count -gt "1")
{
Get-Process -Name Notepad | Sort-Object StartTime | Select-Object -Skip 1 | Stop-Process
}
}
Also it’s possible to add delay between process kills.
Posted in Windows Powershell, Windows Server | No Comment | 2,693 views | 24/05/2013 09:33
First, you need to install PSTools on your server. Copy psexec.exe and pskill.exe into C:\ directory.
After that you can patch servers like this:
1
2
3
4
5
6
7
8
9
10
11
| $Servers = Get-Content "C:\Servers.txt"
foreach ($Server in $Servers)
{
$PSExecPath = "C:\psexec.exe"
$PSKillPath = "C:\pskill.exe"
$Command1 = "C:\cp017534.exe /s"
$Shell1 = $PSExecPath + " \\" + $Server + " " + $Command1
cmd.exe /c $Shell1
$Shell2 = $PSKillPath + " \\" + $Server + " PSEXESVC"
cmd.exe /c $Shell2
} |
$Servers = Get-Content "C:\Servers.txt"
foreach ($Server in $Servers)
{
$PSExecPath = "C:\psexec.exe"
$PSKillPath = "C:\pskill.exe"
$Command1 = "C:\cp017534.exe /s"
$Shell1 = $PSExecPath + " \\" + $Server + " " + $Command1
cmd.exe /c $Shell1
$Shell2 = $PSKillPath + " \\" + $Server + " PSEXESVC"
cmd.exe /c $Shell2
}
cp017534.exe is the hp’s firmware update. It’s located on my servers C:\ directory also.
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | 4 Comments | 4,969 views | 22/05/2013 16:59
In this sample script, I use SCVMM 2012 to get virtual machine list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| $Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$VMInfo = Get-VM $Server
$VMHost = $VMInfo.VMHost
$VMHostName = $VMHost.Name
$VMHostGroup = $VMHost.VMHostGroup
if ($VMInfo.PassThroughDisks)
{
$PassThroughDisk = "True"
}
else
{
$PassThroughDisk = "False"
}
if ($VMHostGroup -like "All Hosts\*")
{
Add-Content -Value $Server -Path C:\Servers.txt
Add-Content -Value $VMHostName -Path C:\VMHosts.txt
Add-Content -Value $PassThroughDisk -Path C:\PassThroughDisks.txt
}
} |
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$VMInfo = Get-VM $Server
$VMHost = $VMInfo.VMHost
$VMHostName = $VMHost.Name
$VMHostGroup = $VMHost.VMHostGroup
if ($VMInfo.PassThroughDisks)
{
$PassThroughDisk = "True"
}
else
{
$PassThroughDisk = "False"
}
if ($VMHostGroup -like "All Hosts\*")
{
Add-Content -Value $Server -Path C:\Servers.txt
Add-Content -Value $VMHostName -Path C:\VMHosts.txt
Add-Content -Value $PassThroughDisk -Path C:\PassThroughDisks.txt
}
}
Also you can filter specific Host Groups like in this sample.
Posted in Exchange Server, Windows Powershell | 2 Comments | 13,183 views | 22/05/2013 16:45
You can get ready and retry queue’s of Exchange Server with this script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| $ExchangeHost = "ExCas01"
$ExchangeServer = Get-ExchangeServer -Identity $ExchangeHost
# Integers
[int]$ExchangeActiveConnections = 0;
[int]$ExchangeRetryQueue = 0;
[int]$ExchangeMessageQueue = 0;
[int]$ExchangeActiveQueue = 0;
[int]$HostMessageQueue = 0;
[int]$HostRetryQueue = 0;
# Get Mail Queue
if ($ExchangeServer.IsHubTransportServer -eq $True)
{
# Message Queue
$MessageQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Ready" -and $_.MessageCount -gt "0"}
# Sum Message Queue
Foreach ($Queue in $MessageQueue)
{
[int]$HostMessageQueue = [int]$HostMessageQueue + [int]$Queue.MessageCount
}
# Retry Queue
$RetryQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Retry" -and $_.MessageCount -gt "0"}
# Sum Retry Queue
Foreach ($Queue in $RetryQueue)
{
[int]$HostRetryQueue = [int]$HostRetryQueue + [int]$Queue.MessageCount
}
# Exchange Queue
[int]$ExchangeRetryQueue = [int]$ExchangeRetryQueue + [int]$HostRetryQueue
[int]$ExchangeMessageQueue = [int]$ExchangeMessageQueue + [int]$HostMessageQueue
[int]$ExchangeActiveQueue = [int]$ExchangeActiveQueue + [int]$HostRetryQueue + [int]$HostMessageQueue
} |
$ExchangeHost = "ExCas01"
$ExchangeServer = Get-ExchangeServer -Identity $ExchangeHost
# Integers
[int]$ExchangeActiveConnections = 0;
[int]$ExchangeRetryQueue = 0;
[int]$ExchangeMessageQueue = 0;
[int]$ExchangeActiveQueue = 0;
[int]$HostMessageQueue = 0;
[int]$HostRetryQueue = 0;
# Get Mail Queue
if ($ExchangeServer.IsHubTransportServer -eq $True)
{
# Message Queue
$MessageQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Ready" -and $_.MessageCount -gt "0"}
# Sum Message Queue
Foreach ($Queue in $MessageQueue)
{
[int]$HostMessageQueue = [int]$HostMessageQueue + [int]$Queue.MessageCount
}
# Retry Queue
$RetryQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Retry" -and $_.MessageCount -gt "0"}
# Sum Retry Queue
Foreach ($Queue in $RetryQueue)
{
[int]$HostRetryQueue = [int]$HostRetryQueue + [int]$Queue.MessageCount
}
# Exchange Queue
[int]$ExchangeRetryQueue = [int]$ExchangeRetryQueue + [int]$HostRetryQueue
[int]$ExchangeMessageQueue = [int]$ExchangeMessageQueue + [int]$HostMessageQueue
[int]$ExchangeActiveQueue = [int]$ExchangeActiveQueue + [int]$HostRetryQueue + [int]$HostMessageQueue
}
You can add multiple Exchange hosts to an array, it’ll just query Hub Transport servers. It only support Exchange Server 2010.
Posted in Windows Powershell | 1 Comment | 17,608 views | 22/05/2013 15:30
In this sample, I’ll show you how to get active Exchange Server CAS connections via PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| $ExchangeHost = "ExCas01"
$ExchangeServer = Get-ExchangeServer -Identity $ExchangeHost
# Get Connections
if ($ExchangeServer.IsClientAccessServer -eq $True)
{
# OWA Connections
$HostOWAConnections = (Get-Counter "\MSExchange OWA\Current Unique Users" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# RPC Connections
$HostRPCConnections = (Get-Counter "\MSExchange RpcClientAccess\User Count" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# POP3 Connections
$HostPOP3Connections = (Get-Counter "\MSExchangePop3(1)\Connections Current" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# IMAP Connections
$HostIMAPConnections = (Get-Counter "\MSExchangeImap4(1)\Current Connections" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# Exchange Connections
[int]$ExchangeActiveConnections = [int]$HostOWAConnections + [int]$HostRPCConnections + [int]$HostPOP3Connections + [int]$HostIMAPConnections
} |
$ExchangeHost = "ExCas01"
$ExchangeServer = Get-ExchangeServer -Identity $ExchangeHost
# Get Connections
if ($ExchangeServer.IsClientAccessServer -eq $True)
{
# OWA Connections
$HostOWAConnections = (Get-Counter "\MSExchange OWA\Current Unique Users" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# RPC Connections
$HostRPCConnections = (Get-Counter "\MSExchange RpcClientAccess\User Count" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# POP3 Connections
$HostPOP3Connections = (Get-Counter "\MSExchangePop3(1)\Connections Current" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# IMAP Connections
$HostIMAPConnections = (Get-Counter "\MSExchangeImap4(1)\Current Connections" -ComputerName $ExchangeHost).CounterSamples[0].CookedValue
# Exchange Connections
[int]$ExchangeActiveConnections = [int]$HostOWAConnections + [int]$HostRPCConnections + [int]$HostPOP3Connections + [int]$HostIMAPConnections
}
You can loop this script to get real time connection statistics.
|