Ujjawal Mandal
Ujjawal Mandal

Reputation: 1

Get-childitem: Can not get the subfolders from another subfolders to out-gridview: Error Cannot find path because it does not exist

What is the goal of this script?

  1. Need to delete big SQL dump files remotely.
  2. When script is launched, we need to click on ok while parent folder is selected/highlighted.
  3. After then we need click on ok so that we can go to its subfolder.
  4. Finally we need to select the files and hit OK. It will delete the selected files.
  5. For this, a GUI PS script needs to be created.
  6. Files to be deleted are under subfolder of another subfolder.

Error while executing this script:

Get-ChildItem : Cannot find path 'C:\Users\Ujjwal Mandal\@{name=All Documents; Size=6.18 GB}' because it does not exist.
At D:\project\test.ps1:30 char:20
+ $child1= $output | Get-ChildItem | Out-GridView -Title "Select server ...

My PowerShell code for this

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$TempFile = [io.path]::GetTempFileName()
$form = New-Object System.Windows.Forms.Form
$form.Text = "Fix low space issue"
$form.size =  New-Object System.Drawing.Size(900,400)
$remdmp = New-Object System.Windows.Forms.Button
$remdmp.Location = New-Object System.Drawing.Size(300,35)
$remdmp.Size = New-Object System.Drawing.Size(300,45)
$remdmp.Text = "Remove dump Files"
$remdmp.Font = [System.Drawing.Font]::new('Microsoft Sans Serif', 10, [System.Drawing.FontStyle]::Bold)
$remdmp.Add_Click({
Remove-Item $TempFile -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$svrs ="TM-ND0276"
$path = "\\TM-ND0276\d$\"
$size = 0
$decimalPlaces = 2
$output = @()
$data=Get-ChildItem -Directory $path | ForEach-Object {
 $size += (Get-ChildItem -Recurse $_.FullName | Measure-Object -Property Length -Sum).Sum
 $output += New-Object PSObject -Property @{
 name = $_.Name
 Size = "{0:N$decimalPlaces} GB" -f ((Get-ChildItem -Recurse $_.FullName | Measure-Object -Property Length -Sum).Sum / 1GB)
 }
}

$output | Sort-Object -Descending -Property Size | Out-GridView -Title "select" -PassThru 
$child1= $output | Get-ChildItem | Out-GridView -Title "Select server name folder you want to delete the dump files from" -PassThru
$child2= $child1 | Get-ChildItem | Out-GridView -Title "Select files you want to delete" -PassThru
if($child2) {
$child2 | Remove-Item -Recurse -Force
$msgbox=[System.Windows.MessageBox]::Show('Dump files have been deleted. Check notepad for available free space in D drive')
$dskspce=Invoke-Command -ComputerName $svrs -ScriptBlock {Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}} }

$dskspce | Format-Table > $TempFile | Notepad $TempFile
}
else{
$msgbox=[System.Windows.MessageBox]::Show('Action has been cancelled. Script will stop here!')
}
})
$Form.Controls.Add($remdmp)

$QUIT = New-Object 'System.Windows.Forms.Button'
$QUIT.DialogResult = 'OK'
$QUIT.Location = '300, 200'
$QUIT.Margin = '5, 5, 5, 5'
$QUIT.Name = 'buttonOK'
$QUIT.Size = '300, 50'
$QUIT.BackColor ="LightGray"
$QUIT.ForeColor ="black"
$QUIT.Text = '&CLOSE'
$QUIT.Font = [System.Drawing.Font]::new('Microsoft Sans Serif', 10, [System.Drawing.FontStyle]::Bold)
$QUIT.UseCompatibleTextRendering = $True
$QUIT.UseVisualStyleBackColor = $False
$QUIT.Add_Click({$form.Add_FormClosing({$_.Cancel=$false});$form.Close()})    
$QUIT.Show()#$QUIT.Hide()
$form.Controls.Add($QUIT)
$form.showdialog()

It looks like object TypeName of output should be System.IO.DirectoryInfo however, it is System.Management.Automation.PSCustomObject. Is that why I am getting this error?

I have tried all the articles I found on the internet also gone through a PowerShell book but didn't find much information regarding this error and resolution.

When I click on OK button (after selecting any subfolder folder) it should take me to another subfolder but unfortunately it is not.

Screenshot of this gui

enter image description here

Error when I click on ok after select a folder

enter image description here

Upvotes: 0

Views: 60

Answers (1)

sirtao
sirtao

Reputation: 2880

You are passing the whole content of $Output, which is a object with multiple properties.

You need to pass Get-ChildItem only the Name property.

$size = 0
$decimalPlaces = 2
# You are not using $Data so just pass the output of the loop to $Output.   
$Output = Get-ChildItem -Directory $path | ForEach-Object {
    $size += (Get-ChildItem -Recurse $_.FullName |
            Measure-Object -Property Length -Sum).Sum
        # Bit more efficient alternative.   
        [pscustomobject]@{
            Name = $_.Name
            Size = "{0:N$decimalPlaces} GB" -f ((Get-ChildItem -Recurse $_.FullName | 
                        Measure-Object -Property Length -Sum).Sum / 1GB)
            }
        }

        $output | Sort-Object -Descending -Property Size | Out-GridView -Title "select" -PassThru 

        # Using powershell automatic enumeration using .Name: pass only the name of the
        # item instead of the whole object.   
        # You might want to use FullName, perhaps?.   
        $child1 = $output.Name | Get-ChildItem | 
            Out-GridView -Title "Select server name folder you want to delete the dump files from" -PassThru

# $Child1 contains [FileSystemInfo] objects that get automagically casted to
# path strings, which `Get-ChildItem` know how to manage.   
$child2 = $child1 | Get-ChildItem |
    Out-GridView -Title "Select files you want to delete" -PassThru

Upvotes: 0

Related Questions