Seems like a few bugs exist in the PowerShell Web Administration cmdlets, for example I was trying to use Get-Website to find a website by its name but it always retured an array of all websites and ignored the name argument:

This DOESN'T WORK:

 $site = Get-Website -Name "DanSite"  

This WORKS:

 $site = Get-Item "iis:sites/DanSite"  

 


Posted in: IIS7 , PowerShell  Tags: , ,

Had a good one recently on a newly built server whilst trying to get PowerShell WinRM working with a local admin user, here's the scenerio:

 Enter-PSSession -ComputerName servername -Port 81 -cred servername\localusername  
 [servername]: Import-Module webadministration   
Process should have elevated status to access IIS configuration data.

When running this as a local admin account I receive the error "Process should have elevated status to access IIS configuration data.", yet if I use my domain account which is also in the admin group on the server in question then everything works!  How weird...

Anyway the problem lies in the Remote User Account Control (UAC) settings in the registry, here's the fix (taken from http://support.microsoft.com/kb/942817):

  1. Run regedit
  2. Locate and then click the following registry subkey:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system
  3. On the Edit menu, point to New, and then click DWORD Value.
  4. Type LocalAccountTokenFilterPolicy for the name of the DWORD, and then press ENTER.
  5. Right-click LocalAccountTokenFilterPolicy, and then click Modify.
  6. In the Value data box, type 1, and then click OK.
  7. Exit Registry Editor.

or in Powershell:

new-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -propertyType DWord -Name "LocalAccountTokenFilterPolicy" -Value 1

 

This setting will take effect straight away so no reboot required.

 

 


Posted in: PowerShell  Tags: ,
Admin posted on October 30, 2011 16:38

This might catch some people out:  Downloading a ps1 file from the Internet and tring to execute results in error:

 

Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your computer. Do you want to run C:\Downloads\SetupEnterpriseSearch.ps1?

Even with executionpoilcy set to unrestricted you'll get this message, this is because by default these files downloaded are maked as blocked so to unlock simply right click on the script in question and click "unblock".

 


Posted in: PowerShell  Tags:
Admin posted on October 25, 2011 13:08

Finally decided today that I no longer want to use the DOS command prompt, PowerShell is the way to go for everything!  But when I do use the VS DOS prompt it's normally because I want to use msbuild which means setting the correct envionment path.  A quick Google brings up some samples so I "lifted" one from here and changed it for VS 2010.

In PowerShell locate your profile:

  notepad $profile  (if you get prompted to create the file because you don't have a profile then click yes to create it)

Now here's the contents of Microsoft.PowerShell_profile.ps1:

 pushd 'C:\Program Files\Microsoft Visual Studio 10.0\vc'  
 cmd /c “vcvarsall.bat&set” |  
 foreach {  
  if ($_ -match “=”) {  
   $v = $_.split(“=”); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"  
  }  
 }  
 popd  
 write-host "`nVisual Studio 2010 PowerShell Prompt" -ForegroundColor Yellow  

Open up PowerShell and all of the VS commands are availble!...Good bye DOS!!


It's quite simple to find the name of the operating system with PowerShell.  The WMI cmdlet GetWmiObject contains a lot of useful information about the environment, there's numerous ways to use the object but here's one method:

 (Get-WmiObject Win32_OperatingSystem).name  

This should work straight away but for me I encounted some "weird" issues which suggested there were some problems with my Windows installation and specifically the WMI repository.  Intially I received the following error: running the above command:

 InvalidOperation: (:) [Get-WmiObject], COMException  

Here's what I did to fix the problem:

 

  • Acertain if there is a problem with WMI (C:\Windows\System32\wbem)
    winmgmt /verifyrepository
  • This this should show you if there are any issues with WMI, this came back and said everything was ok when clearly it wasn't!...
  • I rebuilt the repository in PowerShell
     winmgmt /resetrepository  
     Restart-Service winmgmt -Force  

 


As with most of my Windows 2008 administration scripts I prefer to only "touch" the system if it needs updating otherwise leave it alone.  The script below shows how to detect if a service is set to manual, change to automatic and start if required.  Oddly the Get-Service cmdlet doesn't return the start type in PowerShell 2.0 for some reason.  

 

 function SetServiceStartToAutoMatic($serviceName = $(throw "Service Name was not specified")){  
      $service = Get-Service $serviceName  
      $startType = Get-WmiObject -Query "Select StartMode From Win32_Service Where Name='$($serviceName)'"  
   
      if ($startType.StartMode -eq "Manual"){  
           Set-Service $serviceName –StartupType Automatic  
      }  
 }  
   
 function StartServiceIfStopped($serviceName = $(throw "Service Name was not specified")){  
      $service = Get-Service $serviceName  
      if ($service.Status -eq "Stopped"){  
           Start-Service $serviceName  
      }  
 }  
   
 function Main(){  
      $smtpServiceName = "SMTPSVC"  
      SetServiceStartToAutoMatic $smtpServiceName  
      StartServiceIfStopped $smtpServiceName  
 }  
   
 Main  

 


I had a nice error on a Windows 2008 R2 server whilst trying to install some server roles/features with PowerShell:

 Import-Module servermanager  
 Get-WindowsFeature  

Resulted in error:

   
 Get-WindowsFeature : Failed to initialize.  
 At line:3 char:19  
 + Get-WindowsFeature <<<<   
   + CategoryInfo     : OperationStopped: (:) [Get-WindowsFeature], COMException  
   + FullyQualifiedErrorId : DiscoveryFailed,Microsoft.Windows.ServerManager.Commands.GetWindowsFeatureCommand  

You might also see this error when opening the Windows 2008 Server Manager Roles or an exception code 0x800B0100.  

The fix:

- Download and install the System Update Readniess Tool: http://support.microsoft.com/kb/947821/en-us
 -When prompted install the KB patch/fix.


Admin posted on October 11, 2011 12:50

Here's a PowerShell script that determines if the OS is Windows Server 2008 and enables a list of windows features.  To get a list of features and enabled status run Get-WindowsFeature (after importing the  servermanager module).

 

 Import-Module servermanager  
   
 function IsWindows2008(){  
   $operatingSystem = gwmi win32_operatingSystem | select name  
   return $operatingSystem.name.contains("Server 2008")  
 }  
   
 function EnableWindowsFeatures($featureList) {  
   foreach ($feature in $featureList) {  
     $featureInfo = Get-WindowsFeature $feature  
     if (!$featureInfo.installed){  
       Add-WindowsFeature $feature  
     }  
   }  
 }  
   
 function Main() {  
   if(IsWindows2008){  
     $features = "Web-Windows-Auth", "Web-Asp-Net"  
     EnableWindowsFeatures $features  
   }  
 }  
   
 Main  
   

Admin posted on October 10, 2011 12:42

Had an interesting one today whilst rebuilding a server and enabling WinRM, after running my scripts to enable Powershell Remoting I was able to remotley invoke-command by passing in my domain user which is a local admin sucessfully but when I tried the same thing using a local admin account I kept receiving:

Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

After digging around the only work around I could find was to grant extra permissions to the local admin account by running (in administrator console):

 Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI -force  

When the UI appears just add the name of the local user with full control.


Posted in: PowerShell  Tags:

Here's how to enable IIS7 ISAPI CGI restrictions using PowerShell and the webadministration modules:

 

 Import-Module webadministration  
   
 function Is64Bit  
 {  
      [IntPtr]::Size -eq 8  
 }  
   
 function EnableIsapiRestriction($isapiPath){  
      $isapiConfiguration = get-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed"  
   
      if (!$isapiConfiguration.value){  
           set-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed" -value "True" -PSPath:IIS:\  
           Write-Host "Enabled ISAPI - $isapiPath " -ForegroundColor Green  
      }  
 }  
   
 function EnableDotNet40Isapi($systemArchitecture){  
      $frameworkPath = "$env:windir\Microsoft.NET\Framework$systemArchitecture\v4.0.30319\aspnet_isapi.dll"  
      EnableIsapiRestriction $frameworkPath       
 }  
   
 function Main(){  
      if (Is64Bit){  
           EnableDotNet40Isapi "64"  
      }  
      EnableDotNet40Isapi  
 }  
   
 Main  

Posted in: IIS7 , PowerShell  Tags: , ,

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Recent Comments

Banners

Theme Grabber
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Dan Gibbons .Net Developer