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

12,313

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) 
}
Share:
12,313
Mattaceten
Author by

Mattaceten

Updated on June 05, 2022

Comments

  • Mattaceten
    Mattaceten almost 2 years

    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
    Mattaceten about 7 years
    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.