Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Monday, 3 May 2021

Write XML Files with Powershell - The Easy Way

 I needed to create a custom XML file using powershell as part of a script I was developing. I found it difficult to understand and use the in-built XML functionality with powershell so I attempted to simply use write-output commands to write the XML file. It worked - mostly, but the application I was submitting the XML file wasn't recognizing it. As it turns out there's some hidden properties within a properly formed XML file which requires a little trick to get right when forming it this way - so here's how I did it..

            $xmlfile = "C:\temp\test.xml"
            write-output '<?xml version="1.0"?>' | out-file "$xmlfile"
            write-output '<test>' | out-file "$xmlfile" -append
            write-output "  <version>1.0</version>" | out-file "$xmlfile" -append
            write-output '</test>' | out-file "$xmlfile" -append

            $xml = [xml](Get-Content $xmlfile)
            $xml.Save($xmlfile)

The first part of the script is fairly straightforward - declare the path to your XML file in the $xmlfile variable.

Then, use write-output commands to enter the text data into your XML file - make sure you include the -append argument after the first line to ensure the lines are appended onto the end of the file - you can open the file in a text editor such as NotePad++ to make sure it appears as you expect. Make sure you close off all your tags so that formatting is valid.

The last 2 lines is the trick I previously mentioned - use the [xml] class with get-content to import the XML file into the $xml variable. Once the data is in the $xml variable, it is then re-saved - these 2 lines are what puts the hidden formatting into the XML file to make it valid.


Thursday, 13 October 2016

Automatically logoff sessions from windows server using Powershell

Windows server has the functionality through group policy to automatically log off users at a certain time of day, or once their set logon hours expire etc, but in my experience (and many other users based on internet research), it doesn't work very reliably.

Here is a script you  can use that will get a list of all current user sessions on a server, and log them off. A "safelist" is also included where you can specify usernames that should not be logged off automatically by this script - ie. administrator accounts etc. Usernames should be specified in inverted commas and separated by single commas.

I've also incorporated logging functionality, as it may be useful to know what users are staying logged onto the server (perhaps when they shouldn't be), and to be 100% certain about what the script is doing, or has done. Adjust the $logfile variable as required, or ensure the default folder (C:\Admin) exists for it to work correctly.

This does not require any additional modules to be installed either.

The script works by using the query session command, and then manipulating/formatting the results to obtain a list of current user sessions. Because the query session command is a DOS based command, the results aren't formatted nicely into variables/members that powershell can easily understand and work with, so formatting/manipulation is done using the .Substring and .Trim functions. The list of user sessions t hat is obtained is then compared against the safelist and if the user is not present in the safelist, is then logged off the server.

You will need to setup a scheduled task to run this powershell script - you can view my blog post here on setting up Powershell scripts to run via scheduled tasks in windows

$safelist = "administrator", "user1"
$date = get-date -f "ddMMyyyy"
$logfile = "C:\Admin\LogOffScript-$date.txt"

