# Problems reading in from array

**URL:** https://forums.powershell.org/t/problems-reading-in-from-array/2457
**Category:** PowerShell Help
**Created:** [April 14, 2014, 1:38am UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457 "2014-04-14T01:38:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dmdougie](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@dmdougie](https://forums.powershell.org/u/dmdougie)
#### Post date: [April 14, 2014, 1:38am UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/1 "2014-04-14T01:38:42Z")

</div>

Hi,

I’m having some issue with following script:

`
$Computers = get-adcomputer -filter * -searchbase "ou=computers,dc=domain,dc=com" | foreach-object {$_.name} 
foreach ($computer in $Computers)
`

`{get-windowskey -targets $computer}
`

Get-windowskey is a function I have loaded into my environment which takes the name of a pc, stated with the param -targets, and returns the Windows version and product key. When I run the above script with hard coded $Computers, it runs great. However, running the above gives me the below error, any advice?

Thanks in advance

`
Cannot convert value "\\CAND8TQ14J\root\default:stdRegProv" to type "System.Management.ManagementClass". Error: "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
At C:\Powershell\GetProductKeyFunction.ps1:11 char:9
+ $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToWMIClass
You cannot call a method on a null-valued expression.
At C:\Powershell\GetProductKeyFunction.ps1:12 char:9

```
    $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)

```

```
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

CategoryInfo : InvalidOperation: (🙂 , RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull

Cannot index into a null array.
At C:\Powershell\GetProductKeyFunction.ps1:13 char:9
`
`

```
    $binArray = ($data.uValue)[52..66]

```

`- `
```
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```
`
`
CategoryInfo : InvalidOperation: (🙂 , RuntimeException
` - `FullyQualifiedErrorId : NullArray
`

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [April 14, 2014, 1:45am UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/2 "2014-04-14T01:45:03Z")

</div>

Your ForEach-Object is a bit of an odd way to do what you’re after…

`
Get-ADComputer -filter * | Select -Expand Name
`

Is a bit more common.

The errors are coming from inside your Get-WindowsKey function. Without seeing it, it’s a bit difficult to guess what might be wrong. But, the error “RPC server is unavailable” suggests that it isn’t connecting to the computer listed in $target.

I suspect you might be able to query the same info more easily by using the Win32\_OperatingSystem class, too. That’d eliminate needing to mess with the registry, if the info you need is in there.

---

<div class="post-metadata">

### Author: ![dmdougie](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@dmdougie](https://forums.powershell.org/u/dmdougie)
#### Post date: [April 14, 2014, 1:52am UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/3 "2014-04-14T01:52:17Z")

</div>

Hey Don, thank you for your response. I’ll make that amendment to the script, thanks for the suggestion.

I noticed as soon as I posted that the error looked to be stemming from the function itself. It’s one I grabbed from the net.

Thanks for your time and insight.

Here is the function:  
`
function Get-WindowsKey {
## function to retrieve the Windows Product Key from any PC
## by Jakob Bindslet (jakob@bindslet.dk)
param ($targets = “.”)
$hklm = 2147483650
$regPath = “Software\Microsoft\Windows NT\CurrentVersion”
$regValue = “DigitalProductId”
Foreach ($target in $targets) {
$productKey = $null
$win32os = $null
$wmi = [WMIClass]“\$target\root\default:stdRegProv”
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$binArray = ($data.uValue)[52…66]
$charsArray = “B”,“C”,“D”,“F”,“G”,“H”,“J”,“K”,“M”,“P”,“Q”,“R”,“T”,“V”,“W”,“X”,“Y”,“2”,“3”,“4”,“6”,“7”,“8”,“9”
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i–) {
$k = 0
For ($j = 14; $j -ge 0; $j–) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = “-” + $productKey
}
}
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target
$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value $win32os.Caption
$obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
$obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
$obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
}
}
`

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [April 14, 2014, 2:00am UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/4 "2014-04-14T02:00:34Z")

</div>

Looks like all you really need is some error handling. If the ([WMIClass]“\$target\root\default:stdRegProv”) expression throws an error such as RPC Server is Unavailable, there’s really no point in trying to run the rest of the code for that particular target.

---

<div class="post-metadata">

### Author: ![dmdougie](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@dmdougie](https://forums.powershell.org/u/dmdougie)
#### Post date: [April 14, 2014, 7:19pm UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/5 "2014-04-14T19:19:19Z")

</div>

Thanks Dave, I’ll take a look at what is needed and how to implement it. Price you pay for using someone elses code I guess!

Thanks again.

---

<div class="post-metadata">

### Author: ![dmdougie](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@dmdougie](https://forums.powershell.org/u/dmdougie)
#### Post date: [April 14, 2014, 8:49pm UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/6 "2014-04-14T20:49:21Z")

</div>

Just to give an update, I managed to get it working. I changed the param for the function to except get-content, and added some error handling. Working out the rest of the kinks, but pretty happy with what I have so far. Thanks to you both for pointing me in the right direction.

Regards,  
Doug

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/problems-reading-in-from-array/2457/7 "2024-05-16T20:52:11Z")

</div>


