Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am automating the build of a legacy MS Access application, and in one of the steps, I am trying to make an Access executable (.ADE). I have come up with the following code, which is stored in a file (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()
}

If I go into the PowerShell ISE and run the command below, then everything works fine, and the expected output is created:

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

However, I can't seem to generate the right hocus-pocus to get this running from the command line, as I would in an automated build. For instance,

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

What am I doing wrong?

share|improve this question

2 Answers 2

PowerShell like Bash can take single or double quotes

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

this can alleviate some of the headache, also I think you can use the literal backslashes without escaping.

To run PowerShell, choose

Start Menu Programs Accessories Windows Powershell Windows Powershell

share|improve this answer
    
The problem that I am having does not manifest when run from within the Powershell ISE; it's when I try to run it from CMD. –  David Keaveny May 23 '13 at 5:52
    
How would I do that as part of an automated build? That's the purpose of my question. –  David Keaveny May 23 '13 at 6:12

For complex parameters, you can use Powershell's -EncodedCommand parameter. It will accept a Base64 encoded string. No escaping is needed for quotes, slashes and such.

Consider a test function that will print its parameters. Like so,

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

Create command to load the script and some parameters. Like so,

# 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`""

After the command syntax is OK, encode it into Base64 form. Like so,

[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`""'))

You'll get a Base64 string. Like so,

LgAgAEMAOgBcAFQAZQBtAHAAXABDAGEAbABsAGkAbgBnAC0AVABlAHMAdAAuAHAAcwAxADsAIAAgAFQAZQBzAHQALQBGAHUAbgBjAHQAaQBvAG4AIAAiAHMAbwBtAGUAXABzAHAAZQBjAGkAYQBsADoAYwBoAGEAcgBhAGMAdABlAHIAcwA/ACIAIAAiAGAAIgBjADoAXABtAHkAIABwAGEAdABoAFwAdwBpAHQAaABcAHMAcABhAGMAZQBzACAAdwBpAHQAaABpAG4ALgBlAHgAdABgACIAIgA=

Finally, start Powershell and pass the encoded string as a parameter. Like so,

# 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"

Should you later want to reverse the Base64 encoding, pass it into decoding method. Like so,

$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`""
share|improve this answer
    
At the risk of asking a stupid question, how do I generate the encoded command from the DOS command line? –  David Keaveny May 23 '13 at 6:15
    
You don't. Use a Powershell prompt to create the command. –  vonPryz May 23 '13 at 6:16
    
Is that practical when running an automated build, where the parameters may vary from build to build?\ –  David Keaveny May 23 '13 at 6:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.