我想使用 VBA 在 Powershell(x86) 中执行以下语句:
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Set-ExecutionPolicy -Scope CurrentUser Unrestricted -force;
$DBPath = "U:\Data\Distribution\Common\Dist\Weekly Hours Entry\DC Master Production DB V2\Packing Slip Generation\PackingSlipGeneratorBE.accdb";
$SQL = "SELECT VENDOR, PackingSlipID, EntryNumber, ShipDate, TrackingNumber, RA, EntryDate FROM tblPackingSlipLog WHERE EntryNumber>1000001 AND EntryNumber<1000011;";
$Output = "C:\Users\foo\Documents\Test.csv";
$Con= New-Object -TypeName System.Data.OleDb.OleDbConnection;
$con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= $DBpath";
$ConCommand= $Con.CreateCommand();$ConCommand.CommandText = $SQL;
$OAdapt = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $ConCommand;
$DS = New-Object -TypeName System.Data.DataSet;$OAdapt.Fill($DS);
export-csv -InputObject $DS.Tables[0] -Path $Output -NoTypeInformation -force;$Con.Close()
我可以毫无问题地在 Powershell(x86) 中运行 Powershell(x86)。此外,如果我%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
在命令提示符下运行该部分,然后分别运行 powershell 部分,它也可以按预期工作。但是,当我尝试将它们一起运行时,它不起作用。
调用 VBA 函数:GetPackingSlipData ("WHERE EntryNumber>1000001 AND EntryNumber<1000011")
Function GetPackingSlipData(strSQL_WhereClause As String) As String
On Error Resume Next
Dim wsShell As Object
Dim strPowerShellCommand As String
Dim strOutputFile As String
strOutputFile = GetDocuments & "\Test.csv"
Set wsShell = CreateObject("WScript.Shell")
strPowerShellCommand = "%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Set-ExecutionPolicy -Scope CurrentUser Unrestricted -force; $DBPath = ""U:\Data\Distribution\Common\Dist\Weekly Hours Entry\DC Master Production DB V2\Packing Slip Generation\PackingSlipGeneratorBE.accdb""; " & _
"$SQL = ""SELECT VENDOR, PackingSlipID, EntryNumber, ShipDate, TrackingNumber, RA, EntryDate FROM tblPackingSlipLog " & strSQL_WhereClause & ";"";" & _
"$Output = """ & strOutputFile & """;" & _
"$Con= New-Object -TypeName System.Data.OleDb.OleDbConnection;$con.ConnectionString = ""Provider=Microsoft.ACE.OLEDB.12.0;Data Source= $DBpath"";" & _
"$ConCommand= $Con.CreateCommand();$ConCommand.CommandText = $SQL;" & _
"$OAdapt = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $ConCommand;" & _
"$DS = New-Object -TypeName System.Data.DataSet;$OAdapt.Fill($DS);" & _
"export-csv -InputObject $DS.Tables[0] -Path $Output -NoTypeInformation -force;$Con.Close()"
Debug.Print strPowerShellCommand
Call wsShell.Run(strPowerShellCommand, 1, True)
If Err.Number <> 0 Then
GetPackingSlipData = "Error: " & Err.Number & ", " & Err.Description
Else
GetPackingSlipData = strOutputFile
End If
On Error GoTo 0
End Function
打印的命令:
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Set-ExecutionPolicy -Scope CurrentUser Unrestricted -force; $DBPath = "U:\Data\Distribution\Common\Dist\Weekly Hours Entry\DC Master Production DB V2\Packing Slip Generation\PackingSlipGeneratorBE.accdb"; $SQL = "SELECT VENDOR, PackingSlipID, EntryNumber, ShipDate, TrackingNumber, RA, EntryDate FROM tblPackingSlipLog WHERE EntryNumber>1000001 AND EntryNumber<1000011;";$Output = "C:\Users\foo\Documents\Test.csv";$Con= New-Object -TypeName System.Data.OleDb.OleDbConnection;$con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= $DBpath";$ConCommand= $Con.CreateCommand();$ConCommand.CommandText = $SQL;$OAdapt = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $ConCommand;$DS = New-Object -TypeName System.Data.DataSet;$OAdapt.Fill($DS);export-csv -InputObject $DS.Tables[0] -Path $Output -NoTypeInformation -force;$Con.Close()