Tag Archives: windows

Powershell: Some basic XML handling with Powershell and .NET

Hi everybody,

here are some fundamentals to handle XML Files with powershell.

The following commands uses these XML File


<config description="Config file for testing">
    <system description="Document Management">
        <document authorFirstName="Kirk" authorSurname="Author1" date="20130503 01:15:43" description="Document Hammet" index="3">C:\temp\doc0001.txt</document>
        <document authorFirstName="Lars" authorSurname="Author5" date="20130503 10:05:42" description="Document Ulrich" index="4">C:\temp\doc0002.txt</document>
        <document authorFirstName="Cliff" authorSurname="Author5" date="20130612 11:54:33" description="Document Burton"  index="6">C:\temp\doc0051.txt</document>
        <document authorFirstName="Jason" authorSurname="Author2" date="20130806 04:02:41" description="Document Newsted"  index="1">C:\temp\doc0041.txt</document>
        <document authorFirstName="Robert" authorSurname="Smith" date="20131202 07:12:03" description="Document Trujillo"  index="2">C:\temp\doc0012.txt</document>
        <document authorFirstName="James" authorSurname="Smith" date="20130211 09:05:59" description="Document Hetfield"  index="5">C:\temp\doc0003.txt</document>
    </system>
</config>

Creating  a XML File, create a document root, create subelements, add and set attributes, save the file

# Create a new XML File with config root node
[System.XML.XMLDocument]$oXMLDocument=New-Object System.XML.XMLDocument
# New Node
[System.XML.XMLElement]$oXMLRoot=$oXMLDocument.CreateElement("config")
# Append as child to an existing node
$oXMLDocument.appendChild($oXMLRoot)
# Add a Attribute
$oXMLRoot.SetAttribute("description","Config file for testing")
[System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("system"))
$oXMLSystem.SetAttribute("description","Document Management")
# Save File
$oXMLDocument.Save("c:\temp\config.xml")

Loading an existing XML File, read an element, do some XPATH queries

Continue reading Powershell: Some basic XML handling with Powershell and .NET

Windows: Create a new local User Account and add them to a local group by using .NET and C#

Hi,

a short post. It describes how to create a new local Windows User account and add them to an existing local user group.

First of all, include the following references in your project:


using System.DirectoryServices;
using System.Runtime.InteropServices;

Create a new User with local administrator rights.

// define Username and Password
const string USER_NAME = "NewAdmin";
char[] aPWchars = { 'P', 'a', 's', 's' , 'w', 'o', 'r', 'd'};
System.Security.SecureString oPW = new System.Security.SecureString();
foreach (char cChr in aPWchars) {
    oPW.AppendChar(cChr);
}
// Get Computerobject via ADSI
DirectoryEntry oComputer = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
// New User
DirectoryEntry oNewUser = oComputer.Children.Add(USER_NAME, "user");
// define Pointer to a string
IntPtr pString = IntPtr.Zero;
// Pointer to password
pString = Marshal.SecureStringToGlobalAllocUnicode(oPW);
// Set password
oNewUser.Invoke("SetPassword", new object[] { Marshal.PtrToStringUni(pString) });
// Add a description
oNewUser.Invoke("Put", new object[] { "Description", "New Administrator" });
// Save changes
oNewUser.CommitChanges();
// Cleanup and free Password pointer
Marshal.ZeroFreeGlobalAllocUnicode(pString);
// Get Group
DirectoryEntry oGroup = oComputer.Children.Find("Administrators", "group");
// And add the recently created user
oGroup.Invoke("Add", new object[] { oNewUser.Path.ToString() });

Michael

Windows Update and Windows Search do not work

Hi,

a good friend of mine came to me last week because some services of Windows Vista on his notebook did not run as expect. In detail:

  • Windows Search service starts but terminates
    • EventID 7034: The Windows Search service terminated unexpectedly.  It has done this 4 time(s).

      Windows search terminates after start
      Windows search terminates after start
    • EventID 7024: The Windows Search service terminated with service-specific error 2147749155 (0x80040D23)
    • EventID 1006: The Windows Search Service has failed to create the new search index. Internal error <4, 0x8004117f
    • EventID 9000: The Windows Search Service cannot open the Jet property store.
  • Windows Update service starts, but do not work, some errors are logged in C:\Windows\WindowsUpdate.log
    • 45c    DtaStor    FATAL: Failed to initialize datastore, error = 0xC8000247
    • 45c    AU    FATAL: Failed to get session from datastore, error = 0xC8000247
    • 45c    AU    FATAL: Failed to Unserialize from data store, error = 0xC8000247
  • No Windows Update File could installed by double clicking a msu File.
  • Cryptocraphic service do not run as expected:
    • Signature for Windows builtin programs cannot be verified, for example mmc.exe. Error Message:

      Do you want the following program to allow changes to this computer?

      UAC Message: builtin executables could not verified.
      UAC Message: builtin executables could not verified.
    • Cause: The folder c:\windows\system32\catroot2 is empty or/and some errors are logged in file C:\Windows\System32\catroot2\dberr.txt:
      CatalogDB: 21:39:37 28.02.2013: JetInit Corruption
      CatalogDB: 21:39:37 28.02.2013: catdbsvc.cpp at line #747 encountered JET error -583
      CatalogDB: 21:39:37 28.02.2013: catdbsvc.cpp at line #961 encountered JET error -583
      CatalogDB: 21:39:37 28.02.2013: catdbsvc.cpp at line #6636 encountered JET error -583
    • EventID 257: The following information was included with the event: -583 the message resource is present but the message is not found in the string/message table

It seems that all services which uses the Window Jet Database engine are affected.

Solution

Continue reading Windows Update and Windows Search do not work

Windows: Disable or enable assigning of a drive letter when plugin a removeable device

Hi,

the default setting in Windows Vista, 7 or 8 is to assign a drive letter when you plugin a removable device. But sometimes you want to prevent Windows from doing that, for example security reasons.

You can control this behaviour with the mountvol command.  The following command line disable the automatical assignment.


mountvol /N

to enable the auto assignment again execute

mountvol /E

Michael