PowerShell for Windows Admins


September 11, 2012  11:22 AM

Finding the drive letter of a mounted VHD

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In Windows 8/2012 you can mount a VHD into the file system. Is there a way to discover the drive letter of the mounted VHD

function get-mountedvhdDrive {            
$disks = Get-CimInstance -ClassName Win32_DiskDrive | where Caption -eq "Microsoft Virtual Disk"            
foreach ($disk in $disks){            
 $vols = Get-CimAssociatedInstance -CimInstance $disk -ResultClassName Win32_DiskPartition             
 foreach ($vol in $vols){            
   Get-CimAssociatedInstance -CimInstance $vol -ResultClassName Win32_LogicalDisk |            
   where VolumeName -ne 'System Reserved'            
 }            
}            
}

Use Get-CimInstance to get the Win32_DiskDrive class. Filter on caption equalling "Microsoft Virtual Disk"

for each “physical” disk returned get the associated Win32_Volume and use that to get the associated Win32_Logical disk where you will find the drive letter.

Nice example of using associations in the CIM cmdlets

September 10, 2012  2:27 AM

PowerShell Summit Open for registration

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The PowerShell summit to be held April 2013 at Microsoft’s Redmond campus is now open for early registration.  There are very good deals available for early registration.

Details and registration link here – http://powershell.org/summit/registration.php


September 5, 2012  12:40 PM

PowerShell Summit April 2013

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A PowerShell summit is being proposed for April 2013.  It will be held on the Microsoft campus in Redmond.

Preliminary information can be found here

http://powershell.org/summit/

We’d like your help with this   by answering a s very short survey

http://674004.polldaddy.com/s/powershell-summit-na-2013

Your feed back will help us get it right

Thanks


September 4, 2012  2:46 PM

PowerShell v3 download

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The download for PowerShell v3 – now know as Windows Management Framework as you get PowerShell, WinRM and new WMI API is now available from

http://www.microsoft.com/en-us/download/details.aspx?id=34595

Versions are available for:

  • Windows 7 SP1
  • Windows 2008 R2 SP1
  • Windows 2008 SP2

No support for Windows 2003, Vista or XP


September 4, 2012  12:53 PM

Finding if the user associated with a profile is logged on

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A forum question asked how to find if the user to whom a profile belonged was logged on. There isn’t an easy way as there isn’t an association between the profile and the log on session.

There is a quick and dirty way  though

Get-WmiObject -Class Win32_UserProfile |            
foreach {            
            
$filt =  Split-Path -Path $($_.LocalPath) -Leaf            
$loggedon = $null            
$loggedon = Get-WmiObject Win32_loggedonuser |             
 where {$_.Antecedent -like "*Name=*$filt*"} |             
 select -First 1             
            
$log = $false            
if ($loggedon){$log = $true}            
            
New-Object -TypeName PSObject -Property @{            
 Name = $filt            
 Logged = $log            
}            
}

get the profiles and for each split the Localpath (path to profile) – the leaf holds the user name

Test if you can find an instance of Win32_LoggedOnUser where the Antecedent contains the name

Display data.  Results look like this

Name           Logged

—-           ——

DefaultAppPool  False

Richard          True

NetworkService  False

LocalService    False

systemprofile   False

need to filter the system accounts out but I’ll leave that to you


September 3, 2012  2:05 PM

New PowerShell site

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you haven’t found it already I would recommend you visit powershell.org at http://powershell.org/wp/home/.

Its a new location for getting answers to your PowerShell questions.

You will probably recognise some of the people moderating the forums there – Don Jones, Jeff Hicks, Kirk Munro & me among others.

Highly recommended as a PowerShell forum


September 2, 2012  12:43 PM

Windows 8 RTM startup

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I was looking at Win32_OperatingSystem today and noticed that the last boot up time wasn’t right

PS> Get-CimInstance -ClassName Win32_OperatingSystem | select LastBootUpTime

LastBootUpTime
————–
17/08/2012 10:02:11

17 August isn’t right as I know I did a cold start this morning – or at least I think I did!

Windows 8 starts up much faster than Windows 7

Looking at the System event log the last but one entry last night was event id 64 – “The system is entering sleep.”

Looks like using the Shutdown option from Settings – Power only puts the machine to sleep.

I tried using the PowerShell stop-computer cmdlet.

Shutdown took longer than usual. 

I found an entry in the system log stating

“The kernel power manager has initiated a shutdown transition.”

Startup then took much longer than usual

PS> Get-CimInstance -ClassName Win32_OperatingSystem | select LastBootUpTime

LastBootUpTime
————–
02/09/2012 19:27:41

So looks like use the Settings – Power – Shutdown option to put the machine into a deep sleep and get very quick start up.  Use stop-computer and completely shutdown but have longer startup times.

Question now is does the deep sleep put any drain on the battery for laptops? I’ll try and find out.


August 25, 2012  5:11 AM

Windows 8 tip: 1 Weather app

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Windows 8 start screen has a nice looking app to display the weather.

You can search for towns and cities to add to the display. My home town (or even the nearest big town) doesn’t show in the results returned by the search. The nearest town I can find is 50 miles away. In England that can be a whole different weather pattern.

On a whim I tried searching for my post code – came up with the correct village and displays.

US zip codes also seem to work.  Haven’t tried for any other countries so can’t guarantee results


August 25, 2012  2:06 AM

Windows 8 Networking cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Windows 8 brings PowerShell v3 and a whole bunch of PowerShell modules.  One such module is NETTCPIP and as the name suggests is about networking.

