python - pywinrm - running New-Mailbox powershell cmdlet remotely -
i've been trying pywinrm module run new-mailbox powershell cmdlet remotely. have far:
import winrm  ps_text = "$pass = convertto-securestring -string '%s' -asplaintext -force; \                 add-pssnapin microsoft.exchange.management.powershell.e2010; \                 new-mailbox -userprincipalname '%s@contoso.lan' \                 -alias '%s' -name '%s' -password $pass \                 -firstname '%s' \                 -lastname '%s' \                 -displayname '%s' \                 -primarysmtpaddress '%s'" % \                 ('password123',                  'jfd',                 'john.doe',                 'john doe',                 'john',                 'doe',                 'john doe',                 'john.doe@contoso.co.uk')  remote = winrm.session('https://contoso.co.uk:5986', auth=('ps_remote', 'irrelevant')) r = remote_session.run_cmd("powershell", ["-version", "2.0", "-c", ps_text])   this output get:
new-mailbox : value cannot null. parameter name: serversettings @ line:1 char:147 + $pass = convertto-securestring -string 'password123' -asplaintext -force; add-pssnapin microsoft.exchange.management.powershell.e2010; new-mailbox <<<< -userprincipalname 'jfd@contoso.lan' -alias 'john.doe' -name 'john doe' -password $pass -firstname 'john' -lastname 'doe' -displayname 'john doe' -primarysmtpaddress 'john.doe@contoso.co.uk' + categoryinfo : notspecified: (:) [new-mailbox], argumentnullexception + fullyqualifiederrorid : system.argumentnullexception,microsoft.exchange.management.recipienttasks.newmailbox
i figure doesn't $pass being unquoted, wrap in single quotes , get:
new-mailbox : cannot bind parameter 'password'. cannot convert "$pass" value of type "system.string" type "system.security.securestring".at line:1 char:231 + $pass = convertto-securestring -string 'password123' -asplaintext -force; add-pssnapin microsoft.exchange.management.powershell.e2010; new-mailbox -userprincipalname 'jfd@contoso.lan' -alias 'john.doe' -name 'john doe' -password <<<< '$pass' -firstname 'john' -lastname 'doe' -displayname 'john doe' -primarysmtpaddress 'john.doe@contoso.co.uk' + categoryinfo : invalidargument: (:) [new-mailbox], parameterbindingexception + fullyqualifiederrorid : cannotconvertargumentnomessage,microsoft.exchange.management.recipienttasks.newmailbox
emphasis mine. , interprets literally instead of expanding $pass variable. there way can correctly executed?
some notes:
- i'm not using documented 
run_psmethod ofwinrm.sessionnot run against correct version of powershell on remote side (version 2.0 required here that's exchange management snap-in requires). - i have tried using low level api of pywinrm detailed on package page, it's no good.
 - if insert 
$pass;after first line of powershell, standard output returnsystem.security.securestring, makes weirder complain null argument in first example. - i have tried wrapping in triple quotes, , swapping quote styles. no dice.
 - i have tried various other methods, including making subprocess calls local powershell run 
new-pssessionimport remote session, fails access cmdlet need. i've tried running script blocks remotely usinginvoke-command, though supposedly imports snap-in successfully, doesn't run actual cmdlet. - a pure python solution preferable (hence use of pywinrm), i'm open @ stage. examples of running 
new-mailboxremotely pretty sparse, or maybe google-fu weak in instance. 
in powershell, single quotes prevent variable expansion occurring.
my recommendation be:
- remove single quotes 
$pass - notice in error message posted, error coming 
-serversettings, not-password - pass in value 
-serversettings(i couldn't find parameter in exchange documentation, check if exists or not in version) - validate powershell command on windows system, before attempting issue command through 
pywinrm 
hope helps! cheers
Comments
Post a Comment