Public/FileSystem/Format-RemoteDrive.ps1

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
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()]
  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
  [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
  )

  Begin{
  }

  Process{

    Write-Log -Info -Message "Processing computer {$ComputerName}"
    $ConnectProps = @{
      ComputerName = $ComputerName
    }
    if($PSBoundParameters.ContainsKey('Credential')) {
      Write-Log -Info -Message "Credential {$($Credential.UserName)} will be used to connect to computer {$ComputerName}"
      $ConnectProps.Credential = $Credential
    }

    Write-Log -Info -Message "Creating PSSession to computer {$ComputerName}"
    $pssession = New-PSSession @ConnectProps

    Write-Log -Info -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-Log -Info -Message "Found #{$($rawDisks.Count)} raw disks on {$ComputerName}"
      }
      else {
        Write-Log -Info -Message "Found #{1} raw disk on {$ComputerName}"
      }
      Write-Log -Info -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-Log -Info -Message "Removing PSSession to Computer {$($pssession.ComputerName)}"
    $pssession | Remove-PSSession
  }


  End {
  }
}