Reputation: 1
Goal of this script?
Problem
After script is completed the TypeName for variable $output should be "System.IO.DirectoryInfo" so that another variable called $child1 should use $output and lets us to go inside $output using Get-Childiitem
However, after script is completed, somehow the TypeName for $output is getting changed to System.Management.Automation.PSCustomObject from "System.IO.DirectoryInfo" which shouldn't happen. Consequently variable called $child1 can not go inside the variable $output and it's giving errors. This issue needs to be resolved.
Errors
Get-ChildItem : Cannot find path 'C:\Users\Ujjwal Mandal\@{Name=OFFICE; Size=6.16 GB}' because it does not exist.
At line:21 char:26
+ $child1 = $res | Get-ChildItem | Out-GridView -Title "Select ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Ujjwal...; Size=6.16 GB}:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Expectation
After I click on OK button as shown in the following screenshot it should let me go inside the child directory. But it is giving error as described above. After clicking on OK button there will another subfolder. Similarly we need click on OK which should take us to inside this subfolder and finally we will select all files and hit ok which will delete all dump files. enter image description here
Code for this script:
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$\All Documents"
$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)
}
}
$res= $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 = $res.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
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()
I have looked into several articles on internet but did find one which tells me how to stop PowerShell to change TypeName to "System.Management.Automation.PSCustomObject" from System.IO.DirectoryInfo.
** Expectation:** At the beginning of the script TypeName is "System.IO.DirectoryInfo" which should remain the same after script is completed. However the end variable called $output has values of TypeName "System.Management.Automation.PSCustomObject" which is the issue and need help to resolve.
Upvotes: 0
Views: 19