Blog

Powershell Get-ADComputer

The following Powershell commmand uses the Get-ADComputer cmdlet to query AD and return a CSV with the headers “OU”,”Name” & “OperatingSystem”.
It was designed to return this information to marry up with GPO controlled policies against the OUs so I could plan servicing schedules.

Note the “-Properties *” to ensure the OperatingSystem value is available and the “-NoTypeInformation” to drop the header row of the CSV file and leave the column headers as the first row.

Get-ADComputer -Filter * -property * -SearchBase "OU=XXX, DC=XXX, DC=XXX, DC=XXX, DC=XXX" | Select @{l='OU';e={$_.DistinguishedName.split(',')[1].split('=')[1]}},"Name", "OperatingSystem" | export-csv -Path C:\SomeDir\SomeFile.csv -NoTypeInformation

Cancelling a Scheduled chkdsk (Check Disk)

  1. Boot from the Windows 10 DVD / install media
  2. [SHIFT]+[F10] to reveal a command prompt
  3. REGEDT32 to open registry editor
  4. Highlight HKLM > File > Load Hive
  5. Navigate to the local system disk :\Windows\System32\config and open [SYSTEM]
  6. Navigate to HKEY_LOCAL_MACHINE\<LOADED HIVE NAME>\CurrentControlSet\Control\Session Manager\ and edit the BootExecute MULTI-SZ value to read only: autocheck autochk *
  7. Reboot
  8. tadaa!

WIndows Command For-loop

