Skip to content

Instantly share code, notes, and snippets.

View mczerniawski's full-sized avatar

mczerniawski mczerniawski

View GitHub Profile
$userOUs = @('OU=Leavers,OU=Site1,OU=Contoso Users,DC=Contoso,DC=com','OU=Leavers,OU=Site2,OU=Contoso Users,DC=Contoso,DC=com')
$disabledUsers = foreach ($ou in $userOUs) {
get-aduser -filter {Enabled -eq $false} -SearchBase $ou
}
$disabledUsers | Export-Csv -Path C:\AdminTools\disabled_users.csv -NoTypeInformation
function Get-EmptyFolder {
<#
.SYNOPSIS
Returns if there are empty folders in given path.
.DESCRIPTION
Will check for empty folders in given path. If ComputerName is provided will connect to remote computer. Credential parameter works in combination with ComputerName only
.PARAMETER Path
Path to check for empty folders
function Set-ClusterVMAntiAffinity {
<#
.SYNOPSIS
Will configure Preferred Owners for Virtual Machine running on Failover Cluster
.DESCRIPTION
Uses Invoke-Command to allow for PSCredential
If Preferred Owner is provided will verify if it matches cluster Owner Nodes.
If yes - will set Preferred Owners for given VMs.
If no - will abort.
foreach ($VM in $VMName) {
Write-Verbose -Message "Processing VM {$VM}"
$ClusterVM = Invoke-Command -Session $ClusterSession -ScriptBlock {
Get-ClusterGroup -Name $USING:VM -ErrorAction SilentlyContinue | Select-Object *
}
if (-not $ClusterVM) {
Write-Error -Message "VM {$VM} not found on cluster {$Cluster}"
}
else {
$currentPreferredOwners = Invoke-Command -Session $ClusterSession -ScriptBlock {
#region Validate Preferred Owner
$nodesInCluster = Invoke-Command -Session $ClusterSession -ScriptBlock {
Get-ClusterNode | Select-Object -ExpandProperty Name
}
#null or empty string - reset to defaults
if (-not $PreferredOwner) {
Write-Verbose -Message "Provided no Preferred Owner. Will reset to default setting"
$PreferredOwner = ''
}
#check if given hosts given in $PreferredOwner are members of $Cluster
[Parameter(Mandatory, HelpMessage = 'Provide Cluster Name',
ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String]
$Cluster,
[Parameter(Mandatory = $false, HelpMessage = 'Provide Credentials for Cluster',
ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.Management.Automation.PSCredential]
$Credential,
function Set-ClusterVMPossibleOwner {
<#
.SYNOPSIS
Will configure Possible Owners for Virtual Machine running on Failover Cluster
.DESCRIPTION
Uses Invoke-Command to allow for PSCredential.
If PossibleOwner is provided first will verify if it matches cluster Owner Nodes.
If yes - will set PossibleOwner for given VMs.
If no - will abort.
function Set-ClusterVMPreferredOwner {
<#
.SYNOPSIS
Will configure Preferred Owners for Virtual Machine running on Failover Cluster
.DESCRIPTION
Uses Invoke-Command to allow for PSCredential
If Preferred Owner is provided will verify if it matches cluster Owner Nodes.
If yes - will set Preferred Owners for given VMs.
If no - will abort.
switch (Compare-Object -ReferenceObject @($nodesInCluster) -DifferenceObject @($PreferredOwner) -IncludeEqual ) {
{$PSItem.SideIndicator -eq '=>'} {
Write-Error -Message "Given preferred owner {$($PSItem.InputObject)} not found in nodes of cluster {$Cluster}. Aborting" -ErrorAction Stop
break
}
{$PSItem.SideIndicator -eq '=='} {
Write-Verbose -Message "Given preferred owner {$($PSItem.InputObject)} was found in nodes of cluster {$Cluster}"
}
}
$HyperVHosts = @('Cluster1','Node1','Node2')
$VMs = Get-VM -ComputerName $HyperVHosts | ForEach-Object {
Write-Host "Procesing VM {$($PSItem.VMName)}"
Write-Host "Getting VM {$($PSItem.VMName)} disk information"
$disks = Get-VHD -VMId $PSItem.VMId -ComputerName $PSItem.ComputerName
$diskCount = $disks | Measure | Select-Object -ExpandProperty Count
$diskCurrentSize = $disks | Measure-Object -Sum -Property FileSize | Select-Object -ExpandProperty Sum
$diskMaximumSize = $disks | Measure-Object -Sum -Property Size | Select-Object -ExpandProperty Sum
[pscustomobject]@{