forked from scriptrunner/ActionPacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-FilMPartition.ps1
More file actions
144 lines (123 loc) · 5.26 KB
/
Copy pathNew-FilMPartition.ps1
File metadata and controls
144 lines (123 loc) · 5.26 KB
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
137
138
139
140
141
142
143
144
#Requires -Version 4.0
<#
.SYNOPSIS
Creates a new partition on an existing Disk object
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, AppSphere AG assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of AppSphere AG.
© AppSphere AG
.COMPONENT
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/WinFileManagement/Disks
.Parameter Number
Specifies the disk number
.Parameter ComputerName
Specifies the name of the computer on which the partition creates. If Computername is not specified, the current computer is used.
.Parameter AccessAccount
Specifies a user account that has permission to perform this action. If Credential is not specified, the current user account is used.
.Parameter AssignDriveLetter
Assigns a drive letter to the new partition
.Parameter DriveLetter
Specifies the specific drive letter to assign to the new partition
.Parameter IsActive
Specifies that the object is marked active
.Parameter IsHidden
Specifies that is a hidden partition
.Parameter MbrType
Specifies the type of MBR partition to create
.Parameter UseMaximumSize
Creates the largest possible partition on the specified disk
.Parameter Size
Specifies the size of the partition to create, in bytes
.Parameter FormatNewPartition
Formats the new partition after create
.Parameter FileSystem
Specifies the file system with which to format the volume
.Parameter FormatFull
Performs a full format. A full format writes to every sector of the disk, takes much longer to perform than the default (quick) format
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[int]$Number,
[switch]$AssignDriveLetter,
[string]$DriveLetter,
[bool]$IsActive,
[bool]$IsHidden,
[ValidateSet("","FAT12", "FAT16", "Extended", "Huge", "IFS", "FAT32")]
[string]$MbrType="",
[switch]$UseMaximumSize,
[UInt64]$Size,
[bool]$FormatNewPartition,
[ValidateSet("NTFS","ReFS","exFAT","FAT32","FAT")]
[string]$FileSystem = "NTFS",
[switch]$FormatFull,
[string]$ComputerName,
[PSCredential]$AccessAccount
)
$Script:Cim=$null
[string[]]$Script:Properties = @("PartitionNumber","OperationalStatus","DriveLetter","DiskNumber","IsActive","IsHidden","Size","MbrType")
try{
if([System.String]::IsNullOrWhiteSpace($Properties)){
$Properties=@('*')
}
if([System.String]::IsNullOrWhiteSpace($ComputerName)){
$ComputerName=[System.Net.DNS]::GetHostByName('').HostName
}
if($null -eq $AccessAccount){
$Script:Cim =New-CimSession -ComputerName $ComputerName -ErrorAction Stop
}
else {
$Script:Cim =New-CimSession -ComputerName $ComputerName -Credential $AccessAccount -ErrorAction Stop
}
$Script:Disk = Get-Disk -CimSession $Script:Cim -Number $Number -ErrorAction Stop
if([System.String]::IsNullOrWhiteSpace($MbrType)){
if($Size -gt 0){
$Script:Parti = New-Partition -InputObject $Script:Disk -AssignDriveLetter:$AssignDriveLetter -Size $Size -ErrorAction Stop
}
else{
$Script:Parti = New-Partition -InputObject $Script:Disk -AssignDriveLetter:$AssignDriveLetter -UseMaximumSize:$UseMaximumSize -ErrorAction Stop
}
}
else{
if($Size -gt 0){
$Script:Parti = New-Partition -InputObject $Script:Disk -AssignDriveLetter:$AssignDriveLetter -Size $Size -MbrType $MbrType -ErrorAction Stop
}
else{
$Script:Parti = New-Partition -InputObject $Script:Disk -AssignDriveLetter:$AssignDriveLetter -UseMaximumSize:$UseMaximumSize -MbrType $MbrType -ErrorAction Stop
}
}
Start-Sleep -Seconds 5
if(-not [System.String]::IsNullOrWhiteSpace($DriveLetter)){
Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -NewDriveLetter $DriveLetter.toUpper() -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('IsActive') -eq $true){
Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -IsActive $IsActive -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('IsHidden') -eq $true){
Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -IsHidden $IsHidden -ErrorAction Stop
}
if($FormatNewPartition){
$Script:Parti.ObjectID
Format-Volume -Partition $Script:Parti -FileSystem $FileSystem -Force -Full:$FormatFull -ErrorAction Stop
}
$Script:Parti = Get-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber | Select-Object $Script:Properties
if($SRXEnv) {
$SRXEnv.ResultMessage =$Script:Parti
}
else{
Write-Output $Script:Parti
}
}
catch{
throw
}
finally{
if($null -ne $Script:Cim){
Remove-CimSession $Script:Cim
}
}