Stuck and not working

Welcome Forums General PowerShell Q&A Stuck and not working

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
2 months, 4 weeks ago.

  • Author
    Posts
  • #159593

    Participant
    Topics: 4
    Replies: 1
    Points: 21
    Rank: Member

    I can not get the Hyper-V / vMWare drive type part to work and gave my self a migrain looking at it, PLEASE HELP!.

    ##==============================================================================
    ## START
    ##==============================================================================
    #>
    clear
    #---------- Configurations - Start.
    # $cre = get-Credential # This user account should be Adminstrator.
    $Servers = get-Content "list.txt" # Replace this with your TXT file containg Server names.
    # Do not change below this line.
    $OS = (gwmi win32_operatingsystem).caption
    $MediaType = gwmi -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage
    $HVDrive = Get-VMHardDiskDrive | Where-Object { $_.DriveType -eq 1 }
    $LogicalDisks = gwmi Win32_LogicalDisk | Where-Object { $_.DriveType -eq 2 }
    $LocalHDisks = $LogicalDisks | Where-Object { $_.DriveType -eq 3 }
    $RemoteDrives = gwmi Win32_LogicalDisk | Where-Object { $_.DriveType -eq 4 }
    $OpticalDrives = gwmi Win32_CDRomDrive | Where-Object { $_.DriveType -eq 5 }
    $HVDrive = Get-VM | Select-Object VMID | Get-VHD | Select-Object VhdFormat
    # Do not change above this line.
    $Records = 1000 # Sets Max Records
    $CSV = "DiskSpaceReport_$(((get-date).ToUniversalTime()).ToString("yyyy-MM-dd")).csv" # File to export to.
    #---------- Configurations - End.
    Clear-Host
    #---------- The report - Start.
    Write-Host " Your Report started at: $(((get-date).ToUniversalTime()).ToString("yyyy-MM-dd"))."
    #---------- Wright to File - Start.
    #Get-WmiObject Win32_LogicalDisk -filter 'DriveType=3' -computerName $servers | ## use this if you want to show only Drive Type 3 drives.
    Get-WmiObject Win32_LogicalDisk -computerName $servers |
    Select-Object `
    SystemName, DeviceID, VolumeName,
    @{N='Size(MB)';E={ '{0:N2}' -f ($_.Size/1MB) }},
    @{N='FreeSpace(MB)';E={ '{0:N2}' -f ($_.FreeSpace/1MB) }},
    @{N='Used(MB)';E={ '{0:N2}' -f (($_.Size-$_.FreeSpace)/1MB) }},
    @{N='PercentFree';E={ '{0:P2}' -f ($_.FreeSpace/$_.Size) }},
    @{N='PercentUsed';E={ '{0:P2}' -f (($_.Size-$_.FreeSpace)/$_.Size) }},
    @{N="Drive Type";E={switch ($_.DriveType) { 0 {"(unknown)"} 2 {"USB/FD"} 3 {"HDD"} 4 {"SSD"} 5 {"CD/DVD/BD"} 6 {"RAM disk"}}}},
    @{N="VM Drv";E={if($hvdrive) {$_VhdFormat}else{'no VM Disk'}}},
    @{N="Operating System";E={$OS}},
    @{N='Free Space (Warning)';E={if($_.FreeSpace/$_.Size -lt .10){'Below 10%'}else{'GOOD'}}} |
    Export-CSV $CSV -NoTypeInformation -Encoding UTF8 | Format-Table -AutoSize
    #---------- Wright to File - END.
    #---------- Wright to Screen - Start.
    #Get-WmiObject Win32_LogicalDisk -filter 'DriveType=3' -computerName $servers | ## use this if you want to show only Drive Type 3 drives.
    Get-WmiObject Win32_LogicalDisk -computerName $servers |
    Select-Object `
    SystemName, DeviceID, VolumeName,
    @{N='Size(MB)';E={ '{0:N2}' -f ($_.Size/1MB) }},
    @{N='FreeSpace(MB)';E={ '{0:N2}' -f ($_.FreeSpace/1MB) }},
    #@{N='Used(MB)';E={ '{0:N2}' -f (($_.Size-$_.FreeSpace)/1MB) }},
    @{N='PercentFree';E={ '{0:P2}' -f ($_.FreeSpace/$_.Size) }},
    #@{N='PercentUsed';E={ '{0:P2}' -f (($_.Size-$_.FreeSpace)/$_.Size) }},
    @{N="Drive Type";E={switch ($_.DriveType) { 0 {"(unknown)"} 2 {"(USB/FD)"} 3 {"HDD"} 4 {"SSD"} 5 {"CD/DVD/BD"} 6 {"RAM disk"}}}},
    @{N="VM Drv";E={if($hvdrive) {$_VhdFormat}else{'no VM Disk'}}},
    @{N="Operating System";E={$OS}},
    @{N='Above 10%';E={if($_.FreeSpace/$_.Size -lt .10){'Below 10%'}else{'GOOD'}}} | Write-Output | Format-Table
    #---------- Wright to Screen - END.
    # Show Progress Bar - START
    for($i = 1; $i -lt 101; $i++ ) {write-progress Updating progress-> -perc $i} for($i = 1; $i -lt 101; $i++ ) { write-progress Updating progress -percentcomplete $i -id 1}
    # Show Progress Bar - END
    Write-Host " Your Report finished at: $(((get-date).ToUniversalTime()).ToString("yyyy-MM-dd"))."
    #---------- The Report - End.
    
  • #162297

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    You have a lot of useless code in your script and your script is really badly formatted. You should read the The Unofficial PowerShell Best Practices and Style Guide.

    You should split your script into smaller pieces. This way it would be easier to read and to debug.

  • #162620

    Participant
    Topics: 4
    Replies: 54
    Points: 382
    Helping Hand
    Rank: Contributor

    First thing my PSSharper  sees is a {$_VhdFormat} not divined on row 36 and 66

    and please read the suggestions that Olaf pointed out

You must be logged in to reply to this topic.