Running a powershell command within a batch file -


i trying create active directory security groups using new-adgroup need able execute these within batch file. goal have script prompt user running enter ou , branch prefix used in front of group name.

i started working powershell i'm still not 100% on when use different brackets , quotes , suspect issue.

this original code:

set /p ou=enter ou: set /p prefix=enter prefix: powershell new-adgroup -name “%prefix%-owa test” -samaccountname “%prefix%-owa test” -groupcategory security -groupscope global -path "ou=groups,ou=%ou%,ou=test1,dc=test2,dc=test3,dc=test4,dc=com” 

i able execute command inside powershell within batch file causing problem. tried removing variables equation , executed following:

powershell new-adgroup -name "orf-owa test" -samaccountname "orf-owa test" -groupcategory security -groupscope global -path "ou=groups,ou=branch1,ou=test1,dc=test2,dc=test3,dc=test4,dc=com” 

i receive following error in both cases:

new-adgroup : cannot convert 'system.object[]' type 'system.string' required parameter 'path'. specified method not supported. @ line:1 char:110 + ... e global -path ou=groups,ou=branch1 ,ou=test,dc=test1,dc=test2,dc=test3,dc=com -des ... +                    ~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidargument: (:) [new-adgroup], parameterbin    dingexception     + fullyqualifiederrorid : cannotconvertargument,microsoft.activedirectory.    management.commands.newadgroup  

the way read wrong "path" parameter i'm not sure why is successful in powershell not cmd prompt or bat file.

any suggestions appreciated.

when working powershell inside batch file, have remember line parsed twice. first cmd , powershell. personal rules when doing to

  1. use single quotation marks powershell when used in batch or on command line.
  2. enclose entire powershell command in double quotations if containing special characters

example: single quotations:

powershell new-adgroup -name '%prefix%-owa test' -samaccountname '%prefix%-owa test' -groupcategory security -groupscope global -path 'ou=groups,ou=%ou%,ou=test1,dc=test2,dc=test3,dc=test4,dc=com' 

example: explicit command, scoped, , enclosed in double quotations:

powershell -command "&{new-adgroup -name '%prefix%-owa test' -samaccountname '%prefix%-owa test' -groupcategory security -groupscope global -path 'ou=groups,ou=%ou%,ou=test1,dc=test2,dc=test3,dc=test4,dc=com'}" 

i did not have time test whether these work, give em try ;) illustration of working command (note requires 2 input variables), here 1 of batch powershell commands use:

powershell -command "&{$nlm = [activator]::createinstance([type]::gettypefromclsid([guid]'{dcb00c01-570f-4a9b-8d69-199fdba5723b}')); $connections = $nlm.getnetworkconnections(); $connections | foreach { if ($_.getnetwork().getcategory() -eq %filter%) { $_.getnetwork().setcategory(%category%); } }; }" 

Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -