I am trying to run a .ps1 which will allow users to select files from a Windows Dialog, then select the script they wish to run on each of those files. Then for each of those files, open the AutoCAD Core Console and process.
When I run files and script using a .bat or through CMD directly I have no issues. When I attempt to run through the Powershell script below, I get the Accoreconsole.exe to boot but it immediately closes. This same script worked on a previous machine, so I am wondering if it has something to do with security or execution policy?
AcCoreConsole requires an input in the format below. All my powershell script is doing is attempting to recreate a path similar to this.
AcCoreConsole.exe [/i ] /s [/product ] [/l ] [/isolate ] [/readonly] [/p[rofile] ]
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
#Sets username for initial path and script path
$env:UserName
#Initial path to project Sheets directory
$initialDirectory = "C:Users$env:UserNameDesktopPW Exportsest"
#Initial path to project Script directory
$scriptDirectory = "C:Users$env:UserNameDesktopPW Exportsest"
#Number of instances of the AutoCAD Core Console at one time - Recommended 5
$Accoreinstance = 1
#This sets the variable for CAD files in an array or list as string values
[array]$CADF = @()
$CADF = Get-CADFiles
#This sets the variable for your script file in an array or list as string values
[array]$SCR = @()
$SCR = Get-Script
#This to bring up Windows dialog prompt to select CAD files to be processed
Function Get-CADFiles($CADF)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = $initialDirectory
$OpenFileDialog.Multiselect = $true
$OpenFileDialog.Title = "Choose CAD Files The You Wish To Process"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.FileNames
}
#This to allow for a second Windows dialog promt to select the script to be run on the files above
Function Get-Script($SCR)
{
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = $scriptDirectory
$OpenFileDialog.Title = "Choose Script File To Run"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.FileName
}
$CADF_list = get-item $CADF
#This takes the selected files from windows dialog and processes
foreach ($file in $CADF_list) {
#This is to count the number of current AcCore running
$running = @(Get-Job | Where-Object {$_.State -eq 'Running'})
#This will wait until the number of instances is lower than what was set on with $Accoreinstance
if ($running.Count -ge $Accoreinstance) {
$running | Wait-Job -Any | Out-Null
}
Write-Host "Starting to process $file"
#This will start the running of script on the files
Start-Job -ScriptBlock {
#This is where you set which version of Accore you are going to be using to run
$AccoreVrs = "C:Program FilesAutodeskAutoCAD 2020accoreconsole.exe"
$arg1 = ' /i ' + $using:file + ' /s ' + $using:SCR
# Start the core-console for each instance and set the job to wait until it finishes
Start-Process $AccoreVrs $arg1 -wait
} | Out-Null
}
# Wait for all jobs to complete and results ready to be received
get-job | Wait-Job | Out-Null
# Process the results
foreach($Accoreinstance in Get-Job)
{
$result = Receive-Job $Accoreinstance
Write-Host $result
}
Remove-Job -State Completed
question from:
https://stackoverflow.com/questions/65945406/problem-executing-accoreconsole-exe-through-powershell 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…