Manipulando IIS com Powershell
Autor: Eduardo Spaki | Publicado: 2011-09-07T21:24:00-03:00

  Ultimamente tenho trabalhado bastante com Powerhell Powershell devido a uma consultoria no módulo de Build do TFS. O legal desse módulo é que você pode interagir com scripts Powershell e variáveis do Work Flow que o TFS utiliza durante um build.   O Powershell é muito poderoso, isso se deve ao fato dele usufruir das classes .NET, utilizar o Windows Remote Management (WinRM) para execução remota de scripts, e comandos nativos das versões mais atuais do prompt, como o ROBOCOPY.    Assim como no .NET, nós podemos instalar módulos adicionais, e até mesmo importar os já instalados. No caso do IIS devemos importar o módulo “WebAdministration” . Então segue alguns scripts para manipulação do IIS.     Import-Module WebAdministration;    #region Manipulação do IIS     &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function PararPool([string] $poolName)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; Stop-WebAppPool -Name $poolName;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function IniciarPool([string] $poolName)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; Start-WebAppPool -Name $poolName;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function PararSite([string] $siteName)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; $site = GetSite $siteName;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; $site.Stop();      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function IniciarSite([string] $siteName)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; $site = GetSite $siteName;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; $site.Start();      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function GetSite($siteName)      &#160;&#160;&#160; {&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160; $site = Get-Item IIS://Sites/$siteName -ErrorAction SilentlyContinue;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; return $site;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function GetDirectorySite($site)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; (Get-Item IIS://Sites/$site).PhysicalPath;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function GetPoolSite($site)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; $objSite = Get-Item IIS://Sites/$site;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; return $objSite.ApplicationPool;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; function CriarSite([string] $siteName, [string] $caminho)      &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; New-Item IIS:\AppPools\$siteName;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; New-Item IIS:\Sites\$siteName -bindings @{protocol=&quot;http&quot;;bindingInformation=&quot;:80:$siteName&quot;} -physicalPath $caminho;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; Set-ItemProperty IIS:\Sites\$siteName -name applicationPool -value $siteName;      &#160;&#160;&#160; }      &#160;&#160;&#160;&#160;&#160;&#160;&#160; #endregion   Os scripts foram testados no IIS 7 e 7.5.    É lógico que existem mil e uma maneiras de preparar neston atingir um objetivo.     É possível utilizar o Active Directory Service Interfaces (ADSI), ou até mesmo o Windows Management Instrumentation (WMI). Acompanhe como reciclaríamos um determinado pool do IIS com o ADSI:     function ReciclarPool([string] $server, [string] $poolName)     {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160; ([adsi]&quot;IIS://$server/W3SVC/AppPools/$poolName&quot;).Recycle();      }   Uma última dica por hora, para editar e depurar scripts powershell, estou usando o programa PowerGUI.            É isso daí pessoal. Por hora é só, espero que tenham gostado!