PS Tip: PowerShell Catches

In this example I want to check if a list of users samAccountNames returns a list of matching displaynames. In the event that the user is not found I don’t want a system error displayed, I want a custom error message. This is done using a Try & Catch statement, the Try element being the test and the Catch element what I want the custom error to be.

$users = get-content C:\team-members.txt
Foreach ($user in $users) {
try {
(get-aduser $user).Name
}
catch {“$user not found in AD!” #this text will replace the system error output
}
}

Advertisements

Office/Excel Tip: Add cell value to text string, including inverted commas

I have a spreadsheet with the Active Directory current_ObjectID attribute in column H and the new_ObjectID I want to apply in column J.

The powershell script to make the change I want is this:

Rename-ADObject –Identity “<current_objectGUID>” –NewName “<new_objectGUID>”

To autopopulate the current and new ObjectID values from their respective columns and keep the inverted commas around the ObjectID values I needed to type the following:

=”Rename-ADObject –Identity “””&H3&””” -NewName “””&J3&””””

PS Tip: Is Hyper-Threading enabled on my computer

Open an elevated PS session and enter the following:

get-wmiobject -Class win32_processor | ft -Property NumberOfCores, NumberOfLogicalProcessors -auto

If Hyper-threading is enabled you’ll have twice the number of logical processors that you have of physical cores. In my case Hyper-threading is enabled due to the 4-8 ratio.

ht-enabled

 

Disclaimer: provided “AS IS” with no warranties and confer no rights

Hyper-V: Provide Internet Access for VMs with Internal/Private vSwitches

The Scenario

I have VMs configured to use either an Internal virtual switch or a Private virtual switch

The Problem

As neither an Internal or Private virtual switch are bound to a physical NIC they have no way of getting internet connectivity. This for me is a real problem as I want to connect to the internet to download updates and other files from these VMs.

The Solution

I’ve tested this solution on Hyper-V running on Windows Server 2008 R2 Enterprise, 2012 R2 Standard and Windows 10 Professional. This solution assumes the following:

  1. You have already created a standard Private and/or Internal virtual switch
  2. You have a physical NIC on the Hyper-V host that is already connected to a network that has Internet access.

Step 1: Open the services on the Hyper-V host and select the “Routing and Remote Access” service, enable it and set it to automatically run.

Check also that the “Internet Connection Sharing (ICS)” is set to automatic and is started.

Step 2: Open the Network and Sharing Center (NCS) and open the properties of the physical NIC on the Hyper-V host that is connected a network that has Internet connectivity. Select the Share tab and enable the “Allow other network users to connect through this computer’s Internet connection” option.

In the drop down menu select the virtual switch Private/Internal NIC which will use this ICS connection. In the NCS a new icon will appear called “Incoming Connections“.

Step 3: Staying in the Network and Sharing Center open the properties of the virtual switch Private/Internal NIC and ensure it has a static IP address. In my tests it was automatically assigned 192.168.137.1/24, but you can assign any address as you require – make a note of this as it is needed in Step 4.

Step 4: For the VMs to use this ICS, login to them and open their Network and Sharing Center and configure the NIC to have a static IP address in the same subnet. In my example I gave my VM the address 192.168.137.10/24. Finally set the default gateway for the VM to be the IP address of the address of the virtual switch Private/Internal NIC, in my case 192.168.137.1.

In my example I also added the google DNS server 8.8.8.8 to the DNS settings in the VM, but configure this as fits your requirements.

Step 5: Test you have Internet connectivity. In my example no further configuration was required, I had Internet access immediately

Important: As this client has got direct Internet access it is advisable to install an anti-virus solution and ensure it is fully patched.

 

Disclaimer: provided “AS IS” with no warranties and confer no rights

AVG 17.5.x firewall blocking Hyper-V VM connections

The Scenario

I have a Windows 10 (v1607) Pro instance with AVG Internet Security 17.5 installed, including the AVG Firewall component.

