-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathNew-FilMPartition.ps1
More file actions
141 lines (117 loc) · 4.88 KB
/
New-FilMPartition.ps1
File metadata and controls
141 lines (117 loc) · 4.88 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
#Requires -Version 5.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, ScriptRunner Software GmbH 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 ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/WinFileManagement/Disks
.Parameter Number
[sr-en] Disk number
.Parameter ComputerName
[sr-en] Name of the computer on which the partition creates. If Computername is not specified, the current computer is used.
.Parameter AccessAccount
[sr-en] User account that has permission to perform this action. If Credential is not specified, the current user account is used.
.Parameter AssignDriveLetter
[sr-en] Assigns a drive letter to the new partition
.Parameter DriveLetter
[sr-en] Drive letter to assign to the new partition
.Parameter IsActive
[sr-en] The object is marked active
.Parameter IsHidden
[sr-en] Is a hidden partition
.Parameter MbrType
[sr-en] Type of MBR partition to create
.Parameter UseMaximumSize
[sr-en] Creates the largest possible partition on the specified disk
.Parameter Size
[sr-en] Size of the partition to create, in bytes
.Parameter FormatNewPartition
[sr-en] Formats the new partition after create
.Parameter FileSystem
[sr-en] File system with which to format the volume
.Parameter FormatFull
[sr-en] 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($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
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'InputObject' = $Script:Disk
'AssignDriveLetter' = $AssignDriveLetter}
if($Size -gt 0){
$cmdArgs.Add('Size',$Size)
}
else {
$cmdArgs.Add('UseMaximumSize',$null)
}
if([System.String]::IsNullOrWhiteSpace($MbrType) -eq $false){
$cmdArgs.Add('MbrType', $MbrType)
}
$Script:Parti = New-Partition @cmdArgs
Start-Sleep -Seconds 5
if(-not [System.String]::IsNullOrWhiteSpace($DriveLetter)){
$null = Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -NewDriveLetter $DriveLetter.toUpper() -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('IsActive') -eq $true){
$null = Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -IsActive $IsActive -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('IsHidden') -eq $true){
$null = Set-Partition -CimSession $Script:Cim -DiskNumber $Number -PartitionNumber $Script:Parti.PartitionNumber -IsHidden $IsHidden -ErrorAction Stop
}
if($FormatNewPartition){
# $Script:Parti.ObjectID
$null = 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
}
}