1

我正在自动构建旧版 MS Access 应用程序,并且在其中一个步骤中,我正在尝试制作 Access 可执行文件 (.ADE)。我想出了以下代码,它存储在一个文件(PSLibrary.ps1)中:

Add-Type -AssemblyName Microsoft.Office.Interop.Access

function Access-Compile {
param (
    [Parameter(Mandatory=$TRUE,Position=1)][string]$source,
    [Parameter(Mandatory=$TRUE,Position=2)][string]$destination
)
    Write-Output "Starting MS Access"
    $access = New-Object -ComObject Access.Application
    $access.Visible = $FALSE
    $access.AutomationSecurity = 1

    if (!(Test-Path $source)) { Throw "Source '$source' not found" }
    if ((Test-Path $destination)) {
        Write-Output "File '$destination' already exists - deleting..."
        Remove-Item $destination
    }

    Write-Output "Compiling '$source' to '$destination'"
    $result = $access.SysCmd(603, $source, $destination)

    $result

    Write-Output "Exiting MS Access"
    $access.quit()
}

如果我进入 PowerShell ISE 并运行以下命令,那么一切正常,并创建了预期的输出:

PS C:>& "C:\Temp\PSLibrary.ps1"
PS C:>Access-Compile "C:\Working\Project.adp" "C:\Working\Project.ade"

但是,我似乎无法生成正确的 hocus-pocus 来从命令行运行它,就像在自动构建中那样。例如,

powershell.exe -command "& \"C:\\Temp\\PSLibrary.ps1\" Access-Compile \"C:\\Temp\\Project.adp\" \"C:\\Temp\\Project.ade\""

我究竟做错了什么?

4

2 回答 2

1

对于复杂的参数,可以使用 Powershell 的-EncodedCommand参数。它将接受 Base64 编码的字符串。引号、斜杠等不需要转义。

考虑一个将打印其参数的测试函数。像这样,

function Test-Function {
param (
    [Parameter(Mandatory=$TRUE,Position=1)][string]$source,
    [Parameter(Mandatory=$TRUE,Position=2)][string]$destination
)
    write-host "src: $source"
    write-host "dst: $destination"
}

创建命令以加载脚本和一些参数。像这样,

# Load the script and call function with some parameters
. C:\Temp\Calling-Test.ps1;  Test-Function "some\special:characters?" "`"c:\my path\with\spaces within.ext`""

命令语法OK后,编码成Base64形式。像这样,

[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes('. C:\Temp\Calling-Test.ps1;  Test-Function "some\special:characters?" "`"c:\my path\with\spaces within.ext`""'))

你会得到一个 Base64 字符串。像这样,

LgAgAEMAOgBcAFQAZQBtAHAAXABDAGEAbABsAGkAbgBnAC0AVABlAHMAdAAuAHAAcwAxADsAIAAgAFQAZQBzAHQALQBGAHUAbgBjAHQAaQBvAG4AIAAiAHMAbwBtAGUAXABzAHAAZQBjAGkAYQBsADoAYwBoAGEAcgBhAGMAdABlAHIAcwA/ACIAIAAiAGAAIgBjADoAXABtAHkAIABwAGEAdABoAFwAdwBpAHQAaABcAHMAcABhAGMAZQBzACAAdwBpAHQAaABpAG4ALgBlAHgAdABgACIAIgA=

最后,启动 Powershell 并将编码的字符串作为参数传递。像这样,

# The parameter string here is abreviated for readability purposes.
# Don't do this in production
C:\>powershell -encodedcommand LgAgA...
Output
src: some\special:characters?
dst: "c:\my path\with\spaces within.ext"

如果您以后想要反转 Base64 编码,请将其传递给解码方法。像这样,

$str = " LgAgA..."
[Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($str))
# Output
. C:\Temp\Calling-Test.ps1;  Test-Function "some\special:characters?" "`"c:\my path\with\spaces within.ext`""
于 2013-05-23T06:07:20.793 回答
0

像 Bash 这样的 PowerShell 可以使用单引号或双引号

PS C:\Users\Steven> echo "hello"
hello
PS C:\Users\Steven> echo 'hello'
hello

这可以减轻一些头痛,而且我认为您可以使用文字反斜杠而无需转义。

要运行 PowerShell,请选择

Start Menu Programs Accessories Windows Powershell Windows Powershell

于 2013-05-23T05:31:35.207 回答