0

我在这里查看了许多不同的问题/答案并进行了一些研究,但我无法解决这个问题。我正在运行一个脚本来检查注册表项,但我需要在 64 位机器上使用 Powershell 32 位。所以我在 32 位模式下运行 PowerShell 并将它传递给我的脚本块。不过,我需要脚本块才能访问参数,所以我也将它们与脚本块一起传递。但是,当我运行我的代码时,我的第一个参数 ($RegistryKey) 在带有变量的空间中被分解。我尝试过几种不同的方式转义字符串,但没有任何效果。希望有人可以帮助我。

# -----------------------------------------------------------------------------
#
# Powershell script to check registry entry (as first argument to script) and display output on stdout.
#
# Arguments:
#       $RegistryKey: The registry key.
#       $RegistryValue: The registry value name.
#           $Use32BitPowerShell: Boolean as string for whether to use 32-bit PowerShell
#
# Returns:
#       If the registry key exists, and the registry value name is valid, return the registry key,
#           registry value name, registry data, and exists as true
#       If the registry key exists, and the registry value name is invalid, return only the registry 
#       key and exists as false
#       If the registry key doesn't exist, return exists property as false
# -----------------------------------------------------------------------------

$RegistryKey = "HKEY_LOCAL_MACHINE\SOFTWARE\SOME NAME\"
$RegistryValue = 'ValueName_String_StringValueMixedCase'
$Use32BitPowerShell = 'TRUE'

$script =
{
    param([string]$RegistryKey, [string]$RegistryValue, [string]$Use32BitPowerShell)

    # Initialize the RegistryEntry object to be returned.
    $result = New-Object PSObject | select Key, ValueName, DataType, Data, KeyExists, ValueExists, CorrectDataType, CorrectData
    $result.Key = $RegistryKey
    $result.ValueName = $RegistryValue
    $result.DataType = ''
    $result.Data = ''
    $result.KeyExists = $false
    $result.ValueExists = $false
    $result.CorrectDataType = $false
    $result.CorrectData = $false

    # Create a path for the key in the registry.
    $regentry = 'Registry::' + $RegistryKey
    $regentry

    # Get the key object for the registry entry path.
    $key = Get-Item -LiteralPath $regentry

    # If the key exists, get the data for the parameter registry value name. 
    # Set the KeyExists flag on the result object to true.
    if ($key)
    {
        # The key exists, so set the KeyExists flag on the result object to true.
        $result.KeyExists = $true

        # Get the data for the value.
        $data = $key.GetValue($RegistryValue)

        # If there is data, add the data to the result object.
        if ($data)
        {
            # The value exists, so set the ValueExists flag on the result object to true.
            $result.ValueExists = $true

            # Set RegistryData on the result object to the value data.
            $result.Data = [String]$data

            # Set DataType on the result object to the value data type.
            $result.DataType = $key.GetValueKind($RegistryValue)
        }
    }

    # Return a RegistryEntry object.
    # return $result
    $result
}

if ($Use32BitPowerShell.ToUpper() -eq 'TRUE')
{
    & "$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -command "& {$script} -argumentlist $RegistryKey $RegistryValue $Use32BitPowerShell"
}
4

1 回答 1

1

为了测试您的脚本,我修改了脚本以仅测试间距问题。

尝试这个

$RegistryKey = "'HKEY_LOCAL_MACHINE\SOFTWARE\SOME NAME\'"
$RegistryValue = 'ValueName_String StringValueMixedCase'
$Use32BitPowerShell = 'TRUE'
$script =
{
    param(
    [string]$RegistryKey, 
    [string]$RegistryValue,
    [string]$Use32BitPowerShell
    )   
     Write-host $RegistryKey, $RegistryValue, $Use32BitPowerShell
}
if ($Use32BitPowerShell.ToUpper() -eq 'TRUE')
{
    & "powershell.exe" -NoProfile -command "& {$script} -argumentlist '$RegistryKey' '$RegistryValue' '$Use32BitPowerShell'"
}

然后运行脚本-我将脚本命名为spaces.ps1

PS C:\scripts\so> .\spaces.ps1
HKEY_LOCAL_MACHINE\SOFTWARE\SOME NAME\ ValueName_String StringValueMixedCase
于 2013-08-22T20:45:33.750 回答