Skip to content

Instantly share code, notes, and snippets.

@mczerniawski
Last active February 18, 2019 16:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mczerniawski/efb648293cc5be1868767febb707571a to your computer and use it in GitHub Desktop.
Save mczerniawski/efb648293cc5be1868767febb707571a to your computer and use it in GitHub Desktop.
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.
.PARAMETER Credential
Credentials to connect to Cluster.
.EXAMPLE
Get-StorageJobReport -Cluster 'SomeCluster'
Will use current user credentials to connect to SomeCluster, check for StorageJobs, match with VirtualDisks and return custom objects.
.EXAMPLE
Get-StorageJobReport -Cluster 'SomeCluster' -Credential (Get-Credential)
Will use provided credentials to connect to SomeCluster and validate if provided VHDPath exists.
#>
[CmdletBinding()]
[OutputType([PSObject])]
Param(
[Parameter(Mandatory,HelpMessage='Provide Hyper-V Cluster Name',
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
$Cluster,
[Parameter(Mandatory=$false,HelpMessage='Provide Credentials to access cluster',
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[System.Management.Automation.Credential()][System.Management.Automation.PSCredential]
$Credential = [System.Management.Automation.PSCredential]::Empty
)
process{
#region invoke-Command connection properties
$invokeProps = @{
Cluster = $Cluster
}
if($PSBoundParameters.ContainsKey('Credential')) {
Write-Verbose "Credential {$($Credential.UserName)} will be used to connect to Cluster {$Cluster}"
$invokeProps.Credential = $Credential
}
else {
Write-Verbose "Processing Cluster {$Cluster} with default credentials of user {$($env:USERNAME)}"
}
#endregion
Invoke-Command -ComputerName $Cluster -Credential $Credential -ScriptBlock {
$StorageJobs = Get-StorageJob
if ($StorageJobs) {
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
JobState = $stJob.JobState
PercentComplete= $stJob.PercentComplete
Name = $stJob.Name
BytesProcessed = $stJob.BytesProcessed
BytesTotal = $stJob.BytesTotal
}
}
}
}
else {
Write-Verbose "No Storage Jobs Running on Cluster {$USING:Cluster}"
return $null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment