viewpoint-particle

Author Topic: Multiple instances of StrReplace  (Read 154 times)

bettman

  • Newbie
  • *
  • Posts: 41
Multiple instances of StrReplace
« on: June 16, 2020, 02:38:30 pm »
Hi,

I am attempting to write a script which retrieves a list of anyone in the local administrators group. Once I have that, I want to remove certain members/groups from that list and use what is left over. I have the following code which works fine, but I am wondering if there is a cleaner way to do this? Is it possible to nest Strreplace (or a similar function) in a single line of code?

Admins = wntMemberList('','Administrators', @LOCALGROUP,1,0)
Admins = StrReplace(Admins,'.\Administrator','')
Admins = StrReplace(Admins,'XYZ.com\PC Admins','')
Admins = StrReplace(Admins,'XYZ.com\Domain Admins','')

Thank you

td

  • Tech Support
  • *****
  • Posts: 3467
    • WinBatch
Re: Multiple instances of StrReplace
« Reply #1 on: June 16, 2020, 02:52:37 pm »
A WIL function is an expression so you can use it anywhere in a statement the makes sense for the function's return type.  So yes, you can nest away to your heart's content; provided you don't exceed maximum line length.  However, code is easier to debug if you don't nest function calls so it is better to get the kinks out of a section of script before entering nesting nirvana.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bettman

  • Newbie
  • *
  • Posts: 41
Re: Multiple instances of StrReplace
« Reply #2 on: June 16, 2020, 06:29:18 pm »
What is the syntax to be used if I were to nest those strreplace commands into one statement?

JTaylor

  • Pundit
  • *****
  • Posts: 1363
    • Data & Stuff Inc.
Re: Multiple instances of StrReplace
« Reply #3 on: June 16, 2020, 08:06:32 pm »
Admins = wntMemberList('','Administrators', @LOCALGROUP,1,0),'.\Administrator','')
Admins = StrReplace(StrReplace(StrReplace(Admins,'XYZ.com\PC Admins',''),'.\Administrator',''),'XYZ.com\Domain Admins','')

bettman

  • Newbie
  • *
  • Posts: 41
Re: Multiple instances of StrReplace
« Reply #4 on: June 17, 2020, 05:31:41 am »
Thank you for the example. That's exactly what I was looking for.

JTaylor

  • Pundit
  • *****
  • Posts: 1363
    • Data & Stuff Inc.
Re: Multiple instances of StrReplace
« Reply #5 on: June 17, 2020, 05:46:25 pm »
Probably obvious but most of the functions can be used in such a way, even with each other.

Jim