SoulFallens Powershell struggle bus

I’m using this thread as a dump form my general Powershell help questions as i wanted to stop cluttering the lounge but i figured people would still be interested to see how i’m progressing rather than just taking this to a DM

2 Likes

@admindev why the heck is my diskpair loop only renaming the first disk idk what i did wrong?

 $disks = Get-Volume | Select DriveLetter | ForEach-Object {$_.DriveLetter}

foreach($disk in $disks) {
    Get-Volume | Get-Partition | where DriveLetter -eq $disk | Get-Disk | Select-Object Number | ForEach-Object {$_.Number} | Out-File -Append disk_num.txt
}

$nums = Get-Content .\disk_num.txt
$diskPairs = @{}
$i = 0
foreach($disk in $disks) {
    if($disk -eq $null) {
        continue
    } else {
$diskpairs[$nums[$i]] = $disk
$i++
    }
    }

$label = Get-Disk | Select Friendlyname

$i = 0
foreach( $diskpair in $diskpairs ) {
    $currentdisk = "DriveLetter = '" + $diskpairs["$i"] + ":'"
    $drive = Get-WmiObject -Class win32_volume -Filter "$currentdisk"
    $drive.Label = "test"
    $drive.Put()
    $i++
}
2 Likes

Try

foreach( $disk in $disks ) {
    $currentdisk = "DriveLetter = '" + $diskpairs["$i"] + ":'"
    $drive = Get-WmiObject -Class win32_volume -Filter "$currentdisk"
    $drive.Label = "test"
    $drive.Put()
    $i++
}

Lol damn you

1 Like

Lmfao i’m dying

1 Like

had to add that same null exception piece as the previous loop but it works now! now i just need to get the label variable to remove the label from the friendly name so the drive doesn’t wind up named “FriendlyName __________ Drivemodelnumber” instead of “Drivemodelnumber”

1 Like

@admindev for some reason when i try to rename a drive like this i get the error “exceeds character limit” is it still counting the hidden characters?

$label = Get-Disk | Select Friendlyname

$currentdisk = "DriveLetter = '" + $diskpairs["$i"] + ":'"
$drive = Get-WmiObject -Class win32_volume -Filter "$currentdisk"
$drive.Label = $label[$i] | ft -HideTableHeaders
$drive.Put()

also, putting hide table headers in the label variable causes an error when it tries to reference i in label too which I found odd.

I’ve verified outside of the renaming loop that it shows correctly with the drive FriendlyName and nothing else.

1 Like

nevermind i just needed to trim, it works, now i need to create and exclusion for USBs

Edit: systems with optical drives also break everything

1 Like

@admindev the code you wrote to do the matching seems to be having a bit of an oopsies now that I’ve added this random old 40gb hdd to the system. to be perfectly honesty the piece you gave me is a little above my head so i’m not sure how to diagnose why it’s flip flopping.

1 Like

Hm… So you added a drive and the volume/letters became disassociated?

I added a drive and started everything over from scratch as if it was a new system (unititialized disks etc). for some reason this drive is messing it up

is this older sata 1 drive missing an identifier of some kind or something?

In Get-Volume is disk 3 D and disk 2 F?

Disk 3 is F and Disk 2 Is D

Hm… I can’t get that to replicate.

Tbh it’s kind of over my head too :wink: Give me some time to mess with it

Oh god, welp, i’m fucked.

2 Likes

Appears this issue may just be specifically with this old Sata I drive I can’t seem to replicate it with other drives

1 Like

Oh shit sorry, I meant to follow up. I couldn’t get it to replicate on battlestation or old workstation.

well seeing as we don’t actually use sata 1 drives and that was just a random thing i had laying around that i decided to try I think we’re fine.

1 Like

An article on ms says the drive number comes from the plug n play enumeration, not windows.

@admindev script appears to be 100% working now, i just need to test and make sure a system with raid won’t break it.

1 Like