I have a function "Get-UserFolders" that I imported into the runspace using:
$Definition = Get-Content function:\Get-UsersFolders -ErrorAction Stop
$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'Get-UsersFolders', $Definition
$InitialSessionState.Commands.add($SessionStateFunction)
Here is the function:
function Get-UsersFolders($ComputerName) {
#If there is any checkboxes present, delete them
$Indexes = @()
Foreach ($control in $hash.sourceTab.Controls) {
if ($control.GetType().ToString() -eq "System.Windows.Forms.CheckBox") {
$Indexes += $control.TabIndex
}
}
Foreach ($index in $indexes | Sort-Object -Descending) {
$hash.sourceTab.Controls.RemoveAt($index)
}
if ($null -ne $ComputerName) {
$global:UserFolders = Invoke-Command -ComputerName $ComputerName -Credential $script:hash.credentials -ScriptBlock {
$array = @()
$users = Get-ChildItem -path "C:\users" | Where-Object { $_.Name -match "^\d{6}$" }
Foreach ($folder in $users) {
$array += [PSCUSTOMOBJECT]@{
Name = "$($folder.Name)"
Size = "$( [Math]::Round(((Get-ChildItem $folder.fullname -Recurse -ErrorAction SilentlyContinue | Measure-Object -Sum Length -ErrorAction SilentlyContinue).Sum / 1GB),2) ) GB"
}
}
Return $array
}
}
else {
$global:UserFolders = @()
$users = Get-ChildItem -path "C:\users" | Where-Object { $_.Name -match "^\d{6}$" }
Foreach ($folder in $users) {
$global:UserFolders += [PSCUSTOMOBJECT]@{
Name = "$($folder.Name)"
Size = "$( [Math]::Round(((Get-ChildItem $folder.fullname -Recurse -ErrorAction SilentlyContinue | Measure-Object -Sum Length -ErrorAction SilentlyContinue).Sum / 1GB),2) ) GB"
}
}
}
$xPos = 0
$yPos = 0
$numUsers = 0
Foreach ($user in $global:UserFolders | Sort-Object -Property Size -Descending) {
$hash.userCB = New-Object System.Windows.Forms.CheckBox
$hash.userCB.Name = $user.Name
$CBLx = 10 + $xPos
$CBLy = 10 + $yPos
$hash.userCB.Location = New-Object System.Drawing.Point($CBLx, $CBLy)
$hash.userCB.AutoSize = $true
$hash.userCB.Text = "$($user.Name) - ($($user.Size))"
$hash.userCB.Tag = "$($user.Size.split(" ")[0])"
$hash.userCB.Add_CheckStateChanged({
Get-PrecheckTotal
if ($null -ne $global:driveInfo) {
$hash.sizeAfterLabel.text = "Space After: $([Math]::Round($global:driveInfo.Free / 1GB,2) - $global:precheckTotal) GB"
}
})
$numUsers++
$yPos = $yPos + 30
if (($numUsers % 9 -eq 0) -and ($numUsers -ne 0)) {
$xPos = $xPos + 150
$yPos = 0
}
$hash.sourceTab.Controls.Add($hash.userCB)
Write-host "Added Checkbox for $($hash.userCB.Name)"
}
Get-PrecheckTotal
}
When the function runs, it outputs to the console that the checkboxes where added to the form. I have the command:
foreach ($control in $hash.sourceTab.Controls) {
Write-host $control
}
Which lists the controls in the form. The checkboxes are not part of this list or displayed on the form. I cannot figure out why.
Potentially helpful background: stackoverflow.com/q/6184/45375. Potential PowerShell solution (I'd personally avoid runspaces): stackoverflow.com/a/60229906/45375
Thank you mklement0