3

I'm very new to Powershell. Im basically just using my C# logic and .net experience along with google to create this script. I dont understand why im getting the error:

Cannot process argument transformation on parameter 'machine'. Cannot convert value to type System.String

function IterateThroughMachineSubkeys{
(
    [Parameter()]
    [string]$machine,
    [Microsoft.Win32.RegistryKey]$subKey
)

    foreach($subKeyName in $subKey.GetSubKeyNames())
    {
        Write-Host -Object ([System.String]::Format("Machine: {0} Module: {1} Version: {2}",
                            $machine.ToString(), $subKeyName.ToString(), 
                            $subKey.OpenSubKey([string]$subKeyName).GetValue("Version", "Not found", [Microsoft.Win32.RegistryValueOptions]::None)))
    }
}

This is where im calling the function:

switch -CaseSensitive (ValidateConsoleInput((Read-Host).ToString()))
{
   "E" 
   {
    IterateThroughMachineSubkeys($machine.ToString(), $subKey)
   }
   "C"
   {
    Write-Host -Object "Enter the module name to be queried in the registry:"
    GetSpecificSubkey($machine, $subkey, [string](Read-Host))
   }

}
Mattaceten
  • 129
  • 2
  • 5
  • 14

2 Answers2

6

There are a couple of issues with your code.

1) The function parameters are not specified correctly; it should be:

function IterateThroughMachineSubkeys([string]$machine, [Microsoft.Win32.RegistryKey]$subKey)
{
  ...
}

2) The function call is incorrect; it should be:

IterateThroughMachineSubkeys -machine $machine.ToString() -subKey $subKey

Here is the function in full:

function IterateThroughMachineSubkeys([string]$machine, [Microsoft.Win32.RegistryKey]$subKey)
{
    foreach($subKeyName in $subKey.GetSubKeyNames())
    {
        Write-Host -Object ([System.String]::Format("Machine: {0} Module: {1} Version: {2}",
                            $machine.ToString(), $subKeyName.ToString(), 
                            $subKey.OpenSubKey([string]$subKeyName).GetValue("Version", "Not found", [Microsoft.Win32.RegistryValueOptions]::None)))
    }
}

$testKey = get-item "HKCU:\Software"
IterateThroughMachineSubkeys -machine . -subKey $testKey

Or using PowerShell cmdlets:

$key = get-item "hkcu:\Software\Google\Chrome"
$key | get-childitem | foreach-object { 
    write-host "Machine: $machine Module: $($_.PSChildName) Version: " `
    $key.OpenSubKey($_.PSChildName).GetValue("Version", "Not found", [Microsoft.Win32.RegistryValueOptions]::None) 
}
Seymour
  • 7,043
  • 12
  • 44
  • 51
codersl
  • 2,222
  • 4
  • 30
  • 33
  • Thanks for the time to explain man. Like I said I'm new to powershell and I was just trying to bang something out. I appreciate it. – Mattaceten Mar 29 '17 at 17:02
-1

Had this problem, just wanted to point out what fixed it for me:

Print out the variable (echo, Write-Host whatever) and pay incredible attention to what it prints out.

In my case, I didn't assign a return value to a regex search inside my function, so it automatically printed out "True" inside my function, and printouts are returned along with your actual return value as far as I can tell in Powershell, so my return value was "True [actual return value]" which was throwing this error.

kevinlinxc
  • 470
  • 5
  • 20