Running a powershell job within a .bat file that run a ssis package

bstar_ 61 Reputation points
2021-02-26T08:56:04.433+00:00

Hi all,
I am very new to powershell and batch file commands.
Having trouble running a powershell job within a .bat file that run a ssis package.
I tried running the command in powershell, it works but having trouble after merging it with .bat file.

Command for the powershell:
$securepw = convertto-securestring -string "hello" -asplaintext -force
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securepw)
$pw = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)

Flow in .bat file:

  1. run powershell command to decrypt the securestring and store in a variable (e.g pw)
  2. powershell command to pass variable (pw) back
  3. use the value of the variable back to set parameter for the ssis package
  4. run the ssis package job

Any help is appreciated!

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,592 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,555 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,706 Reputation points Microsoft Vendor
    2021-02-26T10:10:01.167+00:00

    Hi,

    You cannot get the powershell variables in batch but you can get the output of powershell.

    $securepw = convertto-securestring -string "hello" -asplaintext -force  
    $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securepw)  
    [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)  
    

    The batch file

    FOR /F "DELIMS=" %%G IN ('powershell.exe -ExecutionPolicy Bypass -File C:\temp\pw.ps1') DO SET "pw=%%G"  
    

    https://stackoverflow.com/questions/23618016/use-powershell-variable-in-batch

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. MotoX80 34,431 Reputation points
    2021-02-26T11:49:14.64+00:00

    Don't use a .bat file at all. Put all of the commands in a .ps1 file and have Powershell execute it.

    How many lines are in the .bat file? Does it just call an executable and pass it some variables, or does it do something more complex like testing to see if files exist and then use GOTO?

    If it's not too big can you share the contents?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.