Skip to content

Instantly share code, notes, and snippets.

@mczerniawski
Created December 11, 2017 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mczerniawski/c5d2b8fbe1052f8492855916e6bf132c to your computer and use it in GitHub Desktop.
Save mczerniawski/c5d2b8fbe1052f8492855916e6bf132c to your computer and use it in GitHub Desktop.
function Format-RemoteDrive {
<#
.SYNOPSIS
Formats all RAW drives on a remote system.
.DESCRIPTION
Using Invoke-Command will connect to remote system (ComputerName) and will format all RAW drives with given properties
.PARAMETER ComputerName
Name of a remote system to connect to using Invoke-Command.
.PARAMETER NewFileSystemLabel
Drive label (i.e. "MyData") for all drives to be formatted.
.PARAMETER FileSystem
FileSystem to be choosen for drives (ReFS and NTFS available)
.PARAMETER Credential
Alternate credentials to use to connect to ComputerName
.EXAMPLE
Format-RemoteDrive -ComputerName SomeServer -NewFileSystemLabel DATA -FileSystem ReFS -Credential (Get-Credential)
Will connect to SomeServer using given credentials and will format all RAW drives to ReFS with DATA label
#>
[CmdletBinding()]
[OutputType([void])]
Param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string]
$ComputerName,
[Parameter(Mandatory=$false,
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string]
$NewFileSystemLabel,
[Parameter(Mandatory=$false,
ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[ValidateSet('NTFS','ReFS')]
[string]
$FileSystem,
[Parameter(Mandatory=$false,
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
Process{
Write-Verbose -Message "Processing computer {$ComputerName}"
$ConnectProps = @{
ComputerName = $ComputerName
}
if($PSBoundParameters.ContainsKey('Credential')) {
Write-Verbose -Message "Credential {$($Credential.UserName)} will be used to connect to computer {$ComputerName}"
$ConnectProps.Credential = $Credential
}
Write-Verbose -Message "Creating PSSession to computer {$ComputerName}"
$pssession = New-PSSession @ConnectProps
Write-Verbose -Message "Checking if there are any not initialized disks on {$ComputerName}"
$rawDisks = Invoke-Command -Session $pssession -ScriptBlock {
get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'}
}
if($rawDisks) {
if($rawDisks.count){
Write-Verbose -Message "Found #{$($rawDisks.Count)} raw disks on {$ComputerName}"
}
else {
Write-Verbose -Message "Found #{1} raw disk on {$ComputerName}"
}
Write-Verbose -Message "Processing disks on {$ComputerName}"
Invoke-Command -Session $pssession -ScriptBlock {
$driveLetter = (get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize ).DriveLetter
$result = Format-Volume -NewFileSystemLabel $USING:NewFileSystemLabel -FileSystem $USING:FileSystem -DriveLetter $driveLetter -Force -confirm:$false
$result
}
}
Write-Verbose -Message "Removing PSSession to Computer {$($pssession.ComputerName)}"
$pssession | Remove-PSSession
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment