search
Categories
Archive
Blogroll

Badges

Microsoft PowerShell MVP
MCSE
Sponsors

VirtualMetric
Cozumpark Bilisim Portali
Posted in Windows Powershell | No Comment | 73 views | 15/11/2014 10:37

This is a text mining example for PowerShell. You can export your Whatsapp conversations
to find out which words you used most.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$Contents = Get-Content Whatsapp.txt -Encoding UTF8
$TagCloud = @{"Whatsapp" = "1"}
$ChatArray = New-Object System.Collections.ArrayList
foreach ($Content in $Contents)
{
	$Words = $Content.Split(" ")
	foreach ($Word in $Words)
	{
		$Word = $Word.ToLower();
 
		if ($ChatArray.Contains($Word) -eq $True)
		{
			$TagCloud.($Word) = [int]$TagCloud.($Word)+1;
		}
		else
		{
			$AddArray = $ChatArray.Add("$Word")
			$TagCloud.($Word) = 1;
		}
	}
}
$TagCloud.GetEnumerator() | Sort-Object -Property Value -Descending

Then you can convert it to tag cloud by using public tag cloud services.


Posted in Windows Powershell | No Comment | 161 views | 31/10/2014 17:39

If you create a secure password on PowerShell and output it into a txt file, you can decrypt it on another Windows machine. So we use a predefined user key to create our secure passwords:

There was a question about this post. So how you can create your own key?

So what you need to do is:

1
2
3
4
$SecureKey = New-Object byte[](16)
$RNGCryptoServiceProvider = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
$RNGCryptoServiceProvider.GetBytes($SecureKey)
$SecureKey

That will create a random key for you. So you can replace it with predefined key.


Posted in Windows Powershell, Windows Server | No Comment | 402 views | 23/09/2014 04:25

This is an example script to show how you can monitor Supermicro servers via PowerShell:

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
# Test Supermicro CLI Path
$TestSupermicroCLIPath = Test-Path -Path "C:\Program Files\Supermicro\SuperDoctor5\sdc.bat"
 
if ($TestSupermicroCLIPath -eq $True)
{				
	# Test Supermicro CLI Output
	$SensorInfo = Invoke-Command -ScriptBlock {
		&'C:\Program Files\Supermicro\SuperDoctor5\sdc.bat' "-d"
	}
}
 
# Create Supermicro Sensors Function
function Get-SupermicroSensor
{
	param ($SensorSource)
 
	if ($SensorSource)
	{
		# Get Supermicro Sensors
		$SupermicroSensors = $SensorSource | Select-String "2.1.1.1.1.2."
 
		# Parse Supermicro Output
		foreach ($SupermicroSensor in $SupermicroSensors)
		{
			# Clear Values
			$SupermicroSensorCurrentReading = $Null;
			$SupermicroSensorHighLimit = $Null;
			$SupermicroSensorLowLimit = $Null;
			$SupermicroSensorMetric = $Null;
 
			# Get Sensor Information
			$SupermicroSensorValues = $SupermicroSensor.ToString().Split(",")
			$SupermicroSensorID = ($SupermicroSensorValues[0]).Split(".")[-1]
			$SupermicroSensorName = $SupermicroSensorValues[-1]
 
			# Get Sensor Results
			$SupermicroSensorCurrentReading = $SensorSource | Select-String "2.1.1.1.1.4.$SupermicroSensorID\b"
			$SupermicroSensorHighLimit = $SensorSource | Select-String "2.1.1.1.1.5.$SupermicroSensorID\b"
			$SupermicroSensorLowLimit = $SensorSource | Select-String "2.1.1.1.1.6.$SupermicroSensorID\b"
			$SupermicroSensorMetric = $SensorSource | Select-String "2.1.1.1.1.11.$SupermicroSensorID\b"
 
			# Parameter Validation
			if (!$SupermicroSensorCurrentReading) { [string]$SupermicroSensorCurrentReading = "Unknown" } else { [string]$SupermicroSensorCurrentReading = $SupermicroSensorCurrentReading.ToString().Split(",")[-1] }
			if (!$SupermicroSensorHighLimit) { [string]$SupermicroSensorHighLimit = "Unknown" } else { [string]$SupermicroSensorHighLimit = $SupermicroSensorHighLimit.ToString().Split(",")[-1] }
			if (!$SupermicroSensorLowLimit) { [string]$SupermicroSensorLowLimit = "Unknown" } else { [string]$SupermicroSensorLowLimit = $SupermicroSensorLowLimit.ToString().Split(",")[-1] }
			if (!$SupermicroSensorMetric) { [string]$SupermicroSensorMetric = "Unknown" } else { [string]$SupermicroSensorMetric = $SupermicroSensorMetric.ToString().Split(",")[-1] }
 
			$Properties = New-Object Psobject
			$Properties | Add-Member Noteproperty ID $SupermicroSensorID
			$Properties | Add-Member Noteproperty Name $SupermicroSensorName
			$Properties | Add-Member Noteproperty CurrentReading $SupermicroSensorCurrentReading
			$Properties | Add-Member Noteproperty Metric $SupermicroSensorMetric
			$Properties | Add-Member Noteproperty HighLimit $SupermicroSensorHighLimit
			$Properties | Add-Member Noteproperty LowLimit $SupermicroSensorLowLimit
			Write-Output $Properties
		}
	}
}
 
