Powershell script doesn't work when called from Matlab

조회 수: 7 (최근 30일)
Les Beckham
Les Beckham 2015년 9월 24일
댓글: Les Beckham 2015년 9월 29일
I have a Powershell script that works when called from the command line (Windows 7) but not when called using the system command within Matlab. The script searches the registry for version information from a specific vendor's software install. When I call it from the command line it generates a file containing the correct version information. When I run it using the system command in Matlab it does generate the file, but it has zero bytes (verified by opening with a hex editor).
What might be causing this?
Here is the script:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\ -rec -ea SilentlyContinue | ForEach-Object {
$CurrentKey = (Get-ItemProperty -Path $_.PSPath)
if ($CurrentKey -match 'Vendor') {
if ($_.PSChildName -match 'InstallProperties') {
$CurrentKey.DisplayVersion
}
}
} | out-File Vendor.txt -Encoding ascii
Here is how I call it from the command line:
C:\...\Analysis>powershell -ExecutionPolicy RemoteSigned -inputformat none -file VersionFile
.ps1
Here is how I call it from Matlab:
[s,r] = system('powershell -ExecutionPolicy RemoteSigned -inputformat none -file VersionFile.ps1')
Note: the return values are s = 0 and r = ''.
I have tried removing the pipe to out-File. On the command line, I get the version number displayed in the command window. When calling from Matlab, I get the same return values shown above.
I am using Matlab R2013b.
Thank you

답변 (1개)

Rahul Goel
Rahul Goel 2015년 9월 28일
Les,
The script you mentioned created an output file "Vendor.txt" but did not write any values to it even when ran from powershell. Same is the case when I executed it in MATLAB.
However, when I executed following, it gave me correct status and return values:
>> [s,r] = system('powershell -inputformat none 2*10; echo ''This is a line''')
s =
0
r =
20
This is a line
  댓글 수: 1
Les Beckham
Les Beckham 2015년 9월 29일
The script will, of course, only generate output if it finds a match for 'Vendor' with corresponding DisplayVersion InstallProperties in the registry. I have such an entry on my machine which is found and written to the file when I run the script from a command line, but not when I invoke the script from inside Matlab. It seems like there is some subtle difference in the cmd environment that is invoked by Matlab but I can't figure out what it is.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by