Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Back Ticks - can't get it to work.

Status
Not open for further replies.

Lectrician

Technical User
Oct 11, 2007
76
GB
I have a script which executes a doc to pdf conversion on my windows server. I originally used a command line switch on Open Office to do the conversion, and this worked, but the PDF created is poor.

I am now using word and OfficeToPDF.exe.

The command required (that works perfectly on the command line) is d:\officetopdf\officetopdf.exe /print d:\test.docx d:\test.pdf
This creates the PDF perfectly.

Trying to execute this is perl, and I run into trouble.

This executes and returns no error, but I have no arguments.

$cmd = "d:\\officetopdf\\officetopdf.exe /print";
`$cmd`;

This wont execute and returns an error of 256 in $?.

$cmd = "d:\\officetopdf\\officetopdf.exe /print d:\\officetopdf\\a.docx d:\\officetopdf\\a.pdf";
`$cmd`;

I have tried all sorts to get this going, and am just not succeeding.

I have tried just file names with no path. If I print $cmd to screen, copy and paste the line, and then paste it into the command prompt, it executes, so the double \\ are doing their job and escaping the 'true' one.

Any ideas?

Thanks.
 
Couple thoughts:
1 - Your assignment $cmd = "d:\\xxx\\yyy" ;
Will translate into d:\xxx\yyy in $cmd. Then when you use `$cmd` it is looking at d:\xxx\yyy and will again translate the \x and \y into their equivalent escape characters. Try using single quotes on the initial $cmd = 'd:\\xxx\\yyy' ; Then the `$cmd` should behave cleaner for you.

2 - If you have access to the perl cookbook - look up their method of using system() to make a call to the OS. Since I can find this online in various places, I'll reprint here but I recommend getting the book. There are some really elegant routines in there
Code:
$rc = 0xffff & system @args;
printf "system(%s) returned %#04x: ", "@args", $rc;
if ($rc == 0) { print "ran with normal exit\n";
} elsif ($rc == 0xff00) { print "command failed: $!\n";
} elsif (($rc & 0xff) == 0) {
    $rc >>= 8;
    print "ran with non-zero exit status $rc\n";
} else {
    print "ran with ";
    if ($rc &   0x80) {
        $rc &= ~0x80;
        print "coredump from ";
    } 
    print "signal $rc\n"
} 
$ok = ($rc == 0);
I've added bells-n-whistles to this basic routine to have it give me a call-stack trace to help debug longer calling paths. But the above should get you started. My apologies to Mr. Torkington and Mr. Christiansen if I've wrongfully posted their code here. FWIW - That book and the Advanced Perl have helped me write code libs much more efficiently.
 
Hi.

Thank you.

I cannot even get system() to work - I just get a 502 error from iis when trying to include a call that way. I struggled with it previously, which is why I ended up using the backticks method instead. That did work fine with the call to Open Office. Not sure why it's not working with this command.
 
Taking a swag here ... Does
Code:
$cmd = "d:\\\\officetopdf\\\\officetopdf.exe /print";
work? (Using 4 backslashes) Try dumping $@, $! and $^E as well. May give more insight.
 
Cheers.

If I execute that command with no arguments, I get the below returned:

$? = 0
$^E = An operation was attempted on something that is not a socket
$! - null
$@ - null

With the arguments added:

$? = 256
$^E = An operation was attempted on something that is not a socket
$! - null
$@ - null

I then realised I can grab the output of the shell command as a variable:

$output = `$cmd`;

This outputs:

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). Did not convert

So I guess I have a problem with permissions - But I am not sure what permissions I need here for IIS? It is an authenticated website, using windows basic authentication (over SSH), so I assumed as long as the user had permission on the folder, it would be fine. I tried giving IUSR permission too....
 
Thinking back, I have seen that sort of thing before - the system call executes under a different set of credentials. At a command prompt I tried 'help start' and 'help cmd' and neither of them showed references on how to supply a user/password in the calling sequence to potentially reset your permissions. Sooo - another idea just to see if you can get something to work: Ever use PsTools or Sysinternals (they're effectively the same program)? I won't plug a website here but it's easy enough to find. Anyway there is a routine in there called PsExec.exe which you can specify a \\computer -u<user> -p<password> and a <command>. I've used this to run program installs on remote computers. I've had the best luck if I have the perl program build a .bat file under the system32 directory of the target computer then call the .bat file using the PsExec command. I realize this is getting way more complicated than what originally started out as a simple $cmd = xxx ; `$cmd` sequence.

Out of curiosity, is the officetopdf.exe making a call out to a web site? If your network does not support anonymous calls out to the web, that could be the source of the socket error. Maybe try a $output = `whoami` ; and see what that gives back. Is that the user you expect? Sorry I'm not much more help at this point.
 
Hi.

officetopdf.exe is just a third party plugin which uses MSword to convert a document to a pdf. It works headless, using MSword to do the work.

All my documents, and MSword are on the server (I know putting word on there is not ideal!).

I sorted this out by amending the permissions in the "dcomcnfg -32". Searched for Office, right clicked properties, and security. I originally added iis_users and IUSR, but then realised I needed to add local user accounts (all users), as the website is authenticated, so it required those users permissions set.

All working now :)
 
A bit late here and it doesn't really solve your problem, but it's worth mentioning anyway - to make your life easier you can use forward slashes in paths for windows and not have to worry about escaping.

For the sake of executing commands in cmd,
Code:
$cmd = "d:\\officetopdf\\officetopdf.exe /print";
is the equivalent of
Code:
$cmd = "d:/officetopdf/officetopdf.exe /print";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top