Wednesday, 17 July 2013

Script to find CPU Usage and Process List

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

intMaxAttempts = 50
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPercent1 = CreateObject("Scripting.Dictionary")
Set objTime1 = CreateObject("Scripting.Dictionary")
Set objPercent2 = CreateObject("Scripting.Dictionary")
Set objTime2 = CreateObject("Scripting.Dictionary")

boolRequery = True
intAttempt = 0
On Error Resume Next
Err.Clear
While boolRequery = True And intAttempt <= intMaxAttempts
intAttempt = intAttempt + 1
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent1.Exists(objProcess.Name) = True Then
objPercent1(objProcess.Name) = objPercent1(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent1.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime1.Exists(objProcess.Name) = True Then
objTime1(objProcess.Name) = objTime1(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime1.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next
WScript.Sleep 1000
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent2.Exists(objProcess.Name) = True Then
objPercent2(objProcess.Name) = objPercent2(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent2.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime2.Exists(objProcess.Name) = True Then
objTime2(objProcess.Name) = objTime2(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime2.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next

' Go through each of the dictionary objects and add a zero entry for processes that may not have existed in each query
For Each strProcess In objPercent1
If objPercent2.Exists(strProcess) = False Then objPercent2.Add strProcess, 0
Next
For Each strProcess In objPercent2
If objPercent1.Exists(strProcess) = False Then objPercent1.Add strProcess, 0
Next
For Each strProcess In objTime1
If objTime2.Exists(strProcess) = False Then objTime2.Add strProcess, 0
Next
For Each strProcess In objTime2
If objTime1.Exists(strProcess) = False Then objTime1.Add strProcess, 0
Next

' Caclulate the usage
TotalCPUUsage = 0
HighestProcessName = ""
HighestProcessUsage = 0
For Each strProcess In objPercent1
'CPUPercent = ((objPercent2(strProcess) - objPercent1(strProcess)) / (objTime2(strProcess) - objTime1(strProcess))) * 100
CPUPercent = ((CDbl(objPercent2(strProcess)) - CDbl(objPercent1(strProcess))) / (CDbl(objTime2(strProcess)) - CDbl(objTime1(strProcess)))) * 100
WScript.Echo strProcess & ": " & CPUPercent
TotalCPUUsage = TotalCPUUsage + CPUPercent
If CPUPercent > HighestProcessUsage Then
HighestProcessUsage = CPUPercent
HighestProcessName = strProcess
End If
Next

TotalCPUUsage = Round(TotalCPUUsage, 3)

If Err.Number <> 0 Then
boolRequery = True
Else
boolRequery = False
End If
On Error GoTo 0
Wend

WScript.Echo VbCrLf & VbCrLf & "Highest Process: " & HighestProcessName & ": " & HighestProcessUsage
WScript.Echo VbCrLf & VbCrLf & "Total CPU Usage: " & TotalCPUUsage & VbCrLf & VbCrLf

Tuesday, 16 July 2013

Disk Space report using Power shell

A little while ago, I demonstrated a way to create a fairly simple disk space report using PowerShell that would be e-mailed to you whenever one of your servers started to get low on disk space.


# PowerShell Systems Report
# Example usage: .\SystemsReport.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against

#region Variables and Arguments
$users = 
"youremail@yourcompany.com" # List of users to email your report to (separate by comma)
$fromemail = 
"youremail@yourcompany.com"
$server = 
"yourmailserver.yourcompany.com" #enter your own SMTP server DNS name / IP address here
$list = $args[0] 
#This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = 
get-content $list #grab the names of the servers/computers to check from the list.txt file.
# Set free disk space threshold below in percent (default at 10%)
$thresholdspace = 20
[int]$EventNum = 3
[int]$ProccessNumToFetch = 10
$ListOfAttachments = @()
$Report = @()
$CurrentTime = 
Get-Date
#endregion

Saturday, 6 July 2013

Script for get the servers NIC Ip configuration

Import-Module ActiveDirectory
$Computer = "Target"

$ComputerInfo = Get-ADDomainController -Identity $Computer
$DCIP = $ComputerInfo.IPv4Address

#########################################
# Get the Server's NIC IP Configuration #
#########################################
Write-Verbose "Enumerate all connected network interfaces and Set the filter to only find Ethernet 802.3 NICs, that are enabled and conected `r "
Write-Verbose "This SHOULD only return the correct NIC. If not, IP Address data will not display correctly. `r "
Write-Verbose "There should only be 1 active NIC on a DC. `r "
Write-Output "Gathering $computer NIC configuration data from all active NICs on $computer... `r "

Write-Verbose "Gather the configured NICs on the computer and identify the one with a configured IP & DefaultGateway `r "
$NICConfig = get-wmiobject -Class win32_networkadapterconfiguration -computername $Computer -property *
$PrimaryNIC = $NICConfig | ? {$_.IPAddress -and $_.IPAddress -notcontains "0.0.0.0" -and $_.DefaultIPGateway }
$NICCount = $PrimaryNIC.Count
IF ($NICCount -gt 1)
 { ## OPEN IF there are multiple Primary NICs discovered
 $PrimaryNIC = $NICConfig | ? {$_.IPAddress -contains "$DCIP" -and $_.DefaultIPGateway }
 } ## CLOSE IF there are multiple Primary NICs discovered

$NICIndex = $PrimaryNIC.Index
$ActiveNIC = get-wmiobject -Class Win32_NetworkAdapter -computername $Computer -filter "Index=$NICindex"
$NICDescName = $ActiveNIC.Name
$NICNAME = $ActiveNIC.NetConnectionID
$NICConnStatus = $ActiveNIC.NetConnectionStatus

Write-Output "Found the NIC $NICDescName labeled [$NICNAME] ..." `r

$PrimaryNICconfig = get-wmiobject Win32_NetworkAdapterConfiguration -computername $Computer -filter "Index=$NICindex"
ForEach ($NetConfig in $PrimaryNICconfig)
 { ## OPEN Bracket ForEACH Loop for NIC Info
 #Get NIC info
 $MACAddress = $NetConfig.MACAddress

 $IPAddressArray = $NetConfig.IPAddress -split "`t "
 $IPAddressIPV4 = $IPAddressArray[0]
 $IPAddressIPv6 = $IPAddressArray[1]

 $SubnetMaskArray = $NetConfig.IPSubnet -split "`t "
 $SubnetMaskIPv4 = $SubnetMaskArray[0]
 $SubnetMaskIPv6 = $SubnetMaskArray[1]

 $DefaultGateway = $NetConfig.DefaultIPGateway

 $DNSServerArray = $NetConfig.DNSServerSearchOrder -split "`t "
 $PrimaryDNSServer = $DNSServerArray[0]
 $SecondaryDNSServer = $DNSServerArray[1]
 $TertiaryDNSServer = $DNSServerArray[2]

 [string]$PrimaryWINSServer = $NetConfig.winsprimaryserver
 [string]$SecondaryWINSServer = $NetConfig.winsSecondaryServer

 $DNSSuffixSearchArray= $NetConfig.DNSDomainSuffixSearchOrder -split "`t "
 $DNSSuffix1 = $DNSSuffixSearchArray[0]
 $DNSSuffix2 = $DNSSuffixSearchArray[1]
 $DNSSuffix3 = $DNSSuffixSearchArray[2]
 $DNSSuffix4 = $DNSSuffixSearchArray[3]
 } ## CLOSE Bracket ForEACH Loop for NIC Info

#######################################
# Display Gathered Configuration Data #
#######################################
#Display gathered data
Write-Output "Found the following network information for $computer..." `r
Write-Output "Server Name : " $Computer `r
Write-Output "NIC : " $NICDescName `r
Write-Output "NIC Name : " $NICName `r
Write-Output "NIC Status : " $NICConnStatus `r
Write-Output "MAC Address : " $MACAddress `r
Write-Output "IPAddress : " $IPAddressIPv4 `r
Write-Output "SubnetMask : " $SubnetMaskIPv4 `r
Write-Output "Default Gateway : " $DefaultGateway `r
Write-Output "DNS Servers : " $DNSServerList `r
Write-Output "DNS Primary Server : " $PrimaryDNSServer `r
Write-Output "DNS Secondary Server : " $SecondaryDNSServer `r
Write-Output "DNS Tertiary Server : " $TertiaryDNSServer `r
Write-Output "WINS Primary Server : " $PrimaryWINSServer `r
Write-Output "WINS Secondary Server : " $SecondaryWINSServer `r
Write-Output "DNS Search Suffixes : " $DNSSearchSuffixList `r
Write-Output "DNS Search Suffix 1 : " $DNSSearchSuffix1 `r
Write-Output "DNS Search Suffix 2 : " $DNSSearchSuffix2 `r
Write-Output "DNS Search Suffix 3 : " $DNSSearchSuffix3 `r