PowerShell modify root site properties - powershell

I need to set the serviceAutoStartProvider and serviceAutostartEnabled properties on some websites with powershell.
This cmdlet works on website with web application:
Set-ItemProperty IIS:\Sites\$websiteName\$webApplicationName -name serviceAutoStartEnabled -Value 'True'
With website without web application, I'm doing this
Set-ItemProperty IIS:\Sites\$websiteName -name serviceAutoStartEnabled -Value 'True'
But it doesn't work, I got this error
Property serviceAutoStartEnabled is not found
When I check the applicationHost.config file of IIS, every website have a default webapplication, so, how can I set these properties on the root site ?
Thank you,

I found this cmdlets
Set-ItemProperty IIS:\Sites\$siteName -name applicationDefaults.serviceAutoStartEnabled -Value 'True'
And It's works :)

Related

How to set applicationDefaults.preloadEnabled to True on a IIS WebApplication (instead of WebSite) with Powershell?

I'm including a Powershell script in my WixToolset installer to do various tasks in IIS, and I can't figure out one thing.
My Site in IIS is structured like this:
Sites
Default Web Site
WebApp1
WebApp2
Identity
I am able to set applicationDefaults.preloadEnabled to true on Default Web Site, but I only want to set preloadEnabled on my Identity WebApplication.
With limited Powershell knowledge, I've tried:
Import-Module WebAdministration
Get-WebApplication
Get-WebApplication "Identity"
The code above lists the Identity WebApplication correctly.
cd C:\inetpub\wwwroot
Set-ItemProperty "Identity" -Name applicationDefaults.preloadEnabled -Value True
The code above gives the error:
The property string applicationDefaults.preloadEnabled=True does not exist or was not found.
At line:1 char:1
I've also tried preloadEnabled instead of applicationDefaults.preloadEnabled, same result.
figured it out thanks to the comment from #guiwhatsthat and some extra searching. This is what worked.
Set-ItemProperty "IIS:\Sites\Default Web Site\Identity" -Name preloadEnabled -Value True

IIS: add a Deny rule to .NET Authorization

Afternoon guys,
I'm working on going through some lockdowns for IIS, I need to add a Deny rule to .NET Authorizations for all anonymous users.
I have this, which partially works
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT' -filter "system.web/authorization" -name "." -value #{accessType='Deny';users='?'}
? is an alias for All anonymous users
It partially works, as in it creates the rule, but it's set as an Allow Rule even though I'm calling Deny.
Does anyone have any ideas on how to get this to register as a Deny Rule?
I ended up finding the answer in the Related section off to the side. Don't know why this never popped up in my initial googling.
Managing IIS .Net Authorization Rules with a powershell script
My final code is
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT' -filter "system.web/authorization" -name "." -value #{users='?'} -Type 'deny'

How to get/add/changed allowed server variables of IIS Web Site with PowerShell?

I'm trying to make a PS script that creates an IIS web page, configures a reverse proxy via URL Rewrite and I'm stuck on how to add allowed server variables via PowerShell.
Does anyone know how to add these variables via powershell for the IIS website?
UPDATE: I have solved this problem
Step 1. Unlock server configuration section
%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/rewrite/allowedServerVariables
Now it's posible to get allowed server variables with
import-module WebAdministration
$webSiteName = "your-site-name"
$allowedServerVariables = Get-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST/${webSiteName}" -filter "system.webServer/rewrite/allowedServerVariables/add" -Name name
Write-Host $allowedServerVariables.Value
Example for adding server variables
import-module WebAdministration
$webSiteName = "your-web-site-name"
add-webconfigurationproperty -pspath "iis:\sites\${webSiteName}" -filter "system.webserver/rewrite/allowedservervariables" -name "." -value #{name='HTTP_X_ORIGINAL_ACCEPT_ENCODING'}
UPDATE 2
Better solution without PowerShell is to add allowed server variables globally on IIS web server. So each variable will be automatically inherited from new web site.
FYI adding server variable on server level by powershell:
Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value #{name="RESPONSE_SERVER"}
this will add the following configuration in applicationhost.config
<system.webServer>
...
<rewrite>
...
<allowedServerVariables>
<add name="RESPONSE_SERVER" />
</allowedServerVariables>
...
</rewrite>
</system.webServer>

Set-ItemProperty for http removes existing bindings for website in IIS

