O R G A N I C / F E R T I L I Z E R: 12.08

Dec 3, 2008

removing lines from repadmin output with powershell

i challenged a coworker to put his powershell class to good use and come up with a script that would actually have an impact, albeit little, to his day-to-day administrative work. he came up with a little script that would dump repadmin output to a text file and mail him an attachment. here’s the script:

%{repadmin /replsum * /bysrc /bydest} > logfile.txt

$filename = “logfile.txt”
$smtpServer = “mysmtp.mydomain.com”

$msg = new-object Net.Mail.MailMessage
$attachment = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “myemail@mydomain.com”
$msg.To.Add(”myemail@mydomain.com”)
$msg.Subject = “Repadmin Report ” + [System.DateTime]::Now
$msg.Body = “The daily Repadmin log file is attached”
$msg.Attachments.Add($attachment)

$smtp.Send($msg)

this is okay, but opening that attachment every time would be irritating… so i thought it might be easier just to get the output into a variable and write it to the body of the email directly. WOW. that proved to be more difficult than i thought! the idea is simple, but the formatting was killing me.

so to begin, i removed the opening line and used the following to create an array to hold the information coming in from repadmin. also, i prefer sorting by delta… so that’s why i modified that.

$myRepInfo = @(repadmin /replsum * /bysrc /bydest /sort:delta)

didn’t need the $filename variable nor the $attachment variable anymore so i dropped both of those. everything else is pretty much the same. once i reached the $msg.body statement is where i had some trouble with it. the first thing i noticed is that $myRepInfo had extra linefeeds once converted [1]. to get around that was actually pretty easy. feeding it through the where object seemed to work.

$myRepInfo | Where-Object { $_ }

now i have something a little more presentable. adding this to $msg.body couldn’t be easier, right? a simple statement like $msg.body = $myRepInfo | ? { $_ } should do fine. it doesn’t though. once i send it to $msg.body, it loses its linefeeds altogether!

Replication Summary Start Time: 2008-12-03 08:49:11 Beginning data collection for replication summary, this may ta
ke awhile:   ..................................................   ............... Source DC           largest delt
a  fails/total  %%  error  myDCServer1               59m:15s    0 / 111    0    myDCServer2                59m:15s
 0 /  38    0    myDCServer3                 59m:15s    0 /  24    0    myDCServer4                59m:15s    0 /
28    0    myDCServer5                 59m:15s    0 /  24    0    myDCServer6                59m:11s    0 /  24   

well, to get around that, we need to modify that statement once more to replace each line with the same line plus a linefeed. here’s the resulting command:

$msg.Body = $myRepInfo | Where-Object { $_ } | ForEach-Object { $_ -replace $_,"$_`n" }

that makes the output a little more favorable like this:

Replication Summary Start Time: 2008-12-03 08:49:11
Beginning data collection for replication summary, this may take awhile:
  ..................................................
  ...............
Source DC           largest delta  fails/total  %%  error
 myDCServer1                 59m:15s    0 / 111    0
 myDCServer2                 59m:15s    0 /  38    0
 myDCServer3                 59m:15s    0 /  24    0
 myDCServer4                 59m:15s    0 /  28    0
 myDCServer5                 59m:15s    0 /  24    0
 myDCServer6                 59m:11s    0 /  24    0

here’s the finished script. a little more compact… not much different. the end product, at least for me, makes it nicer. :)

$myRepInfo = @(repadmin /replsum * /bysrc /bydest /sort:delta)
$smtpServer = "mysmtp.mydomain.com"

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "myemail@mydomain.com"
$msg.To.Add("myemail@mydomain.com")
$msg.Subject = "Repadmin Report " + [System.DateTime]::Now
$msg.Body = $myRepInfo | Where-Object { $_ } | ForEach-Object { $_ -replace $_,"$_`n" }

$smtp.Send($msg)

[1] my understanding from some deep inner circles is that some utilities like repadmin or ipconfig use a printf function. if you want to see the kind of output it displays, you can push this through an octal dump command. this can be accessed from through a unix subsystem (services for unix, etc) or by using the cygwin tools – which is what i did.