$sessions = query session |  where-object { $_ -notmatch '^ SESSIONNAME' } | %{
    $item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
    $item.Active = $_.Substring(0,1) -match '>'
    $item.SessionName = $_.Substring(1,18).Trim()
    $item.Username = $_.Substring(19,20).Trim()
    $item.Id = $_.Substring(39,9).Trim()
    $item.State = $_.Substring(48,8).Trim()
    $item.Type = $_.Substring(56,12).Trim()
    $item.Device = $_.Substring(68).Trim()
    $item


foreach ($session in $sessions)
{
if ($safelist -notcontains $($session.username))
{
$time = get-date
logoff $($session.id)
write-output "$time | Logged off $($session.username)" | out-file $logfile -append
}
}

Wednesday, 12 October 2016

Windows - Unable to remove printer driver - The specified printer driver is currently in use

Sometimes you may need to uninstall a printer driver from a computer (because of corruption, to re-install etc), and may get the below error message;

"Unable to remove printername. The specified printer driver is currently in use"



This may even occur AFTER you have removed the printer itself from your list in Devices & Printers.

Here are some steps I found that allowed me to remove the printer and printer driver without having to restart the computer;


  1. Open Devices and Printers
  2. Right click the device you need to remove and select Remove Device
  3. Open Services.msc and locate the Print Spooler service
  4. Right click the Print Spooler service and select Stop
  5. Open regedit
  6. Browse to the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\
  7. Depending on whether you are running a 32 or 64 bit windows, expand the key for Windows NT x86 (if you're running 32 bit windows), or Windows x64 (if you are running 64 bit windows)
  8. Expand the Print Processors key
  9. Rename any entries under Print Processors to have .old on the end. In the example below, there is one entry, winprint which I renamed to winprint.old

  10. Go back to services.msc and start the Print Spooler service
  11. Open the Print Server Properties and try to remove the driver pack - it should now remove successfully
  12. Once the driver pack is removed, stop the Print Spooler service again
  13. Go back to regedit and rename the key(s) you renamed to have .old on the end back to their original name(s) - as per my example, winprint.old will be renamed back to winprint
  14. Start the Print Spooler service from services.msc
  15. Re-install printer & drivers as required




Tuesday, 11 October 2016

Removing old Windows Updates - Windows Installer Directory

Windows updates can tend to take up a large number of space after some time on the system drive (C:\), and are not easily removed.

Updates are typically installed into the C:\Windows\Installer directory and hold the .msi and .msp files used to install (or uninstall) windows updates.

A company called "Homedev" have developed a product called "Patch Cleaner" that is clever enough to search this windows installer directory and detect which patches can safely be removed. Over time windows updates tend to replace, outdate or supersede each other - rendering them useless and taking up precious hard disk space on your hard disk!

How does it work? Well, as explained by Homedev, their application queries the operating system for a list of all the currently installed patches and updates. It then compares this list returned by the operating system against all the files in the C:\Windows\Installer directory. Anything that's found in the folder but not in the list provided by the operating system is flagged as able to be removed by the application.

The application also has the ability to relocate the files to another location first (such as another drive, like an external USB hdd) which reduces the risk involved in removing some of these files. If it turns out the files are required they can simply be copied back to the C:\Windows\Installer directory.

The latest version of the application can be downloaded and installed from the below website;

http://www.homedev.com.au/free/patchcleaner


  1. Once you have downloaded the file, double click it to run/execute
  2. Click Next to begin the installation process

  3. Select I Agree on the license agreement then select Next

  4. It is recommended to leave the default installation path and set to Everyone to be able to access the application. Click Next

  5. Click Next to begin the installation

  6. Click Close to exit the wizard once installation has completed

  7. You can now run PatchCleaner from the shortcut placed on the desktop or from within the Start Menu

  8. Upon startup, the application will automatically scan for files that can be removed

  9. Once the scan is finished, you will be presented with a window like the one below. The screen details how many files are orphaned and can be removed (in this example, there are 32 files, totalling 2.17GB in size). You can click either the Delete button to permanently delete the files, or the Move button the move all these files to another location (as mentioned previously)

  10. After deleting or moving the files, the application will run another scan and present a window again like the one above.




Friday, 7 October 2016

iOS 10 - How to undelete/restore/recover standard iPhone apps after deleting them

When Apple introduced iOS 10 for iPads and iPhones, they introduced a new feature that allows you to uninstall the standard (apple) apps that come pre-installed on the phone - eg. stocks, mail, newsstand etc.

The process to remove them is simple - just like removing any other app on your device, you press and hold on any of the icons on the home screen for a few seconds until they all start moving slightly, and have a little "x" in the top right corner above each icon. You then click the small "x" to remove the app



What isn't entirely clear or obvious at first is the process to get these apps back after you've removed them. Turns out the answer is very simple, you just need to download them again from the App Store!

For example, if you open the App Store and search for the "mail" app, the first result that appears is the default Apple Mail app. Click the download or "cloud" icon next to it to download it again, and away you go!




Optus - Sagemcom F@ST 3864 modem/router - admin username and password

The Sagemcom F@ST 3864 modem/router that is provided by Optus has an administrative interface that you can login to to access advanced features that aren't accessible via the the "standard" interface.

The standard interface is accessed via http://192.168.0.1/main.html?loginuser=1 and allows access to basic settings such as wifi options without needing to enter a username/password

However, if you change the "loginuser=" value from a "1" to a "0" you get prompted for a username/password. Once you've entered the correct username/password you can then access more advanced features within the modem setup/config.

There are a number of forum posts stating some of the default usernames/passwords for this modem, but none of them worked for me, even after a factory reset. What I did stumble across though was how to definitively (and easily) retrieve the admin password for this modem;


  1. Open a web browser and go to http://192.168.0.1/password.html
    You will be presented with a screen like the one pictured below;

  2. Right click in a blank area of the page, and select the option to "View source" or "View page source" (the wording might be slightly different depending on what browser you are using)

  3. This will load up all the html code used on this page. If you look towards the top of the page, you will see a line starting with "pwdAdmin = " - what is after this and contained within single quotes is the admin password that is set on the router



  4. You can then go to the URL http://192.168.0.1/main.html?loginuser=0 and enter the username "admin" and the password from Step 3 above and you will be able to access the hidden/advanced menu options within the router