# Get Supermicro Sensor Results
$SupermicroSensorResults = Get-SupermicroSensor -SensorSource $SensorInfo

You need to install Super Doctor 5 on your Supermicro server before using this script.


Posted in Windows Powershell, Windows Server | No Comment | 484 views | 05/08/2014 15:21

You can use following script to get passthrough disk reports from Hyper-V Clusters:

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
# Get Clusters
$Clusters = Get-Content Clusters.txt
 
foreach ($Cluster in $Clusters)
{
	# Get Cluster Nodes
	$ClusterNodes = Get-Cluster $Cluster | Get-ClusterNode
 
	foreach ($ClusterNode in $ClusterNodes)
	{
		Write-Host Working on $ClusterNode
 
		# Clear PT Values
		$PTDisks = $Null;
		$DiskNumber = $Null;
 
		# Get Passthrough Disks
		$PTDisks = Get-VM -ComputerName $ClusterNode | Get-VMHardDiskDrive | Where Path -like Disk*
 
		foreach ($PTDisk in $PTDisks)
		{
			# Get VM Name
			$VMName = $PTDisk.VMName
 
			Write-Host Working on $VMName
 
			# Get Passthrough Disk Info
			$PTInfo = $PTDisk.Path
			$DiskNumber = $PTDisk.DiskNumber
			$ControllerNumber = $PTDisk.ControllerNumber
			$ControllerLocation = $PTDisk.ControllerLocation
			$ControllerType = $PTDisk.ControllerType
			Write-Host Working on Disk $DiskNumber
 
			if (!$DiskNumber)
			{
				$DiskNumber = $PTInfo.Split(" ")[1]
			}
 
			# Get Disk Info
			$DiskInfo = Get-WmiObject Win32_DiskDrive -ComputerName $ClusterNode | Where Index -eq $DiskNumber
			$Model = $DiskInfo.Model
			$Caption = $DiskInfo.Caption
			$Signature = $DiskInfo.Signature
			$Size = [math]::round($DiskInfo.Size / 1GB,0)
 
			# Set Output
			$Value = $VMName + ";" + $ClusterNode + ";" + $ControllerType + ";" + $ControllerNumber + ";" + $ControllerLocation + ";" + $DiskNumber + ";" + $Model + ";" + $Signature + ";" + $Size
 
			# Informational Output
			Add-Content -Value $Value -Path Output.txt
		}
	}
}

