1
votes

I need to run a file.bat using DSC resources. The only way I was able to successfully execute the file.bat was by using the Package resource. However I receive the following error:

PowerShell DSC resource MSFT_PackageResource failed to execute Set-TargetResource functionality with error message: Package from Directory\To\MyFile\file.bat was installed, but the specified ProductId and/or Name does not match package details

I know the meaning of the error, and I have previously resolved it. However, there is no productId nor a Name for a batch file.

How else can I run a batch file using DSC?

1
Embed batch file into a script resource? - beatcracker
It doesn't work. Have you tried it? - Samer
Nope, but I can't see why it shouldn't. If this batch file doesn't exist on a target machine, store it in the variable using here-string ($batch = @' ..code.. '@), then dump it to the file ($batch | Out-File -Encoding ascii -Force -FilePath 'X:\path\to\mybatch.cmd') and run using & .\mybatch.cmd. If this this doesn't work for you, then post more details about what's going wrong. - beatcracker
YOU ARE A GENIUS!!! I was using Invoke-Command -ScriptBlock {C:\choco\lib\elasticsearch\tools\elasticsearch-2.3.1\bin\service.bat install} and Invoke-Command -ScriptBlock {start C:\choco\lib\elasticsearch\tools\elasticsearch-2.3.1\bin\service.bat install}. However, Invoke-Command -ScriptBlock {& C:\choco\lib\elasticsearch\tools\elasticsearch-2.3.1\bin\service.bat install} worked. I assume & C:\choco\lib\elasticsearch\tools\elasticsearch-2.3.1\bin\service.bat install would work, but I don't care. Thank you a bunch. - Samer

1 Answers

1
votes

I want to count the question as answered. Thank you @beatcracker for providing the answer.

Use the Script resource and in the SetScript script block use

& your\batch\file\directory.yourBatchFile.bat optionalArguments

If that doesn't work, try

Invoke-Command -ScriptBlock {& your\batch\file\directory.yourBatchFile.bat optionalArguments}

:D