Remove-xRDS_UserProfileDisk.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
99
function Remove-xRDS_UserProfileDisk {

<#
    .DESCRIPTION
    Removing orphan User Profile Disks from RDS Session Hosts.
  
    .PARAMETER Broker
    -ConnectionBroker - FQDN of RDS ConnectionBroker.
 
    .PARAMETER Broker
    -Computer - FQDN of RDS computer. (No required if you use connection brokers FQDN).
 
    .PARAMETER Credential
    -Credential [Optional] - Query RDS Connection Broker resources under provided credentials, the same credentials will be used to access RDS session hosts.
 
    .PARAMETER UI
    -Logfile [Optional] - Output deleted User Profile Disks objects into logfile. (Ignored if used with WhatIf option).
 
    .PARAMETER UI
    -UI [Optional] - Displays records in GridView for output selection.
 
    .PARAMETER WhatIf
    -WhatIf [Optional] - Displays orphan User Profile Disks records without deletion.
 
    .PARAMETER WINRMPort
    -WINRMPort [Optional] - WINRM Port to test for host connectivity validation. Default port is 5985.
 
    .EXAMPLE
    # Starts deletion of orphan User Profile Disks from RDS Session Hosts:
    Remove-xRDS_UserProfileDisk -ConnectionBroker ardscbl01.adatum.labnet
#>



    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)][string]$ConnectionBroker,
        [String]$Computer = "localhost",
        [String]$Logfile,
        [switch]$UI,
        [switch]$WhatIf,
        [Int]$WinRMPort = 5985,    
        [PSCredential]$Credential
    )      
   
    $ObjectsList=@()

    if($ConnectionBroker) {$Collection = Get-xRDS_CollectionsList -ConnectionBroker $ConnectionBroker -Credential $Credential} 
    else {$Collection = @{ $Computer = "Computer"}}

    Try {  

    #List session host maintenance mode

             foreach ($key in $Collection.Keys) 
                {

                $TempObject=@()
                $connection = $null;

                #Test host WInRM access
                $connection =  Invoke-xRDS_TestPort -hostname $key -port $WinRMPort
                
                if ($connection.open) {
                write-host "Removing user disk profiles from $key" -ForegroundColor Cyan
                
                    if($Credential) {
                    $ObjectsList += Invoke-Command -ComputerName $key -ArgumentList $WhatIf -Credential $Credential -ScriptBlock { `
                    $WhatIf = $args[0];
                    $excludefolders = @('Public','Default','UvhdCleanupBin') ;
                    $excludefolders +=  (Get-CimInstance -ClassName win32_userprofile | select LocalPath).LocalPath -replace '.*\\' ; 
                    Get-ChildItem C:\Users\ -Exclude $excludefolders | select FullName | foreach ($_) {if(!($WhatIf)){remove-item $_.fullname -Force -Recurse} ; `
                    (new-object -Type PSObject -Property @{Time = Get-Date -Format "yyyy-MM-dd_MM-mm" ; Profile = $_.fullname})}}
                    }
                    else {
                    $ObjectsList += Invoke-Command -ComputerName $key -ArgumentList $WhatIf -ScriptBlock { `
                    $WhatIf = $args[0];
                    $excludefolders = @('Public','Default','UvhdCleanupBin') ;
                    $excludefolders +=  (Get-CimInstance -ClassName win32_userprofile | select LocalPath).LocalPath -replace '.*\\' ; 
                    Get-ChildItem C:\Users\ -Exclude $excludefolders | select FullName | foreach ($_) {if(!($WhatIf)){remove-item $_.fullname -Force -Recurse} ; `
                    (new-object -Type PSObject -Property @{Time = Get-Date -Format "yyyy-MM-dd_MM-mm" ; Profile = $_.fullname})}}
                    }

                }   

                else {write-host "Cannot access WinRM port for $key" -ForegroundColor Red}                   
                } 

             #Output
             if($WhatIf) {write-host "User Profile Disks records have been listed without deletion." -ForegroundColor Magenta} 
             else {write-host "User Profile Disks records have been deleted." -ForegroundColor Yellow}

             if($logfile -and (!($WhatIf))){Write-host "Writing outout into log file $logfile";$ObjectsList |  Select-Object PSComputerName,Time,Profile | Export-Csv  $logfile -Append -NoTypeInformation }

             If($UI) {$ObjectsList | Out-GridView -PassThru -Title "RDS User Profile Disk List"}
             ELSE {$ObjectsList | ft } 

    } Catch {Write-host $_.Exception.message }   

}