Skip to content

Instantly share code, notes, and snippets.

View mczerniawski's full-sized avatar

mczerniawski mczerniawski

View GitHub Profile
$StorageJobIDs = Get-StorageJob | Select-Object ObjectID | Select-String -Pattern '([A-Za-z0-9]{8}\-?){1}([A-Za-z0-9]{4}\-?){3}([A-Za-z0-9]{12})' -AllMatches |
Select-Object -ExpandProperty Matches |
Select-Object -ExpandProperty Value -Last 1
foreach ($objectId in $StorageJobIDs) {
Get-VirtualDisk | Where-Object {$_.ObjectId -match $objectID}
}
$StorageJobs = Get-StorageJob
foreach ($stJob in $StorageJobs) {
foreach ($jobObjectID in ($stJob | Select-Object -ExpandProperty ObjectId)) {
$objectID = $jobObjectID | Select-String -Pattern '([A-Za-z0-9]{8}\-?){1}([A-Za-z0-9]{4}){1}' -AllMatches |
Select-Object -ExpandProperty Matches |
Select-Object -ExpandProperty Value -Last 1
$vdisk = Get-VirtualDisk | Where-Object {$_.ObjectId -match $objectID}
[PSCustomObject]@{
VirtualDiskName = $vdisk.FriendlyName
HealthStatus = $vdisk.HealthStatus
function Get-StorageJobReport {
<#
.SYNOPSIS
Get current storage jobs on cluster. Returns additional information from Get-VirtualDisk.
.DESCRIPTION
If there are storageJobs running on the cluster will match each job to virtual disk and return custom object.
.PARAMETER Cluster
Hyper-V cluster name.
function Get-LAPSCredential {
<#
.SYNOPSIS
Retrieves LAPS password from AD and creates Credential Object
.DESCRIPTION
Using Local Administrator Password Solution cmdlet Get-AdmPwdPassword will query AD for ms-Mcs-AdmPwd attribute. Will use retrieved password to create Credential Object
.PARAMETER ComputerName
ComputerName to search for LAPS password
$ComputerName = 'SomeComputer'
$LAPSPassword = (Get-AdmPwdPassword -ComputerName $ComputerName -ErrorAction SilentlyContinue).Password
If ($LAPSPassword) {
$LocalAdminPassword = ConvertTo-SecureString -String $LAPSPassword -AsPlainText -Force
$LocalAdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "$ComputerName\Administrator", $LocalAdminPassword
$LocalAdminCredential
}
Else {
Write-error "No LAPS password for computer {$ComputerName}"
$ComputerName = 'SomeComputer'
Invoke-Command -ComputerName $ComputerName -Credential (Get-LAPSCredential -ComputerName $ComputerName) -ScriptBlock {
Get-Service -Name BITS | Stop-Service
}
Get-ADComputer -filter * -properties ms-Mcs-AdmPwd |
Where-Object {$null -ne $_."ms-Mcs-AdmPwd"} |
Select-Object samaccountname,ms-Mcs-AdmPwd
$driveLetter = (get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize ).DriveLetter
Format-Volume -NewFileSystemLabel 'DATA' -FileSystem 'ReFS' -DriveLetter $driveLetter -Force -confirm:$false
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.
$VMName = 'SomeVM'
#Get VM disk path. Assuming there's only one VHD here
$VHDPath = Get-VMHardDiskDrive $VMName | Select-Object -ExpandProperty Path
#Verify VHD information, including current size
Get-VHD -Path $VHDPath
#Expand the disk
$newSize = 120GB
Resize-VHD -Path $VHDPath -SizeBytes $newSize