Monday, November 12, 2012

PowerShell: Resolve Remote Host and IPv4 Address

Update: I found the script block only works on local hosts.  As such, I found another way to get the remote host name and IP via PowerShellASP.

<%$ipaddress = [net.IPAddress]::Parse($request.UserHostAddress)%>

<%$UserHostname = [System.Net.Dns]::gethostentry($ipaddress.ipaddresstostring)%>
<%$UserHostname = $UserHostName.hostname -replace ".domain.suffix",""%>
Your Computers IP:<%=$ipaddress%> & Host Name:<%=$userhostname%><br><hr>

Click here to find your public IP address without using PowerShellASP.


I don't know if this helps but I've been working on a way to obtain the remote host name and IP for my PowerShellASP site.  Every several searches and testing, I found .net commands which extract the host name and an IPv4 adddress:
#PowerShell Script
$remotehostname = [system.net.dns]::gethostname()
#I noticed the ScopeID of IPv4 addresses is always $null, so I used that to find the IPv4 address
$remotehostipv4 = [system.net.dns]::gethostaddresses($remote)|?{$_.scopeid -eq $null}|%{$_.ipaddresstostring}
#Sample line displaying the host and IP.
$remotehostname,$remotehostipv4
Then I applied them to a test PowerShellASP file:



<%$remote = [system.net.dns]::gethostname()%>

<%$remoteip = [system.net.dns]::gethostaddresses($remote)|?{$_.scopeid -eq $null}|%{$_.ipaddresstostring}%>

Remote IP: <%=$remoteip%><br>
Remote: <%=$remote%>

By obtaining the remote host name and IP (a PowerShellASP website visiting computer), I can create a remote connection and run PowerShell commands on the remote host for the user.  By default, PowerShellASP will run all commands on the server, meaning that a request to see how much hard drive space is left will be ran on the server.  This will return the server's remaining hard drive space result to the remote user.  Frankly, the user doesn't care about the servers hard drives and really wants to know about their own hard drives.  To perform this request, the web server has to create a remote WMI connection and run the command. Having the remote host information allows this to happen.

Sunday, November 4, 2012

Virtual PC: Mounting a Windows 7 32-bit VM

The following is an overview of the Windows 7 Virtual Machine (VM) for Windows 7 Virtual PC creation process:
1. Download the AIK (MS Automated Installation Kit) with SP1.
2. Copy a file from the AIK to a hard drive with ample room for the VM.
3. Download and move a script which converts a Windows 7 install CD into a VHD.
4. Download and install Windows 7 Virtual PC.
5. Create a virtual machine.
6. Start the Windows 7 VHD virtual machine.
7. Install Windows 7 as normal.
8. Install the Integration Components.

Detailed Instructions:
1. Download the Automated Installation Kit; which is an ISO file.  ISO's are not normally readable by Windows so I used Slysoft's free virtual clone drive.  Slysoft's software will install a virtual DVD drive which shows up in the system tray with a new drive letter.



Right click on its system tray icon and "mount" the AIK.ISO file that you downloaded.  Let autorun start the AIK install or alternatively double-click on StartCD.exe.  Install the software.

2. Download the Automated Installation Kit Service Pack 1.  Mount the downloaded waik_supplement_en-us (or equiv).ISO and copy the contents into the AIK PETools folder (c:\program files\Windows AIK\Tools\PETools - if you used the defaults).
3. Download the Microsoft WIM2VHD.WSF script. Now you want to copy WIM2VHD.WSF to a large drive.  This will be the location where your new Windows 7 virtual drive will be created.  Also, go into c:\program files\windows AIK\Tools\ and copy imageex.exe (from the appropriate CPU-type subfolder) to the same drive as WIM2VHD.WSF.
4. Make your Windows 7 32-bit DVD/USB source accessible and run this command from an elevated command line (right-click cmd.exe runas-administrator) from the folder which has WIM2VHD.WSF and imageex.exe:
cscript wim2vhd.wsf /wim:d:\sources\install.wim /sku:{professional|enterprise|ultimate} 
note: d: - current Windows 7 install source location; sku type is Windows version (pick one).
 

5.  When the script successfully completes, it will create the VHD file and place it in the folder where you ran the script.  It will have a name similar to this:
(6.1.7600.16385.x86fre.win7_rtm.090713-1255.Enterprise.en-US.vhd)

6. Download Windows Virtual PC. You'll have to pass a Windows license verification authentication first.  Also, check your computer to ensure it can support a virtual machine.  Your BIOS and components have to possess this capability and download and run this Microsoft tool to test your computer.  After you have tested for compatibility and validated your license, install Windows Virtual PC.  You will have the choice of installing the 32 or 64 bit version.  For some reason, I can only use 32-bit virtual machines even though I have the 64-bit Virtual PC software.  I think it might be a software limitation.  Also, be aware that if you've disabled the Windows Update service, this software will be unable to install.

7.  From the Start Menu, open Windows Virtual PC.  Create a Virtual Machine and select your newly created VHD.  After creating the VM, click on Settings and review/alter the settings to suit your needs.



8.  Open the VM and you'll see it perform sysprep steps as it installs Windows 7 in your Virtual Machine.
9. When finished, it will act like a normal Windows installation and need anti-virus, security patches, and other normal utilities.
10.  Ensure to install the Integration Tools found on the VM's Tools menu.  This will allow you to move your mouse in and out of the VM without it being captured.  To break your mouse out of the VM, press the Windows keyboard key.


This blog entry based on information from:
http://forums.mydigitallife.info/archive/index.php/t-32822.html
http://www.hanselman.com/blog/StepByStepTurningAWindows7DVDOrISOIntoABootableVHD VirtualMachine.aspx