Mattaceten
Mattaceten

Reputation: 159

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

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))
   }

}

Upvotes: 5

Views: 33717

Answers (2)

kevinlinxc
kevinlinxc

Reputation: 604

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.

Upvotes: -2

codersl
codersl

Reputation: 2332

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) 
}

Upvotes: 8

Related Questions