for /f "delims=*" %f in ('dir "C:\folder\of\files\*.efs" /b') DO C:\folder\of\executable\program.exe -f "C:%~pf%f"
  1. A standard debugging technique is to insert the echo command into scripts and even compound/complex commands.  If you do
    for /f "delims=*" %a in ('dir *.avi /b /s') do @echo md "%~na"
    

    you’ll get the output

    "file 1"
    "file 2"
    "file 3"
    "file 4"
    

    Notes:

    • The @ prevents the echo commands themselves from displaying, so you see only their output.
    • "delims=…" tells for how to parse the lines of output from the dir *.avi /b /s command.  I don’t know why the answer you linked to suggests "delims=*".  But the default behavior is to break lines apart at spaces, so, if your directory and/or file names contain spaces (as you indicated), you should use "delims=" (specifying that there are no delimiters) to get this to work.
  2. If you type for /? or help for, you’ll get documentation on the for command.  Down in the fifth page, you’ll see
    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:
    
        %~I         - expands %I removing any surrounding quotes (")
                         ︙ 
        %~pI        - expands %I to a path only
        %~nI        - expands %I to a file name only
                         ︙ 
    
    The modifiers can be combined to get compound results …
                             ︙ 
    

    which explains why %~na is getting you just the file name of the *.avi files whose full names are in %a.  Now try

    for /f "delims=" %a in ('dir *.avi /b /s') do @echo md "%~pa"
    

    and you’ll get

    "the_current_directory\Folder A\"
    "the_current_directory\Folder A\"
    "the_current_directory\Folder B\"
    "the_current_directory\Folder B\"

    From which we can conclude that you want to do

    for /f "delims=" %a in ('dir *.avi /b /s') do md "%~pa%~na"
    

    to create the file 1 and file 2 directories under Folder A, and the file 3 and file 4 directories under Folder B.   And, as @dave_thompson_085 points out, you can combine %~pa%~na into %~pna.

https://superuser.com/questions/1033360/how-do-i-execute-commands-on-files-in-multiple-folders

Powershell Query – get-ADObject filter for Bitlocker

The following command returns all objects in the specified OU (replace XXX with your own values) which have Bitlocker recovery information and what the recovery key is.

Get-ADObject -Filter "objectClass -eq 'msFVE-RecoveryInformation'" -SearchBase "OU=XXX, OU=XXX, OU=XXX, DC=XXX, DC=XXX, DC=XXX, DC=XXX" -Properties msFVE-RecoveryPassword,whenCreated

I’m only really interested in the machine name for review so the following command both chops the Distinguished name field and returns just the second part and then chops that part removing the “CN=” section whilst also returning just that column name from the above query.

Get-ADObject -Filter "objectClass -eq 'msFVE-RecoveryInformation'" -SearchBase "OU=XXX, OU=XXX, OU=XXX, DC=XXX, DC=XXX, DC=XXX, DC=XXX" | Select @{l='ComputerName';e={$_.DistinguishedName.split(',')[1].split('=')[1]}}

The tricky part was the select statement, here’s a brief breakdown of what it’s doing.

@{} defines an array to be returned, which we’ll expect although it could end up being an array of 1 or 0 with no results.

“l” is shorthand for “label” which is the column header.
“e” is shorthand for “expression”.
“$_.” indicates a single item in the pipeline, the results of Get-ADObject are then piped and this catches them one at a time.
“DistinguishedName.split(‘,’)[1].split(‘=’)[1]” takes the value of the name DistinguishedName being piped and splits it first by “,”, selecting the second item in the array that forms “[1]” is second when counting from 0. It then splits that string again on the “=” sign, returning the second part (the actual computer name alone!).

Phew!

 

Learned a little more, powershell syntax understanding on the up!

VMWare Workstation 14 Ubuntu

I recently encountered a  minor issue attempting to create additional VMs in VMWare Workstation 14 on Ubuntu. The error I received suggested there wasn’t enough memory free to power on an additional VM but the host has 32GB of RAM and I’d allocated 20GB across all VMs.

Inspecting the /etc/vmware/config file revealed the value:

prefvmx.allVMMemoryLimit = "12954"

Which is a hard upper limit for all VMs, adjusting it to the following with VMWare closed and then start VMWare resolved the problem:

prefvmx.allVMMemoryLimit = "20480"

 

eSXi update storage drivers

1. Power down all running VMs on host (or migrate to another eSXi host if using vCenter)

2. Enable SSH on the eSXi host

3. SSH to the host

4. Enter maintenance mode via:

esxcli system maintenanceMode set --enable true

5. Uninstall the previous storage driver via:

esxcli software vib remove -n scsi-hpvsa -f

6. Reboot the eSXi host via:

esxcli system shutdown reboot --reason "your reason here" 

7. When the eSXi host is back SSH back in and ensure Maintenance Mode is enabled through the same command in step 4.

8. SCP / SFTP copy the new storage driver to /tmp/ on the eSXi host

9. Install the new storage drive via:

esxcli software vib install -v file:/tmp/name-of-driver-file-here.vib --force --no-sig-check --maintenance-mode

10. Reboot

11. Disable SSH

12. Exit Maintenance Mode

13. Power on / migrate back VMs

Powershell : Query for user’s last logon date

I needed to work out some AD accounts’ last logon dates to make a further assessment, in powershell I found this was fairly simple:

To get a list of all user attributes available for query:

$> get-aduser -identity <USERNAME_HERE> -Properties *

To query for last logon date:

$> get-aduser -identity <USERNAME_HERE> -Properties LastLogonDate

 

Ubuntu, Kernel 4.14+, VMWare Workstation 14

I was unable to compile the ubuntu kernel patches for VMWare Workstation 14.0 for a couple of reasons on Ubuntu 16.04 with an updated kernel v4.14:

 

1. Launching VMWare Workstation resulted in a GUI window complaining no GCC-7.2 was available, solved by the following commands (thanks to https://askubuntu.com/questions/859256/how-to-install-gcc-7-or-clang-4-0) :

sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get update
sudo apt-get install gcc-7 g++-7
 

2. The next failure was with further kernel compiling modules with the vmmonitor service failing. This needed a patch on the install scripts to support the latest kernel, resolved by the following commands (thanks to https://github.com/mkubecek/vmware-host-modules/commit/770c7ffe611520ac96490d235399554c64e87d9f for the patch and https://superuser.com/questions/1255099/vmware-workstation-not-enough-physical-memory-since-last-update/1255963 for guidance on applying it):


~$ sudo cd /tmp
~$  cp /usr/lib/vmware/modules/source/vmmon.tar .
~$  tar xf vmmon.tar
~$  rm vmmon.tar
~$  wget https://raw.gi 
thubusercontent.com/mkubecek/vmware-host-modules/fadedd9c8a4dd23f74da2b448572df95666dfe12/vmmon-only/linux/hostif.c
~$  mv -f hostif.c vmmon-only/linux/hostif.c
~$  tar cf vmmon.tar vmmon-only
~$  rm -fr vmmon-only
~$  mv -f vmmon.tar /usr/lib/vmware/modules/source/vmmon.tar
~$  vmware-modconfig --console --install-all

Ubuntu, LVM, Partitions

Ubuntu disk partition extension under LVM:

1/ extend the LVM volume (cheated with gparted but parted would work fine, this is the container partition for the “Volume Group”.

 

2/ Next launch LVM and use “lvdisplay” to print the current output, mine was a container group with a single logical volume named “root”

 

3/ Now I know the location and name of the LV I can issue the following command to expand it into the available free space created in step 1:

lvextend -l +100%FREE /dev/Container1/root

 

4/ Finally exit lvm and expand the file system to fill the LV:

sudo resize2fs /dev/Container1/root

Ubuntu

Issuing “apt-get update” or “apt-get upgrade” commands resulted in the following error:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

I couldn’t locate any running processes which would be locking these files so went ahead and removed the lock files via:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

And then repeated the commands successfully.