Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Error compiling script running powershell command

by sweetblood (Prior)
on Dec 28, 2017 at 22:35 UTC ( [id://1206372]=perlquestion: print w/replies, xml ) Need Help??

sweetblood has asked for the wisdom of the Perl Monks concerning the following question:

The following snippet should upload an image to Active Directory. The powershell command runs from a cmd prompt fine, howerver I get a syntax error during compilation when I try to run from my perl script. As far as I can tell, perl really ought to treat the "command" as a string and not attempt to compile it.
#!/usr/bin/perl -w use strict; my $un = 'steve gilbert'; my $jpeg = 'v:/employee photos/out/steve gilbert.jpg'; my $result = `c:\\windows\\system32\\windowspowershell\\v1.0\\powershe +ll.exe -command "Set-ADUser $un -Replace @{thumbnailPhoto=([Byte[]](G +et-Content $jpeg -Encoding Byte))}"`;

I have tried various forms of the string, but I still receive the following error message. I have a feeling that this is one of those really simple bugs that 2 eyes are just not enough to see. If anyone else sees something please let me know.

Thanks!
syntax error at C:\Users\steveg\adimg-test.pl line 5, near "Byte[" Execution of C:\Users\steveg\adimg-test.pl aborted due to compilation +errors.

Sweetblood

Replies are listed 'Best First'.
Re: Error compiling script running powershell command
by Happy-the-monk (Canon) on Dec 28, 2017 at 23:27 UTC

    -Replace @

    It is not compiled, it is called interpolation when a variable is replaced with its contents.

    That is what you want Perl to do with your variable $un and Perl could not have guessed you did not want it to do the same with the next one.

    Your simple solution is putting a backslash in front of the next variable, so make it read -Replace \@

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Error compiling script running powershell command
by NetWallah (Canon) on Dec 29, 2017 at 05:53 UTC
    In addition to the perl interpolation issues others have pointed out, your variables carry names that have spaces.

    This is likely to cause downstream powershell commandlet parsing errors.

    Try populating the vairablles like this to keep their content quote-enclosed after interpolation:

    my $un = '"steve gilbert"'; my $jpeg = '"v:/employee photos/out/steve gilbert.jpg"';

                    We're living in a golden age. All you need is gold. -- D.W. Robertson.

Re: Error compiling script running powershell command
by Laurent_R (Canon) on Dec 28, 2017 at 23:47 UTC
    One basic debugging trick in such a case is to put your command line in a string (between double quotes or within the qq// quoting construct if needed) and to try to print it. This way, you can see how Perl interpolates it and what exactly is being passed to the underlying shell (or powershell in your case):
    my $string = qq/c:\\windows\\system32\\windowspowershell\\v1.0\\powers +hell.exe -command "Set-ADUser $un -Replace @{thumbnailPhoto=([Byte[]] +(Get-Content $jpeg -Encoding Byte))}"/; print $string;
    Once your happy with the quoted string, put it back between backticks.
Re: Error compiling script running powershell command
by huck (Prior) on Dec 28, 2017 at 23:25 UTC

    As far as I can tell, perl really ought to treat the "command" as a string and not attempt to compile it.

    Did you expect it to leave $un and $jpeg untranslated as well? Or did you expect perl to parse it and replace it with the contents of those variables?

    I suspect something similar it going on here. for some reason it is checking Byte (or maybe its the @{...}) as a replaceable part and then finds [] which it doesnt like.

    i would do something like this.

    my $command='Set-ADUser '.$un.' -Replace @{thumbnailPhoto=([Byte[]](Ge +t-Content '.$jpeg.' -Encoding Byte))}'; my $result = `c:\\windows\\system32\\windowspowershell\\v1.0\\powershe +ll.exe -command "$command"`;

Re: Error compiling script running powershell command
by dasgar (Priest) on Dec 29, 2017 at 01:33 UTC
    The powershell command runs from a cmd prompt fine

    Did you manually try the command in a command prompt or a PowerShell prompt? There's a significant difference between two. That may or may not be a factor in your situation.

    As far as I can tell, perl really ought to treat the "command" as a string and not attempt to compile it.

    The contents inside of the backticks is interpolated by Perl. You can get more information by reading the documentation on Quote and Quote-like Operators. Based on the error message that you've provided, it looks like Perl is trying to interpolate PowerShell syntax (in particular your data type casting step) and is getting confused. If you follow Happy-the-monk's suggestion, that should fix that issue. However, I suspect that you've got bigger issues to deal with once you get past this issue. And those issues are with the PowerShell environment.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1206372]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found