this is what a typical output would look like. it’s all the “\r” formatting that junks it up.

repadmin /replsum * /bysrc /bydest /sort:delta | od -c

0000000   R   e   p   l   i   c   a   t   i   o   n       S   u   m   m
0000020   a   r   y       S   t   a   r   t       T   i   m   e   :
0000040   2   0   0   8   -   1   2   -   0   3       0   9   :   3   7
0000060   :   0   9  \r  \r  \n  \r  \r  \n   B   e   g   i   n   n   i
0000100   n   g       d   a   t   a       c   o   l   l   e   c   t   i
0000120   o   n       f   o   r       r   e   p   l   i   c   a   t   i
0000140   o   n       s   u   m   m   a   r   y   ,       t   h   i   s
0000160       m   a   y       t   a   k   e       a   w   h   i   l   e
0000200   :  \r  \r  \n           .   .   .   .   .   .   .   .   .   .
0000220   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .
*
0000260   .   .   .   .   .   .   .   .  \r  \r  \n           .   .   .
0000300   .   .   .   .   .   .   .   .   .   .   .   .  \r  \r  \n  \r
0000320  \r  \n  \r  \r  \n   S   o   u   r   c   e       D   C
0000340                                       l   a   r   g   e   s   t
0000360       d   e   l   t   a           f   a   i   l   s   /   t   o
0000400   t   a   l           %   %           e   r   r   o   r  \r  \r

Dec 2, 2008

using powershell to replace “find” or “findstr”

this is one of those things i’m blogging to remind myself instead of bugging hal rottenberg. :)

in order to find something inside a list of files, you can use find or findstr. what’s the difference between those? findstr is a bit more robust, accepting pattern matches with regex, for example. in most cases though, i’m just looking for a string inside of a list of text files. so here we go with find and the general output we can expect…

C:\temp>find /i "wscript.echo" *.*

---------- DATE2INTEGER8.VBS
' wscript.Echo CurrentDate(Now)
' WScript.Echo CurrentDate(dDateThreshold)
      WScript.Echo oRecordSet.Fields("cn") & ":" & oRecordSet.Fields("displayname") & ":" & Integer8Date(oRecordSet.Fields("pwdlastset").Value,lBias)

---------- DLNAMES.TXT

---------- DNS_DEBUG.LOG

---------- TEMP_SCRIPT.SM
 WScript.Echo
 WScript.Echo "=========================================="
 WScript.Echo "Computer: " & strComputer
 WScript.Echo "=========================================="
    WScript.Echo "ArpAlwaysSourceRoute: " & objItem.ArpAlwaysSourceRoute
    WScript.Echo "ArpUseEtherSNAP: " & objItem.ArpUseEtherSNAP

i was kind of perturbed about having to switch back and forth from cmd shell to powershell so i asked hal about it one day… and he told me to use select-string. it turns out if acts differently if you don’t pipe anything to select-string. as you can see, the output is much nicer too …

[12] » Select-String -SimpleMatch -Pattern "wscript.echo" -Path *.* | Format-Table filename, linenumber, line -autosize

Filename          LineNumber Line
--------          ---------- ----
date2integer8.vbs         11 ' wscript.Echo CurrentDate(Now)
date2integer8.vbs         12 ' WScript.Echo CurrentDate(dDateThreshold)
date2integer8.vbs         33     WScript.Echo oRecordSet.Fields("cn") & ":" &
temp_script.sm             8    WScript.Echo
temp_script.sm            10    WScript.Echo "Computer: " & strComputer
temp_script.sm            18       WScript.Echo "ArpAlwaysSourceRoute: " &
temp_script.sm            19       WScript.Echo "ArpUseEtherSNAP: " &
temp_script.sm            20       WScript.Echo "Caption: " & objItem.Caption

i purposely wrote the command verbosely for clarity. to be succinct, in this case, you’ll get the same result with:

ss "wscript.echo" *.* | ft f*, l* -auto

Dec 1, 2008

discovery data manager fails while processing a ddr

seems like a good day to blog about sms.  this happened with sms 2003.  yes, i realize there’s a configmgr.  very soon, i’ll be using it!  :)  for the rest of the unfortunate few, here’s the background:

