r/PowerShell • • 23h ago

Question Function Issue in $PROFILE path

0 Upvotes

Hello everyone. I hope you are having a good time.

```powershell

function gethash {

param([string]$string = "")

if ($string -eq [string]::Empty -or $string -isnot [string])

{

Write-Output "input not valid"

}

else

{

(Get-FileHash -algorithm sha256 $string).Hash | tee -Variable b

}

}
```

I Have this function in my $Profile script. when i run gethash something it returns the hash string as expected, but when I try to print the variable containing the hash i just obtained from piped command tee, (alias of Tee-Object) as an input with `$b

it prints nothing, the variable is not saved.

on the other hand, if i copy the exact same command from the $profile function directly through the terminal, $b prints what expected: the hash of the file.

I haven't used this function so much, but I'm pretty sure it worked before today.

Thanks in advance to any who will respond.


r/PowerShell • • 21h ago

Solved Gathering meter reads from Konica printers.

13 Upvotes

(I'll start by admitting that some of my PoSh may be unconventional / and abbreviated - I have embraced it accordingly).

I needed to come up with a way to create some logs of the usage on the fleet of 10 Konica printers across four locations / print servers.

These logs will be used to validate the pricing figures our printer support vendor provides (pricing is based on estimated / averaged usage per device - that data is audited on a quarterly basis, via automated auditing tools on each print server - FM Audit).

Currently - Without any of our own historical data - We have to take the vendors word, that those quarterly averages are 'OK'...

So, someone from accounting asked me for help.

The below script collects settings info for all of the Konica printers on each of the print servers, then uses the 'PortName' (IP address) to run an SNMP query on each device (SNMP must be enabled in the devices network settings).

The critical items are the OID's.

Figuring out which ones pull in the values for 'black' and for 'color' were the first challenge.

Once I got that figured out - It took me a while to find the OID that gathers the devices serial number - I could NOT find this documented anywhere, and AI (Copilot, Gemini, Claude) wasn't able to either.

For this last bit - I ended up walking (snmpwalk) through ALL 7125 OID's to locate the one that gathers the serial number.

wsl snmpwalk -v2c -c public <printer IP> 1.3.6.1 > 'C:\Users\richie\Documents\PoSh_Stuff\OID_walk.txt'

And looking for the serial number I already knew for that printer.

Select-String -Path 'C:\Users\richie\Documents\PoSh_Stuff\OID_walk.txt' -Pattern '<SerialNumber>'

OID_walk.txt:460:iso.3.6.1.2.1.43.5.1.1.17.1 = STRING: "<SerialNumber>"

Posting what I figured out.

I will pipe '$Report' to a Csv, and save it accordingly.

I'll be scheduling this to run on a quarterly basis - And handing the Csv's over to accounting when they ask for them.

$AllKonica = @()

("Site1,site2,site3,site4").Split(',') | % {
    If (Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
    $AllKonica += Get-Printer -ComputerName $_ | ? { $_.DriverName -match 'Konica' -and $_.PortName -NOTmatch "portprompt|microsoft|_"} | Select ComputerName, Name, PortName, DriverName, Comment 
    } 
}

$Report = @()
$AllKonica | % {

$PrintServer =  $_.ComputerName 
$PrinterIP = $_.PortName  
$PrinterName = $_.Name
$Sn = $_.Comment
$SNMP_Community = "public"

If (Test-Connection $PrinterIP -Count 1 -ErrorAction SilentlyContinue) {

$OIDs = [Ordered]@{
    "Model"        = ".1.3.6.1.4.1.18334.1.1.1.3.5.2.2.0"
    "SerialNo"     = ".1.3.6.1.2.1.43.5.1.1.17.1"
    "Total"        = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.1.1.0"
    "Print_Black"  = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.2"
    "Copy_Black"   = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.1"
    "Print_Color"  = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.2"
    "Copy_Color"   = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.1"
    "Print_2Color" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.4.2"
    "Copy_2Color"  = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.4.1"
}

$snmp = New-Object -ComObject OlePrn.OleSNMP

try {
    $snmp.Open($PrinterIP, $SNMP_Community, 2, 1200)
    $Data = [Ordered]@{ "Printer" = "$PrinterName - $PrinterIP" }

foreach ( $Key in $OIDs.Keys ) {
    try { $Value = $snmp.Get($OIDs[$Key])
         if ( $null -eq $Value -or $Value -eq "" ) 
         { $Data[$Key] = 0 }
         #{ $Data[$Key] = $null }
          else { $Data[$Key] = $Value }
    } # END 'try' # 2
    # catch { $Data[$Key] = $null }
    catch { $Data[$Key] = 0 }
} # END 'foreach ($Key in $OIDs.Keys)'

$TotalColor = [int]$Data["Print_Color"] + [int]$Data["Print_2Color"] + [int]$Data["Copy_Color"] + [int]$Data["Copy_2Color"]
$TotalBlack = [int]$Data["Print_Black"] + [int]$Data["Copy_Black"] 

$Report += [PSCustomObject][Ordered]@{
"Model"    = $Data["Model"]
"SerialNo" = $Data["SerialNo"]
"Printer"  = $Data["Printer"]
"Total"    = $Data["Total"]
"Black"    = $TotalBlack
"Color"    = $TotalColor
    }
} # END 'try' # 1

catch { Write-Host "Connection failed: $PrintServer -> $PrinterIP ($PrinterName)" -F 14 }
finally { $snmp.Close() }

} # END 'If (Test-Connection...'

else {
    $Report += [PSCustomObject][Ordered]@{
        "Model"    = "Unk"
        "SerialNo" = $Sn
    "Printer"  = "$PrinterName - Unreacheable"
        "Total"    = "NULL"
        "Black"    = "NULL"
        "Color"    = "NULL"
    }
} # END 'else'

} # END '$AllKonica | %'

$Report | ft