I have the Hyper-V feature enabled and an Internal Hyper-V vSwitch named “hv-int” which all my VMs use. This vSwitch uses the APIPA 169.254.0.0/16 range.

AVG175_version.png

The Problem

I cannot locally connect to any of my VMs via the Connect option in Hyper-V Manager, the connection just times out after several attempts. When I disable the AVG Firewall it works fine, but I don’t want to permanently disable my Firewall for obvious reasons.

The Solution

By default all local network connections, including Hyper-V vSwitches are classified as Public. By changing this to the more trusted Private option the local connections are permitted:

Step 1: Open the AVG management console

AVG175-0.png

Step 2:  Click the Internet Security option:

AVG175-1.png

 

Step 3:  Click the top-right Menu > Settings option – this lists the AVG components installed.

AVG175-2.png

Step 4: Click the left-menu Components option and in the main pane scroll down to the Firewall option. Click Customize.

AVG175-3.png

Step 5: Click the left-menu Network profiles option and in the main pane locate your Hyper-V vSwitch. Change the Profile type from Public to Private. Click OK to confirm the changes and exit out of the AVG console.

AVG175-4.png

Now try launching a local connection to the VM via the Hyper-V Manager, in my case it now worked.

 

Disclaimer: provided “AS IS” with no warranties and confer no rights

Hyper-V Compact VHD(X) Disk Task Taking Hours to Complete in GUI

The Scenario

I have a VM on my Windows Server 2012 R2 Standard with Hyper-V role. It has a dynamically expanding disk which I need to compact to reclaim disk space on the host.

The Problem

I used the Hyper-V Management console to compact the disk, but after several hours it’s still running. And because you can only compact a disk on a powered-off VM my business critical VM remains offline.

The Solution

In my case the compact process actually completed successfully within 30 minutes, it’s just the GUI had crashed even though it looked like it was still running normally (the green progress bar hadn’t quite reached the end). I found this out by look in the Event Logs:

Event Viewer > Applications and Services Logs > Microsoft > Windows > Hyper-V-VMMS > Operational log

Search for event ID 27301

“The system successfully compacted ‘:\Virtual Hard Disks\.vhdx’.”

Once I saw this I pressed cancel on the GUI compact window and restarted the Hyper-V management console. I was then able to successfully power-on my VM and the space had been successfully reclaimed.

I also noticed that the high disk activity that had been seen during the early stages of the disk compaction had completely stopped, however the above event log gave me the confidence to cancel the GUI.

NB To enable disk performance counter in Server 2012 R2 open an elevated command prompt and enter the following command “diskperf -y” then close/reopen the task manager window.

Disclaimer: provided “AS IS” with no warranties and confer no rights

WSUS Console Fails to Start: SQL server may not be running

The Scenario

I have WSUS installed on a Windows Server 2012 Standard instance. At the weekend I have installed various updates, including KB3159706.

The Problem

Once KB3159706 installed the WSUS console will not run despite both the WSUS Service and Windows Internal Database services running. It keeps reporting the error that the SQL server may not be running, which is not the case.

The Solution

The fault lies with update KB3159706, and there are two options

Solution 1 (Preferred):

Open an elevated command prompt and type

“C:\Program Files\Update Services\Tools\wsusutil.exe” postinstall /servicing

wsus1

Wait 1-2 minutes for it to complete…

wsus2

Enable HTTP Activation under .NET Framework 4.5 Features (I did this in PowerShell or use the Server Manager GUI)

Install-WindowsFeature AS-HTTP-Activation

wsus3

Finally, restart the WSUS Service

get-service WsusService | Restart-Service

The WSUS console should now launch successfully.

 

Solution 2 (less secure):

You can simply uninstall this update from the Control Panel (Control Panel > All Control Panel Items > Programs and Features > Installed Updates and search for this update, right-click and select uninstall).

 

Disclaimer: provided “AS IS” with no warranties and confer no rights