You need to add your clusters into Clusters.txt file.


Posted in Windows Powershell, Windows Server | No Comment | 2,783 views | 05/08/2014 14:28

You can get operating system report of your remote servers via PowerShell:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
	try
	{
		$OS = Get-WmiObject -ComputerName $Server -Class Win32_OperatingSystem -EA Stop
		$OSName = $OS.Caption
		$SPV = $OS.CSDVersion
 
		$Value = $Server + ";" + $OSName + ";" + $SPV
		Write-Host $Value
	}
	catch
	{
		Write-Host $_
	}
}

That will give you OS Name and Service Pack version.


Posted in Windows Powershell, Windows Server | No Comment | 513 views | 31/07/2014 11:30

You can get host reports with following 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
38
39
40
41
42
43
$Servers = "Server01","Server02"
 
foreach ($Server in $Servers)
{
	# Get Local Disks
	$Disks = (Get-WmiObject -ComputerName $Server -Class Win32_LogicalDisk)
 
	# Clear Variables
	$TotalSize = 0;
	$TotalUsedSize = 0;
 
	foreach ($Disk in $Disks)
	{
		# Get Disk Space
		$Size = $Disk.Size
		$FreeSpace = $Disk.FreeSpace
 
		# Calculate Used Size
		[int64]$UsedSize = [int64]$Size - [int64]$FreeSpace
 
		# Calculate Total Size
		$TotalSize = $TotalSize + $Size;
		$TotalUsedSize = $TotalUsedSize + $UsedSize;
	}
 
	# Convert Total Size to MB
	[int64]$TotalUsedSize = [math]::round(($TotalUsedSize / 1MB), 0)
	[int64]$TotalSize = [math]::round(($TotalSize / 1MB), 0)
	[int64]$TotalFreeSpace = $TotalSize-$TotalUsedSize
 
	# Get OS and CPU Info
	$OSInformation = Get-WmiObject -ComputerName $Server -Class "Win32_OperatingSystem"
	$CPUInformation = (Get-WmiObject -ComputerName $Server -Class "Win32_Processor").Count
 
	# Calculate Memory
	$HostTotalMemory = ([math]::round(($OSInformation.TotalVisibleMemorySize / 1MB), 0)) * 1024
	$HostFreeMemory = ([math]::round(($OSInformation.FreePhysicalMemory / 1MB), 0)) * 1024
	$HostUsedMemory = $HostTotalMemory-$HostFreeMemory
 
	# Output Results
	$Value = $Server + ";" + $CPUInformation + ";" + $HostTotalMemory + ";" + $HostUsedMemory + ";" + $HostFreeMemory + ";" + $TotalSize + ";" + $TotalUsedSize + ";" + $TotalFreeSpace
	$Value
}

You can export all results to Excel to parse values.


Posted in Windows Powershell, Windows Server | 1 Comment | 943 views | 20/07/2014 23:31

You may get following issues due to WMI problem:

403042

Reboot is only way to fix it but at least you can prevent future issues:

1. Go to Start -> Run and type wbemtest.exe
2. Click Connect
3. In the namespace text box type “root” (without quotes).
4. Click Connect
5. Click Enum Instances
6. In the Class Info dialog box enter Superclass Name as “__ProviderHostQuotaConfiguration” (without quotes) and press OK.
(Note: the Superclass name includes a double underscore at the front.)
7. In the Query Result window, double-click “__ProviderHostQuotaConfiguration=@”
8. In the Object Editor window, double-click HandlesPerHost
9. In the Value dialog, type in 8192
10. Click Save Property
11. Click Save Object
12. Under properties find the property “MemoryPerHost” or any other ones you need to modify and double click it
13. Change the value from 512 MB which is 536870912 to 1GB which is 1073741824
14. Click Save Property
15. Click Save Object
16. Close Wbemtest
17. Restart the computer

Thanks to Shaon Shan for sharing this solution.