如何将脚本块和带有空格的参数传递给新的PowerShell实例?

时间:2013-08-22 19:40:08

标签: powershell powershell-v2.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"
}

1 个答案:

答案 0 :(得分: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