1

従来の MS Access アプリケーションのビルドを自動化しています。手順の 1 つで、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`""

コマンド構文に問題がなければ、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 に答える