I know many of us work on lots of different technologies, many of which don’t have native PowerShell cmdlets, and that kind of thing. Sometimes it’s DOS, sometimes, it’s Telnet/SSHing into a command line where you got to run individual command strings to fix a bunch of individual objects. I know many of you guys end up hacking stuff together using Excel or other tools to basically to assemble a repeated command to fix multiple objects, or create rules or whatever, like…
First part of command object1 second part of command
First part of command object2 second part of command
And you got a list of all your objects you got to do this on. This can be painful.
Let me give you an example…
Working on an issue with an old version of EMC RecoverPoint, which has no PowerShell integration.
Basically, the customer masked some LUNs to VMAX front end ports that aren’t hooked up, and RecoverPoint is barking because it can’t access those ports. So the customer has to unmap the front end ports and unmask. I know for many of you guys, it’s this garbledy gook of tech you don’t work with. In the end, the specific technology doesn’t matter.
RecoverPoint reports all the volumes that are the problem, like this:
Devices: 2B3B,277F,83D8,2B34,2250,21DD,2774,102A,21E2,281E,102B,281F,83D5,83E1,12B7,83CB,83DC,83DF,2775,83DB,24BB,83CE,818D,83D9,2784,2776,83CD,83DA,12CF,281D,83E3,0FB4,83D0,2B50,83CC,0FA3,8037,0FB3,83D1,2772,8196,83D4,83CF,83E2,83D3,83D7,2773,277E,12CC,12C9,8038,83DE,8036,1518,83D6,83D2,83DD,83E0
The first thing I need is an array of these I can pump into a loop.
This is stupid simple for PowerShell. Each device is separated by a comma, so I can just use comma as the split character.
(Cut off the long string of devices, you get the idea)
$devicelist = “2B3B,277F,83D8,2B34,2250,21DD”
$devices = $devicelist.split(‘,’)
Now, if you type $devices, you get:
2B3B
277F
83D8
2B34
2250
21DD
Now we have our simple array.
Also, another helpful thing to know is if you have a sequence of numbers, you can use another PowerShell trick. Say I need an array of objects that’s object1-10. Also easy:
$objects = 1..10 | foreach-object {“object” + $_}
Type $objects and you get:
object1
object2
object3
object4
object5
object6
object7
object8
object9
object10
Yes, you can do this for IPs. Say I want an array of all IPs in 192.168.0.0/24, so I can ping them or whatever.
$ips = 1..254 | foreach-object {‘192.168.1.’ + $_}
Maybe port ranges with “TCP” in front for firewall rule statements.
$tcpports = 3000..4000 | foreach-object {“TCP” + $_}
Now, I need to have command string stuff added in front and behind this. Again, this doesn’t matter what tech you’re working on, just put your garbledy gook that I wouldn’t understand in. $_ is the instance in the array
$commands = $devices | foreach-object {‘symconfigure -sid 1234 -cmd “unmap dev ‘ + $_ + ‘ from dir ALL:ALL;” commit’}
If I type $commands, I get:
symconfigure -sid 1234 -cmd “unmap dev 2B3B from dir ALL:ALL;” commit
symconfigure -sid 1234 -cmd “unmap dev 277F from dir ALL:ALL;” commit
symconfigure -sid 1234 -cmd “unmap dev 83D8 from dir ALL:ALL;” commit
symconfigure -sid 1234 -cmd “unmap dev 2B34 from dir ALL:ALL;” commit
symconfigure -sid 1234 -cmd “unmap dev 2250 from dir ALL:ALL;” commit
symconfigure -sid 1234 -cmd “unmap dev 21DD from dir ALL:ALL;” commit
BAM! We got our commands, and we’re rolling. If I want to save the commands as a text file…
$commands | out-file c:\dir\ourcoolscript.txt
Now I can copy/paste into putty/telnet session, or upload the script file and launch it if that’s possible, whatever I want to do.
WAY faster IMO than using other tools or duct taping a solution using Excel or other weird methods, and far more flexible.
So even if your technologies don’t have PowerShell, you can still use PowerShell!