One of my application (say app1) running under website in IIS created https binding during deployment. However when another application (say app2) under same website deployed recently via power shell script, it removed previously added https binding and broke app1.
When I looked into deployment script of app2, I realized there is a function to check if binding already exist - if yes, simply call Set-ItemProperty to update that binding or else create the one. This idea looks fine to me - basically it says create binding specific to application or update if already existing. But am not sure, why Set-ItemProperty for http removed https binding (in fact all others as well like net.tcp, net.pipe etc)
Below is function from that deployment script.
Import-Module -Name WebAdministration
function SetBindingsIIS
{
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$WebsiteName,
[HashTable]$protocol
)
$Status=$null
$GetProtocolName= $protocol["Protocol"]
$BindingsCollection=Get-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings
$ProtocolExists=$BindingsCollection.Collection | Where-Object{$_.protocol -eq $GetProtocolName}
Try
{
if($ProtocolExists -eq $null)
{
New-ItemProperty -Path IIS:\Sites\$WebsiteName -Name Bindings -Value $protocol -Force
}
else
{
Set-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings -Value $protocol -Force
}
$Status="Success"
}
Catch
{
$ErrorMessage=$_.Exception.Message
$Status="Error in Add/Update bindings : $ErrorMessage"
}
return $Status
}
Running this function simply removes all existing bindings already configured for web site in IIS
SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol #{Protocol="http";BindingInformation=":81:"}
The reason it is removing all your bindings is that it is taking whatever you pass to $Protocol and over-writing the Bindings property, which is a collection of all the bindings for the site.
You should use the WebAdministration module that ships with IIS to do this instead of the generic item cmdlets. It contains various useful cmdlets, including Set-WebBinding and New-WebBinding. For example:
New-WebBinding -Name "TestMiddleTierSite" -IPAddress "*" -Port 81 -Protocol http
While #boxdog's answer is right and recommendable: it is possible to add a binding using *-ItemProperty and the IIS: PSDrive. Just do not use Set-ItemProperty, but New-ItemProperty to add a new property to the collection:
New-ItemProperty 'IIS:\Sites\Default Web Site' -Name bindings -Value #{protocol='http'; bindingInformation='*:81:'}

I have 2 questions about powershell and IIS

I'm trying to set a user account as the Anonymous identity for a website authentication, and I've looked through every page on my Google searches but haven't found anything. There's a few things about setting it as the pool identity which is where I got the below line, but I'm not sure how that line is setting the pool identity, and I've tried to modify it to set the user account I want, but it's not working:
set-webconfigurationproperty /system.webServer/security/authentication/anonymousAuthentication -name UserName -value "domain\user" -Location "iis:\Sites\$NewApp"
I'm also trying to set a binding after I create a site, but the binding isn't being applied correctly, and I can't see why. I've looked at many examples online and tried to mimic what they have. I'm sure I just have a formatting error or something small like that. This is my binding line:
New-Item IIS:\Sites\$NewApp -bindings #{protocol="http";bindingInformation=$NewIP+":80"} -physicalPath "E:\Physicalpath"
I've spent 2 hours on this tonight, and it's driving me crazy. I just broke down and configured the servers manually for those couple items, but I really want to get this working so I can use it for other site creation scripts.
Thanks in advance for any assistance.
I was able to set the user, needed to add -PSPath. You should be able to combine -PSPath and -Location as per the Link: Enable authentication for IIS app in Powershell
set-webconfigurationproperty /system.webServer/security/authentication/anonymousAuthentication -name userName -value "domain\user" -pspath iis:\ -Location "iis:\Sites\pwa"
Further, you second doubt is clarified here: Examples of IIS Powershell cmdlets
From the above link:
COMMAND Examples: When -Value parameter is used by IIS powershell cmdlet, it usually set with a single string value (NOTE: if
the value contains space, it should be quoted with " or ')
The value can be hash table object if it is used to set a collection item
add-webconfiguration
'/system.applicationHost/sites/site[#name="Default Web
Site"]/bindings'-value #{protocol="http";bindingInformation="*:80:"}
-pspath iis:\
You can use Add-WebConfiguration for setting bindings, like so:
Add-WebConfiguration '/system.applicationHost/sites/site[#name="pwa"]/bindings' -Value #{protocol="ftp";bindingInformation="10.0.0.100:21:pwa.fabrikam.local"} -PSPath iis:\
Or, you can use Set-WebConfiguration for modifying:
set-WebConfiguration '/system.applicationHost/sites/site[#name="pwa"]/bindings' -Value #{protocol="https";bindingInformation="*:443:pwa.fabrikam.local";sslFlags=0} -PSPath iis:\
Adding everything via variables. You can manage your variables it suites your needs, I've separated every components of bindings into various variables. Putting variables inside bracket helps evaluating expressions.
$siteName = "pwa"
$appFilter = "//system.applicationHost/sites/site[#name='$($sitename)']/bindings"
$newIP = "*"
$port = 80
$hostName = "pwa.fabrikam.local"
$bindings = #{
protocol = "http"
bindingInformation="$($newIP):$($port):$($hostName)"
}
set-WebConfiguration -Filter $appFilter -Value $bindings -PSPath iis:\
If you now change only variables, the commands should continue to function. I tried changing IP and ports via variables $newIP and $port. Let us know how it goes.