Get Uptime with Powershell

Use the following script to determine the uptime of your servers which are defined in a text file:

 

# Writer = Ritesh Parab; fb.com\parab.ritesh  
# File Name = Get-uptime 1.2
# Description = Get Server up time 
# function Date and time copied from Technet :)
# Error Handling : TRY and CATCH
# Bugs are welcome 2 [email protected]
# Fix > WMI Query time function added {from technet forum}


cls                                             
$erroractionpreference = "SilentlyContinue"
# Helper Function - convert WMI date to TimeDate object
function WMIDateStringToDate($Bootup) {
 [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
}

# Main script
$Computer = Get-Content "c:\Servers.txt"

foreach ($system in $Computer) {
$rtn = Test-Connection -ComputerName $system -Count 1 -BufferSize 16 -Quiet
	 	IF($rtn -match 'True') {
		$NameSpace = "Root\CIMV2"
		$wmi = [WMISearcher]""
	 	$wmi.options.timeout = '0:0:15' #set timeout to 10 seconds
	 	$query = 'Select * from Win32_OperatingSystem'
	 	$wmi.scope.path = "\\$system\$NameSpace"
	 	$wmi.query = $query
	  	Try{
	  	$wmiresult = $wmi.Get()
	  	#	$wmiresult
			foreach ($wmioutput in $wmiresult){
			   $Bootup = $wmioutput.LastBootUpTime
			   $LastBootUpTime = WMIDateStringToDate($Bootup)
			   $now = Get-Date
			   $Uptime = $now - $lastBootUpTime
			   $d = $Uptime.Days
			   $h = $Uptime.Hours
			   $m = $uptime.Minutes
			   $ms= $uptime.Milliseconds
			   $a = "$System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
			   Write-Host "$a" -ForegroundColor Green
	  		   }
	  		}
		Catch {
	  			Write-host "WMI not available on : $System" -ForegroundColor Red
				Write-Output "WMI failed on $system because : $_" | Out-File "c:\failed.txt" -Force -Append 
	  	}
		}
		else{
			write-host "$system Offline" -ForegroundColor DarkRed
			Write-Output "$system Offline" | Out-File 'c:\failed.txt' -Force -Append 
		}
	} 

 

Windows Server 2012: Deactivating UAC

source: http://social.technet.microsoft.com/wiki/contents/articles/13953.windows-server-2012-deactivating-uac.aspx

Deactivating UAC in Windows 2008 R2 was possible via running msconfig (for example winkey+r -> msconfig), going to the Tools tab and launching “Change UAC settings”. There, you select “Never notify” to disable UAC. The same approach is still available in Windows Server 2012, though UAC is still active after you selected “Never notify”. You have the option to turn off UAC via registry by changing the DWORD “EnableLUA” from 1 to 0 in “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system”. You will get a notification that a reboot is required. After the reboot, UAC is disabled.

How to enable Remote Desktop remotely using Powershell

source: http://networkerslog.blogspot.nl/2013/09/how-to-enable-remote-desktop-remotely.html

In Windows Server 2012, remote management is enabled by default but not Remote Desktop. To enable RDP on the server, add the target server to the Server Manager and run remote Powershell console.

On the remote Powershell console, enable remote desktop and firewall using the following cmdlets:
1) Enable Remote Desktop
set-ItemProperty -Path ‘HKLM:\System\CurrentControlSet\Control\Terminal Server’-name “fDenyTSConnections” -Value 0

2) Allow incoming RDP on firewall
Enable-NetFirewallRule -DisplayGroup “Remote Desktop”

3) Enable secure RDP authentication
set-ItemProperty -Path ‘HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp’ -name “UserAuthentication” -Value 1

Convert Disk to VDH

http://technet.microsoft.com/en-us/sysinternals/ee656415.aspx

download: http://download.sysinternals.com/files/Disk2vhd.zip

 

Introduction

Disk2vhd is a utility that creates VHD (Virtual Hard Disk – Microsoft’s Virtual Machine disk format) versions of physical disks for use in Microsoft Virtual PC or Microsoft Hyper-V virtual machines (VMs). The difference between Disk2vhd and other physical-to-virtual tools is that you can run Disk2vhd on a system that’s online. Disk2vhd uses Windows’ Volume Snapshot capability, introduced in Windows XP, to create consistent point-in-time snapshots of the volumes you want to include in a conversion. You can even have Disk2vhd create the VHDs on local volumes, even ones being converted (though performance is better when the VHD is on a disk different than ones being converted).

It will create one VHD for each disk on which selected volumes reside. It preserves the partitioning information of the disk, but only copies the data contents for volumes on the disk that are selected. This enables you to capture just system volumes and exclude data volumes, for example.

Note: Virtual PC supports a maximum virtual disk size of 127GB. If you create a VHD from a larger disk it will not be accessible from a Virtual PC VM.

To use VHDs produced by Disk2vhd, create a VM with the desired characteristics and add the VHDs to the VM’s configuration as IDE disks. On first boot, a VM booting a captured copy of Windows will detect the VM’s hardware and automatically install drivers, if present in the image. If the required drivers are not present, install them via the Virtual PC or Hyper-V integration components. You can also attach to VHDs using the Windows 7 or Windows Server 2008 R2 Disk Management or Diskpart utilities.

Command Line Usage

Disk2vhd includes command-line options that enable you to script the creation of VHDs. Specify the volumes you want included in a snapshot by drive letter (e.g. c:) or use “*” to include all volumes.

Usage: disk2vhd <[drive: [drive:]…]|[*]> <vhdfile>
Example: disk2vhd * c:\vhd\snapshot.vhd

Why I can’t convert my Windows Server 2012 R2 Core to GUI

Error:

Install-WindowsFeature : The request to add or remove features on the specified server failed.
Installation of one or more roles, role services, or features failed.

The source files could not be downloaded.

Use the “source” option to specify the location of the files that are required to restore the feature. For more information on specifying a source location,see http://go.microsoft.com/fwlink/?LinkId=243077. Error: 0x800f0906

Coretech Blog » Blog Archive » Why I can’t convert my Windows Server 2012 R2 Core to GUI
Here are the commands to update the source files with PowerShell

# Create folders
New-Item -Path C:\WS2012R2 -ItemType Directory
New-Item -Path C:\TEMP -ItemType Directory

# Copy the install.wim to temporary folder
Copy-Item H:\sources\install.wim C:\TEMP

# Remove the read-only attribute from the copied file
Set-ItemProperty -Path C:\TEMP\install.wim -Name IsReadOnly -Value $False

# Get the Windows Image Index
Get-WindowsImage -ImagePath C:\TEMP\install.wim

# Mount the Image
Mount-WindowsImage -Path C:\WS2012R2 -ImagePath C:\TEMP\install.wim -Index 2

# Add the Updates to Windows Image
$Updates = Get-ChildItem C:\Users\kaj\Desktop\Updates
foreach($item in $Updates){
Add-WindowsPackage -PackagePath $item.FullName -Path “C:\WS2012R2”
}

# Commit the changes
Dismount-WindowsImage -Path C:\WS2012R2 -Save -Verbose

source: http://blog.coretech.dk/kaj/why-i-cant-convert-my-windows-server-2012-r2-core-to-gui/

Mount and Modify a Windows Image Using DISM

source: http://technet.microsoft.com/en-us/library/hh824814.aspx

Applies To: Windows 8, Windows 8.1, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2

Mounting an Image


You can mount an image using the /optimize option to reduce initial mount time. However, When using the /optimize option, processes that are ordinarily performed during a mount will instead be completed the first time that you access a directory. As a result, there may be an increase in the time that is required to access a directory for the first time after mounting an image using the /optimize option.

To mount an image


  1. Open a command prompt with administrator privileges. If you are using a version of Windows other than Windows 8, use the Deployment Tools Cmd Prompt installed with the ADK or navigate to the DISM directory on your local computer.
  2. Mount the image.
    Copy
    Dism /Mount-Image /ImageFile:C:\test\images\myimage.wim /index:1 /MountDir:C:\test\offline
    
    noteNote
    To mount a Windows image from a VHD file, you must specify /index:1

     

    You can also add options to mount the image with read-only permissions or to reduce the initial mount time with the /Optimize option. For example,

    Copy
    Dism /Mount-Image /ImageFile:C:\test\images\myimage.wim /index:1 /MountDir:C:\test\offline /ReadOnly /Optimize
    

    For more information about the options available for the /Mount-Image option in DISM, see DISM Image Management Command-Line Options.

 

Modifying an Image


After you mount an image, you can browse the directory of the image. You can review the file and folder structure, and add, edit, or delete files and folders.

You can also use the DISM tool to add and remove drivers and packages, including language packs, enumerate drivers and packages, modify configuration settings, and more. For more information, see Service a Windows Image Using DISM.

To view and modify an image


  1. On your technician computer open the mounted directory. For example,
    Copy
    cd C:\mounted_images
    
  2. Delete, edit, or add additional files and folders to the location where they must appear after they have been applied to the destination computer. For example, C:\program_files\application_name.
    ImportantImportant
    If you must add an application or a device, verify that you included all of the required files. Although you can add application files and folders, you cannot install applications. 

     

Committing Changes to an Image


You can commit changes to an image without unmounting the image.

To commit changes to an image


  1. At the command prompt, type:
    Copy
    Dism /Commit-Image /MountDir:C:\test\offline
    

    Use /CheckIntegrity to detect and track .wim file corruption when you commit changes to the image. When you apply or mount the image, use /CheckIntegrity again to stop the operation if file corruption was detected. /CheckIntegrity cannot be used with virtual hard disk (VHD) files.

Unmounting an Image


After you modify an image, you must unmount it. If you mounted your image with the default read/write permissions, you can commit your changes. This makes your modifications a permanent part of the image.

To unmount an image


  1. Open a command prompt with administrator privileges. If you are using a version of Windows other than Windows 8, use the Deployment Tools Cmd Prompt installed with the ADK or navigate to the DISM directory on your local computer.

    For Windows 8.1:

    Copy
    cd C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\ <architecture>\DISM
    

    For Windows 8:

    Copy
    cd C:\Program Files (x86)\Windows Kits\8.0\Assessment and Deployment Kit\ <architecture>\DISM
    

    where <architecture> can be x86 or amd64.

  2. Unmount the image.
    Copy
    Dism /Unmount-Image /MountDir:C:\test\offline /commit
    

    where C:\test\offline is the location of the mount directory. If you do not specify the parameters to unmount, this option lists all of the mounted images but does not perform the unmount action.

    ImportantImportant
    You must use either the /commit or /discard argument when you use the /unmount option. 

     

After modifying an image, you can apply the image from a network share or from local media, such as a CD/DVD or a USB flash drive (UFD).

Troubleshooting


If the DISM commands in this topic fail, try the following:


  • Make sure that you are using the Windows 8.1 version of DISM that is installed with the Windows ADK.
  • If you are using a Windows 8 or Windows 7 PC, use the Deployment and Imaging Tools Environment to access the tools that are installed with the Windows 8.1 version of the Windows ADK.
  • Don’t mount images to protected folders, such as your User\Documents folder.
  • If DISM processes are interrupted, consider temporarily disconnecting from the network and disabling virus protection.
  • If DISM processes are interrupted, consider running the commands from the Windows PE environment instead.

DelinvFile – Delete Invalid Files and Folders

Windows allows the creation of file names and folder names that can not be deleted or renamed via Windows Explorer.  This includes names with invalid characters and names that are too long. The current limit is 260 characters.  In other words, windows can create names that are too long for the standard Windows Explorer to delete. When copying or deleting file the message: “Path too long” appears.

 

“DelinvFile.exe” is a Windows program that provides a convenient User Interface for selecting the file that you need to delete and provides for a choice of two delete methods. You use the standard controls for Drives, Folders, and Files to navigate to the Folder and Select the file to be deleted.

 

A trial version is available. See http://www.purgeie.com/delinv/

divfscrn

The Windows Server Update Services console crashes when browsing for updates

The Windows Server Update Services (WSUS) console crashes when browsing for updates and displays the following error message:

error

An unexpected error occurred. 
click reset server node to try to connect to the server again

 

This can occur if the application cache is corrupted.

To resolve this issue, delete the WSUS application cache from the location below:

C:\Documents and Settings\<user profile>\application data\microsoft\mmc

where user profile is the currently logged in user profile.

 

source: http://support.microsoft.com/kb/2761925