search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell, Windows Server | No Comment | 2,696 views | 07/01/2014 14:42

This is my format disk script for System Center Orchestrator. PoSHServer and PsExec is required for operation.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Get Parameters
$VMName = $PoSHQuery.VMName
$DiskDrive = $PoSHQuery.DiskDrive
$DiskIndex = $PoSHQuery.DiskIndex
 
if (!$VMName -or !$DiskIndex -or !$DiskDrive)
{
	$ResultMessage = "A required parameter is not exist. Please check parameters."
	$Result = "1"
}
else
{
	# Clear Values
	$MountPoint = $Null;
	$DriveLetter = $Null;
 
	if ($DiskDrive.Length -gt "3")
	{
		$MountPoint = $DiskDrive
	}
	else
	{
		$DriveLetter = $DiskDrive	
	}	
 
	if ($DriveLetter)
	{
		$Type = "1"
 
		# Parameter Control
		if ($DriveLetter.Length -gt "1")
		{
			$DriveLetter = $DriveLetter.Remove(1)
		}
 
		# Create Drive Letter Array
		$DriveLetterArray = New-Object System.Collections.ArrayList
		$DriveLetterArray.Clear();
		$Volumes = (Get-WmiObject Win32_Volume -ComputerName $VMName)
		foreach ($Volume in $Volumes)
		{
			$VolumeLetter = $Volume.DriveLetter
			$AddArray = $DriveLetterArray.Add("$VolumeLetter")
		}
 
		# Control Drive Letter
		$DriveLetterControl = $DriveLetter + ":"
		if ($DriveLetterArray.Contains($DriveLetterControl) -eq $True)
		{
			$ResultMessage = "That drive letter is already exist in the server. Please format disk drive manually."
			$Result = "1"
		}
		else
		{
			$Result = "0"
		}
	}
	elseif ($MountPoint)
	{
		$Type = "2"
 
		$TargetDrive = $MountPoint.Remove(1)
		$TargetPath = $MountPoint.Split(":")[1]
		$TargetSharePath = "\\" + $VMName + "\" + $TargetDrive + "$" + $TargetPath
 
		$TestTargetPath = Test-Path $TargetSharePath
		if ($TestTargetPath)
		{
			$ResultMessage = "That mount point is already exist. Please format disk drive manually."
			$Result = "1"
		}
		else
		{
			$CreateTargetFolder = New-Item $TargetSharePath -ItemType Directory
 
			$Result = "0"
		}
	}
	else
	{
		$ResultMessage = "A required parameter is not exist. Please check parameters."
		$Result = "1"
	}	
}
 
if ($Result -eq "0")
{
	$Snapshot2 = (Get-WmiObject Win32_DiskDrive -ComputerName $VMName).Index
	$DiskIndex = $DiskIndex.TrimStart("index;")
	$Snapshot1 = $DiskIndex.Split(";")
	$DiffDiskIndex = (Diff $Snapshot1 $Snapshot2).InputObject
	if ($DiffDiskIndex)
	{
		$Value1 = "select disk $DiffDiskIndex"
		$Value2 = "online disk noerr"
		$Value3 = "attributes disk clear readonly"
		$Value4 = "create partition primary"
		$Value5 = "format fs=ntfs quick"
 
		if ($Type -eq "1")
		{
			$Value6 = "assign letter=$DriveLetter"
		}
 
		if ($Type -eq "2")
		{
			$Value6 = "assign mount=$MountPoint"
		}
 
		$Path = "\\" + $VMName + "\C$\diskpart.txt"
		Add-Content -Value $Value1 -Path $Path
		Add-Content -Value $Value2 -Path $Path
		Add-Content -Value $Value3 -Path $Path
		Add-Content -Value $Value4 -Path $Path
		Add-Content -Value $Value5 -Path $Path
		Add-Content -Value $Value6 -Path $Path
 
		$PsExecPath = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PoSHServer\http\PsExec.exe"
		$Command1 = "diskpart /s C:\diskpart.txt"
		$Shell1 = $PSExecPath + " -accepteula \\" + $VMName + " " + $Command1
		$FormatDisk = c md.exe /c $Shell1
		Remove-Item -Path $Path
 
		$ResultMessage = "Disk is successfully formatted."
		$Result = "0"
	}
	else
	{
		$ResultMessage = "No new virtual disk detected. Please check previous steps in Orchestrator."
		$Result = "1"
	}
}
 
@"
	$($ResultMessage)
"@

This is just an example to show you how to automate disk operations via Orchestrator.