i received this error in mom today.  this is one of those indicators that should immediately tell you that a great day is about to ensue.

mySMSServer - SMS 2003 Perf Threshold: Site Server DDR Backlog > 10,000 over 3 hours.

SMS Discovery Data Manager: Total DDRs Enqueued: value = 8700. The average over last 12 samples is 5280.83.


that’s just bad.  further investigation showed that the server failed to process ddrs since 11/28/08.  i checked around to see if anything changed, but there wasn’t anything unusual.  so … off to the status messages.  here’s what i found:

Microsoft SQL Server reported SQL message 242, severity 16: [22007][242][Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


now you just have to know that can’t be a good thing.  i presumed it was a generalized error message, checking the table for congruency against a working sms server.  everything looked fine.  off to the logs (ddm.log in particular)…

Update base table: System_DISC : execute sql update System_DISC set ItemKey = 17602, DiscArchKey = 5, User_Name0 = "myUser", User_Domain0 = "myDomain", SMS_UUID_Change_Date0 = "01/01/1601 08:01:44" where ItemKey = 17602

*** update System_DISC set ItemKey = 17602, DiscArchKey = 5, User_Name0 = "myUser", User_Domain0 = "myDomain", SMS_UUID_Change_Date0 = "01/01/1601 08:01:44" where ItemKey = 17602

*** [22007][242][Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


that looks bad, particularly the field for sms_uuid_change_date0.  i tailed this back to the .ddr it was complaining about and found the property.  i just changed 1601 to a value it could accept… like 2008.  it actually happened on two or three.  i happened to see those come in to ddm.log when i did some syntax highlighting to show them in real time.  after making the modification to the ddrs, they processed through.  all ~8000 or so…

yes, you can test this exact scenario!  knowing that it was running a datetime conversion, i ran the following query and got the following response.  looks eerily similar eh?

SELECT convert(datetime,'01/01/1601 08:01:44')

Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


there are probably plenty of reasons why discovery data manager will choke.  i know i ran into plenty of articles about it.  interesting that they weren’t tossed out as bad ddrs.  honestly, i have no idea why the date came in as that format.

using preloadpkgonsite.exe to stage compressed copies to child site distribution points

UPDATE: john marcum sent me a kind email to let me know about a problem he ran into with preloadpkgonsite.exe in the new SCCM Toolkit V2 where under certain conditions, packages will not uncompress.  if you are using the v2 toolkit, PLEASE read this blog post before proceeding.

 

here’s a scenario that came up on the mssms@lists.myitforum.com mailing list. when confronted with a situation of large packages and wan links, it’s generally best to get the data to the other location without going over the wire. in this case, 75gb. :/

the “how” you get the files there is really not the most important thing to worry about. once they’re there and moved to the appropriate location, preloadpkgonsite.exe is required to install the compressed source files. once done, a status message goes back to the parent server which should stop the upstream server from copying the package source files over the wan to the child site.

anyway, if it’s a relatively small amount of packages, you can run the execution like so:

preloadpkgonsite.exe <pkgid>

of course, if it was a small amount of packages, we wouldn’t be having this conversation right now. so what do you do when you have an enormous directory with 75gb worth of packages?

well, it would look a little something like this…

  1. on the site server, navigate to <installdir>\bin\i386. this is where the preloadpkgonsite.exe should have been dropped.
  2. run the following command:
for /f "delims=." %a in ('dir /b \\<siteserver>\sms_<sitecode>\inboxes\pkginfo.box\*.pkg') do preloadpkgonsite.exe %a

that will effectively run preloadpkgonsite.exe against every known package on that child site server. now there are caveats to making this work – one being that the .pkg has to exist on the child site server.

i’m really blogging this for sherry kissinger who wrote this excellent blog article on replacing secondary site servers. she claims she doesn’t want to dig this up in her email. i think really, she didn’t want to be bothered with having to blog this. :)

this is just a small part to an overall solution for managing sites across the wan. make sure you read the information associated with the preloadpkgonsite.exe tool to understand the caveats and dependencies.