In order to get the output down to one line, we use a tool called sed. This tool can:
Example 1: what if I wanted to know the active code page on a machine. To do this from a command prompt I would typechcp and get the output something like
Active Code Page: 437
Example 2: #1 is already one line so that's easy. But what if I wanted to get the output of a particular line of netstat output. For example, I want to know the status of the local port 5900 (VNC). Thenetstat -nao output looks like this:
>netstat -nao Active Connections Proto Local Address Foreign Address State PID TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1456 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 1352 TCP 0.0.0.0:5800 0.0.0.0:0 LISTENING 1088 TCP 0.0.0.0:5900 0.0.0.0:0 LISTENING 1088 TCP 0.0.0.0:52230 0.0.0.0:0 LISTENING 552 TCP 127.0.0.1:1028 0.0.0.0:0 LISTENING 1696 TCP 127.0.0.1:1063 127.0.0.1:62514 ESTABLISHED 2404 TCP 127.0.0.1:62514 0.0.0.0:0 LISTENING 504 TCP 127.0.0.1:62514 127.0.0.1:1063 ESTABLISHED 504 TCP 192.168.1.111:139 0.0.0.0:0 LISTENING 4 TCP 192.168.1.111:3698 172.18.0.122:52230 SYN_SENT 552 UDP 0.0.0.0:445 *:* 4 UDP 0.0.0.0:500 *:* 1196 UDP 0.0.0.0:4500 *:* 1196 UDP 127.0.0.1:123 *:* 1664 UDP 127.0.0.1:1900 *:* 1988 UDP 127.0.0.1:3482 *:* 9308 UDP 127.0.0.1:62514 *:* 504 UDP 192.168.1.111:123 *:* 1664 UDP 192.168.1.111:137 *:* 4 UDP 192.168.1.111:138 *:* 4 UDP 192.168.1.111:1900 *:* 1988
Using Sed.exe I can get this to one line like this:netstat -nao | sed -n "/[ ]*TCP.*:5900/p". The output now reads:
>netstat -nao | sed -n "/[ ]*TCP.*:5900/p" TCP 0.0.0.0:5900 0.0.0.0:0 LISTENING 1088
Example 1: Let us say we only want the code page number. Sed can use pattern matching to replace string patterns with other patterns. To get it down to one value we can tell set to remove the stringActive code page: with nothing so all that is left is the number. Like this:
>chcp | sed -n "s/Active code page:[ ]*//p" 437
Example 2: Let us say we only want the port status of "LISTENING" or whatever it might be. Similar to #1 except we need to do a pattern replace before and after the value we are interested in so we can call sed twice like this
>netstat -nao | sed -n "s/[ ]*TCP.*:5900.*:[0-9]*[ ]*//p" | sed -n "s/[ ]*[0-9]*[0-9]//p" LISTENING
Note that if the results of netstat -nao were stored in a file (e.g netstatoutput.txt) then the command line would be:
sed -n "s/[ ]*TCP.*:5900.*:[0-9]*[ ]*//p" netstatoutput.txt | sed -n "s/[ ]*[0-9]*[0-9]//p"
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
strValueName = WScript.Arguments.Item(0)
Set StdIn = WScript.StdIn
strValue = StdIn.ReadLine
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\KACE"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValueThe way this script works is it takes the first line of standard output from one command and puts it into a reg value that you specify. Imagine a text file called hello.txt that contained the textHello KACE!. To write this to the registry value of HKLM\Software\KACE\Greeting we would use it like this:
type hello.txt | cscript.exe set_reg_value.vbs "Greeting"
To use it to store a command line output you would have a command line as above examples and modify it to use this script. Extending Example #1 like this:
>chcp | sed -n "s/Active code page:[ ]*//p" | cscript set_reg_value.vbs "Active Code Page"
du -q "c:\program files\kace" | sed -n "s/Size:[ ]*//p" | sed -n "s/[ ]*bytes/bytes/p" |cscript.exe set_reg_value.vbs AgentSize