Sunday, October 20, 2013

removing stupid "Administrator :" command prompt titlebar prefix to fix taskbar

I  constantly have several administrator cmd prompts open because whatever most people can try to do in "windows" and a mouse, it is often twice as fast from a command prompt, and then there is the added advantage that I can also scroll up and review or replay or tweak commands then combine them and create scripts all beyond the scope of anything done in a GUI... ahh, the power of scripting.

Anyways... so administrator command prompts windows in the taskbar, titled as "Administrator: ", all appear truncated in the taskbar as "Admi" which are troublesome to differentiate if two or more of those are cmd tasks.

So, I was looking for a quick way to re-patch my Windows 8 installation... and now my Windows 8.1 installation and various Windows Servers I deal with... and someone out there asked if there was such a tool so I though I'd spend a few minutes and here is another script that I can now use and take 3 seconds to update a Windows whenever I need...

This is close to OK, but it sometimes doesn't always work... a problem with permissions -- you may need to either "run as administrator" this VBS from explorer or even try steps #2 and #3 yourself, so go to "C:\Windows\System32\en-US\" and change owner and permissions of "cmd.exe.mui" from explorer then try to run this script again... But actually most times, I can just run it once directly and it will work successfully...   So it usually it works, but here is the sequence of things it does:
  1. check to see if backup exist to see if we've already patched the .mui
  2. load the original .mui file into memory
  3. patch the .mui file in memory
  4. take ownership of the .mui file
  5. allow permissions for the .mui file
  6. backup the .mui file
  7. save replace the .mui file
  8. run mcbuilder to update the system
So this is my "cmdA_patch.vbs" (click link to download) script that I have in the cloud so I can run it easily:

  Set fso = CreateObject("Scripting.FileSystemObject")
  set wso = CreateObject("wscript.Shell")
  b64 = true  '' try this if 32bit patch fails
  b64 = false '' normally this will work in 64bit windows

  oldString = "Administrator: %0" & chr(13) & chr(10) & chr(0)
  newString = "! %0" & chr(0) 

  if Wscript.arguments.count > 0 then
  if Wscript.arguments(0) = "-r" then
  newString = oldString '' revert to original
  end if
  end if

  newString = newString & right(oldString,len(oldstring)-len(newstring))
  if len(newString) <> len(oldString) then '' newString must to be same length as oldString
    wscript.echo "ABORT: replacement newString can not be longer than original"
    wscript.Quit
  end if


  ''#0 exists   cmd.exe.mui.0
  if fso.FileExists("C:\Windows\System32\en-US\cmd.exe.mui.0") then
    wscript.echo "warn: backup already exists doing update"
    sourcefile = "C:\Windows\System32\en-US\cmd.exe.mui.0"
    bOriginal = false '' doing an update sourcing backup
  else
    sourcefile = "C:\Windows\System32\en-US\cmd.exe.mui"
    bOriginal = true  '' doing first time sourcing original
  end if

  wscript.echo "#1 read.mod cmd.exe.mui"
  dim binData
  binData = loadBinaryFile(sourcefile)

  '' if 1F9EC = "Administrator: %0" cr nl 00 then replace 1F9EC = "21 00 20 00 25 00 30 00" '' 
  if instr(binData,uniString(oldString)) then
    wscript.Echo "found string, patching with: " & newString
    binData = Replace(binData, uniString(oldString), uniString(newString), 1,1,0)
  Else
    wscript.Echo "ABORT: 'Administrator:...' string not found"
    wscript.Quit
  End If

  saveBinaryFile binData , "C:\Windows\System32\en-US\cmd.exe.mui.1"

 if bOriginal then '' doing for the first time
  wscript.echo "#2 take.own cmd.exe.mui with takeown"
  wso.Run "takeown /f C:\Windows\System32\en-US\cmd.exe.mui"
if b64 then wso.Run "takeown /f C:\Windows\SysWOW64\en-US\cmd.exe.mui"
  wscript.Sleep 2000
  wscript.echo "#3 fix.perm cmd.exe.mui with icacls"
  wso.Run "icacls C:\Windows\System32\en-US\cmd.exe.mui /grant administrators:F"
if b64 then wso.Run "icacls C:\Windows\SysWOW64\en-US\cmd.exe.mui /grant administrators:F"
  wscript.Sleep 2000
  wscript.echo "#4 ren/back cmd.exe.mui as cmd.exe.mui.0"
  fso.MoveFile "C:\Windows\System32\en-US\cmd.exe.mui"  , "C:\Windows\System32\en-US\cmd.exe.mui.0"
if b64 then fso.MoveFile "C:\Windows\SysWOW64\en-US\cmd.exe.mui"  , "C:\Windows\SysWOW64\en-US\cmd.exe.mui.0"
  wscript.Sleep 2000
 end if
  wscript.echo "#5 replace  cmd.exe.mui"
  saveBinaryFile binData , "C:\Windows\System32\en-US\cmd.exe.mui"
  fso.CopyFile "C:\Windows\System32\en-US\cmd.exe.mui.1", "C:\Windows\System32\en-US\cmd.exe.mui", True
if b64 then fso.CopyFile "C:\Windows\System32\en-US\cmd.exe.mui.1", "C:\Windows\SysWOW64\en-US\cmd.exe.mui", True

  wscript.echo "#6 execute  mcbuilder"
  wso.Run "mcbuilder"
  wscript.Quit



Function uniString(astr)
  for x = 1 to len(astr)
    uniString = uniString & mid(astr,x,1) & chr(0)
  next
end function


Function loadBinaryFile(path)
  Dim fso, ts, a, i
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set ts = fso.getFile(path).OpenAsTextStream()
  a = makeArray(fso.getFile(path).size)
  i = 0
  While Not ts.atEndOfStream '' Don't use = by ts.readAll(), as not BinaryFile
      a(i) = ts.read(1)
      i = i + 1
  Wend
  ts.close
  loadBinaryFile = Join(a,"")
  wscript.echo "read "  & path & " " & fso.getFile(path).size & ", close"
End Function


Sub saveBinaryFile(bstr, path)
  Dim fso, ts
  Set fso = CreateObject("Scripting.FileSystemObject")
  ''On Error Resume Next
  Set ts = fso.createTextFile(path)
  If Err.number <> 0 Then
      wscript.echo "Error: " &  Err.message
      Exit Sub
  End If
  ''On Error GoTo 0
  ts.Write(bstr)
  ts.Close
  wscript.echo "close save " & path
End Sub


Function makeArray(n)
  makeArray = Split(Space(n)," ")
End Function
 
.x


No comments:

Post a Comment