PowerShell v3 automatically loads modules for you so as soon as PowerShell opens try

PS> Get-NetIPConfiguration

InterfaceAlias       : Ethernet
InterfaceIndex       : 13
InterfaceDescription : NVIDIA nForce 10/100/1000 Mbps Ethernet
NetProfile.Name      : Unidentified network
IPv4Address          : 10.10.54.202
IPv6DefaultGateway   :
IPv4DefaultGateway   :
DNSServer            : fec0:0:0:ffff::1
                       fec0:0:0:ffff::2
                       fec0:0:0:ffff::3

InterfaceAlias       : WiFi
InterfaceIndex       : 12
InterfaceDescription : Qualcomm Atheros AR5007 802.11b/g WiFi Ada
NetProfile.Name      : TiscaliF23E11
IPv4Address          : 192.168.1.2
IPv6DefaultGateway   :
IPv4DefaultGateway   : 192.168.1.1
DNSServer            : 192.168.1.1

InterfaceAlias       : Bluetooth Network Connection
InterfaceIndex       : 30
InterfaceDescription : Bluetooth Device (Personal Area Network)
NetAdapter.Status    : Disconnected

One thing that you will need to do is to set up PowerShell remoting

PS> Enable-PSRemoting

WinRM Quick Configuration
Running command "Set-WSManQuickConfig" to enable remote management of this computer by using the Windows Remote
Management (WinRM) service.
This includes:
    1. Starting or restarting (if already started) the WinRM service
    2. Setting the WinRM service startup type to Automatic
    3. Creating a listener to accept requests on any IP address
    4. Enabling Windows Firewall inbound rule exceptions for WS-Management traffic (for http only).

Do you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a

Set-WSManQuickConfig : <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2150859113"
Machine="localhost"><f:Message><f:ProviderFault provider="Config provider"
path="%systemroot%\system32\WsmSvc.dll"><f:WSManFault xmlns:f="
http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"
Code="2150859113" Machine="RSLAPTOP01"><f:Message>WinRM firewall exception will not work since one of the network
connection types on this machine is set to Public. Change the network connection type to either Domain or Private and
try again. </f:Message></f:WSManFault></f:ProviderFault></f:Message></f:WSManFault>
At line:69 char:17
+                 Set-WSManQuickConfig -force
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-WSManQuickConfig], InvalidOperationException
    + FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.SetWSManQuickConfigCommand

The emphasis of Public is mine. We have a network connection type set to public. At this point I would normally be tearing my hair out because network connection types are the ultimate pain to modify. I have hated the things since Windows Vista. No more they are now a minor inconvenience.

Look in the module NetConnection for Get-NetConnectionProfile

PS> Get-NetConnectionProfile

Name             : Unidentified network
InterfaceAlias   : Ethernet
InterfaceIndex   : 13
NetworkCategory  : Public
IPv4Connectivity : NoTraffic
IPv6Connectivity : NoTraffic

Name             : TiscaliF23E11
InterfaceAlias   : WiFi
InterfaceIndex   : 12
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic

Now we can get to it modifying is easy

PS> Set-NetConnectionProfile -InterfaceIndex 13 -NetworkCategory Private
PS> Get-NetConnectionProfile

Name             : Unidentified network
InterfaceAlias   : Ethernet
InterfaceIndex   : 13
NetworkCategory  : Private
IPv4Connectivity : NoTraffic
IPv6Connectivity : NoTraffic

Name             : TiscaliF23E11
InterfaceAlias   : WiFi
InterfaceIndex   : 12
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic

And now you can enable PowerShell remoting

Best of all the change is WMI based.  The netconnection cmdlets are created as CDXML from WMI classes new to Windows 8. Get-NetIPConfiguration is also CDXML.

CDXML is cmdlets over objects – WMI classes wrapped in XML and presented as a module

see Chapters 18 & 19 of PowerShell and WMI for more details


August 21, 2012  2:09 PM

Windows 8 upgrade experience: #1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve upgraded two machines to Windows 8

The first is my main working laptop:

  • Windows 8 Enterprise can’t do an in place upgrade of Windows 7 Ultimate. So had to re-install all my applications. Took the opportunity to upgrade to Office 2013 and Visual Studio 2012
  • Following installed and work:
    • Adobe Digital Editions
    • Camtasia
    • Snaggit
    • Live mail 2012
    • Palm eReader
    • Skype
  • Magic Disk is no longer required because Windows 8 can mount .iso files in the file system.  Right click an .iso file in File Explorer and Mount is the first choice on the context menu
  • Windows defender now does Anti-Virus

 

The second machine was my Dell Duo:

  • Installed windows 8 as an upgrade to Windows 7 Home premium
  • Kindle for PCs doesn’t work on Windows 8. Download the free Kindle app from the Microsoft  store. Its not obvious where the downloaded books are stored
  • The DUO is a hybrid netbook/tablet. The accelerometer stopped working post upgrade meaning the screen didn’t rotate. Not good for a tablet. Dell don’t appear to do an updated driver but a web search found the issue was fixable using the Samsung driver from http://www.samsung.com/global/windowspreview/
  • Running as a tablet swipe & touch all seem to work OK

One draw back to the Kindle app is that it only seems able to access ebooks through the Amazon store. With Kindle for windows PC I could add books in from other sources. Stopping this is a backward step.  Luckily I have other means of reading those ebooks


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: