I have a home ISP and get every once a week or so a new external WAN IP.

It’s no problem but it makes RDPing a little difficult.

Options are DynDNS or NoIP, I however wanted something Powershell based.

I set it up as a scheduled task and run it once an hour. It will see if there is a new IP, if there is it’ll write it to a Dropbox file that I can access from my phone, so I’ll always know the current WAN IP.

function get-exip {
$url = "http://checkip.dyndns.com"

$webclient = New-Object System.Net.WebClient

$IP = $webclient.DownloadString($url)

$Ip2 = $Ip.ToString()
$ip3 = $Ip2.Split(" ")
$ip4 = $ip3[5]
$ip5 = $ip4.replace("</body>", "")
$FinalIPAddress = $ip5.replace("</html>", "")
$IPnew = Get-Content -Path "C:\users\$ENV:USERNAME\Dropbox\Scripts\TEST\index.html"
}

if (!($FinalIPAddress -eq $IPnew)){
get-exip
}

 


Another handy quick tip:

Ever needed to verify an ISO, to see if it has the right hash?

Super easy with Powershell!

Get-FileHash -Path <path> -Algorithm MD5 | select hash

and you get the MD5 has as a result:

# Will look something like this
Hash
----
C6DF6C9782B127FF277B03DC78FC7846

 


We have quite a few systems that are for high security use and internal access only, which is fine however sometimes working on Powershell scripts using the help files would be quite, well , helpful.

What you can do however (starting Powershell v.3) is import and export export help files. Look at this:

# Save Help Files for all modules
Save-Help -DestinationPath c:\PS_helpfiles -Module * -Force

# Copy the c:\pf_helpfiles to the server where you want to add them
Copy-Item -Path c:\ps_helpfiles -Destination \\server\c$\ps_helpfiles

# Update help from folder
Update-Help -SourcePath c:\ps_helpfiles -Module * -Force

 


A virtual machine in Hyper-V consists of a few files that account for its virtual hardware configuration and the virtual storage (VHD and VHDX files).  By default the virtual machine configuration files are stored in_C:\ProgramData\Microsoft\Windows\Hyper-V_, and the virtual hard drives are stored in C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks.

One slight improvement in Hyper-V (in Windows Server 2012) is that during the installation process (GUI mode only) it gives you the option of changing these defaults.  However the defaults are still the same as they used to be… on the C drive.

Being cheap I only have a ‘tiny’ SSD (ok folks I bought it years ago and it felt like it was a fortune back then…) as C:\, all other data is still on rusty spindles on my home lab.

VMs, I know are important but are small are still on the C:\ however others I had to move off.

Now tiering storage is fine, it is a PITA to find where which VHD/VHDX is stored via GUI. The fastest way I found to scavenger your lost treas… um VHDs is of course Powershell.

# Run as admin
Get-VMHardDiskDrive * | Select VMName, Path

then being a little OCD…, I like to sort it after names. An example can look like this:

PS C:\WINDOWS\system32> Get-VMHardDiskDrive * | select vmname, path | Sort-Object VMName

VMName    Path
------    ----
2016      E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\2016.vhdx
CentOS_01 C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\CentOS_01.vhdx
CentOS_02 C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\CentOS_02.vhdx
CentOS_03 C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\CentOS_03.vhdx
CentOS_04 C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\CentOS_04.vhdx
DC01      E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\DC01.vhdx
DC02      E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\DC02.vhdx
Kali      E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\Kali.vhdx
SCCM01    E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SCCM01.vhdx
SCCM02    C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SCCM02.vhdx
SpiceW    E:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SpiceW.vhdx

Now, that is not bad, what if I need more info, let’s say also the VMID so you can quickly RDP into the VM (see my post about RDCMan).

Easy, Get-VMHardDiskDrive does not have the info about the VMID, Get-VM however does, so we can just pipe that in it like so:

Get-VM * | Get-VMHardDiskDrive | Select vmname,vmid,path | Sort-Object vmname

That will give us a nice list with the VMname, VMID and VHD(x) path.

Another thing to mention. if you wan to change the default location of your VM Disks, or even of the machines, Powershell can do that as well:

SET-VMHOST –computername <server> –virtualharddiskpath 'C:\VHDs'
SET-VMHOST –computername <server> –virtualmachinepath 'C:\VMs'

or via GUI of course in the Hyper-V settings:

2016-01-10_17-31-06


I am a big fan of MSFT’s Remote Desktop Connection Manager, I have dozens of servers in it at my work, so I started tinkering around with it at home, especially in mind connecting to all my VMs  on my Hyper-V test lab.

Turns out, is is not as straight forward as you’d think it’d be.

Here is how to get it to work:

  1. There are issues with the authentication. Adding the following registry keys fixes that: <pre class="lang:ps decode:true"># Run PowerShell as admin!

    Please remember, this is provided ‘as-is’

    Think twice before executing code you are not sure what it does. :o)

    New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowDefaultCredentials -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowDefaultCredentialsDomain -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentials -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentialsDomain -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentialsWhenNTLMOnly -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentialsWhenNTLMOnlyDomain -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowSavedCredentials -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowSavedCredentialsDomain -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/” -Force New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowSavedCredentialsWhenNTLMOnly -Name Hyper-V -PropertyType String -Value “Microsoft Virtual Console Service/*” -Force

</pre>

  1. Even though you might be a local admin on the Hyper-V Host, add your account to the ‘Hyper-V Administrators’ Group.
  2. Get the ID of the VM you want to connect to <pre class="lang:ps decode:true"># First use ‘Get-VM’ to get a list of all the VMs on your host Get-VM

    Then get the ID

    Get-VM “<VMNAME>” | select Id</pre>

  3. Now we can go to the RDCMan man and add a new server
&#8211; Server name: _Is the name of your Hyper-V Host (NOT the VM)

_ – Check ‘VM console connect’ and paste the ID we got via PowerShell

&#8211; You can put whatever you want as &#8216;Display Name&#8217;
  
<a href="http://blog.vvittig.com/wp-content/uploads/2015/12/2015-12-30_10-11-04.png" rel="attachment wp-att-390"><img class="aligncenter size-full wp-image-390" src="http://blog.vvittig.com/wp-content/uploads/2015/12/2015-12-30_10-11-04.png" alt="2015-12-30_10-11-04" width="367" height="130" srcset="https://blog.vvittig.com/wp-content/uploads/2015/12/2015-12-30_10-11-04.png 367w, https://blog.vvittig.com/wp-content/uploads/2015/12/2015-12-30_10-11-04-300x106.png 300w" sizes="(max-width: 367px) 100vw, 367px" /><br /> </a>   5. Test and see if you can connect. To connect you have to provide the credentials to log in to the HYPER-V HOST, not the VM.

Now you should be able to use RdcMan to connect to your VMs, be it Window Linux, or anything else you can run as a VM. :o)