How to set applicationDefaults.preloadEnabled to True on a IIS WebApplication (instead of WebSite) with Powershell? - 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

Related

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>

New-WebApplication fails using network path in physicalpath parameter in powershell

We are trying to create list of applications in a particular site,
For this we have a csv which contains required information for site creation. Below is the format:
Below is the script which we are using to create application in IIS:
foreach($app in $apps){
$appname = $app.path.TrimStart("/")
New-WebApplication -Name $appname -Site $app.Site -PhysicalPath $app.PhysicalPath -ApplicationPool $app.applicationPool -Verbose
}
Problem is application whose code is stored on some network path like \\network\Webapps\appname are giving below error:
New-WebApplication : A parameter cannot be found that matches parameter name 'physicalPath'.
whereas application folder located on same server are being created without any issue,
Have also done test-path \\network\webapps\appname it results in true
What is the issue and how to rectify it?
Issue got resolved, I didn't got what was the exact reason behind that, Have updated my script, basically have assigned values to specific variable than was able to create application.
Updated Script
foreach($app in $apps){
$appname = $app.path.TrimStart("/")
$appSite = $app.Site
$appPath = $app.PhysicalPath
$appPool = $app.applicationPool
New-WebApplication -Name $appname -Site $appSite -PhysicalPath $appPath -ApplicationPool $appPool -Verbose
}
Thanks
First of all, you have to make sure that powershell has permission to access the server corresponding to the network path when creating the site.
When using powershell to create a site, if the physical path of the site is a file in the local disk, you can use New-WebApplication, if it is a UNC path, you should use New-Item.
Under normal circumstances, the UNC path will not be used as the physical path of the main site, but will be added as a virtual directory. Because the domain name of the main site points to the local machine and the UNC points to the remote IP address, the two will conflict because a server error occurred when I created the site through IIS Manager.
$siteName = 'Default Web Site'
$virtualDirectoryName = 'Test'
$physicalPath = '\\UNC-path'
## Init
$virtualDirectoryPath = "IIS:\Sites\$siteName\$virtualDirectoryName"
## Create Virtual Directory where physicalpath is an UNC-path (New-WebVirtualDirectory
wont do)
New-Item $virtualDirectoryPath -type VirtualDirectory -physicalPath $physicalPath
Any chance it's this? That error sounds like it's an issue with the cmdlet and not the data you're feeding it.
https://social.msdn.microsoft.com/Forums/en-US/3e0cb076-6ad4-4b03-bb59-c912174266b0/container-with-iis-cant-create-application-using-powershell-physicalpath-parameter-not-found?forum=windowscontainers
specifically:
You need to import the WebAdministration module. If you're in the container through docker exec simply use:
Import-Module WebAdministration

How can I check if there a folder is converted to Application or not in IIS through Powershell on remote servers?

I want to check if folders/virtual folders in the IIS are converted to Application or not through Powershell on multiple remote servers. I tried using Get-WebVirtualDirectory but I'm getting an empty response.
I've my site structure like this : IIS:\Sites\Default Web Site\Services\Folder\V4\Task\SomeService and in this, the SomeService is in folder format on some of the remote servers. I need to check on all the servers if it is still in folder format and later on convert it Application. But Get-WebVirtualDirectory is not giving any errors or results.
I was using this query to get the virtual directory : Get-WebVirtualDirectory -site 'Default Web Site' -Application Task
This is a late answer but it may help someone.
Use Get-WebApplication function instead of Get-WebVirtualDirectory.
Example :
Get-WebApplication -site "Default Web Site" -Name "ApplicationName"
In your case :
if((Get-WebApplication -site "Default Web Site" -Name "Services/Folder/V4/Task/SomeService").Count -gt 0) {
some stuff...
}

Managing IIS .Net Authorization Rules with a powershell script

I'd like to simply add some .Net Authorization rules in IIS (7.5 / win 2008 R2) using a powershell script with PS snap in. So far I was able to add some "allow" rules but not any deny ones.
Each time I try, it either does nothing or creates an "allow" rule, which seems odd, like if it was defaulting to allow all the time.
I tried with add-webconfigurationproperty and add-webconfiguration with no luck.
Maybe one of you has the correct command line to use?
For instance:
Add-WebConfiguration "/system.web/authorization" -value #{ElementTagName="Deny";users="*"} -PSPath "IIS:\Sites\Mysite"
Add-WebConfigurationProperty "/system.web/authorization" -Name "collection" -value #{ElementTagName='deny';users='test'} -PSPath "IIS:\Sites\Mysite"
will create 2 "allow" rules.
Same behavior if I remove ElementTagName='deny'. So weird. Like if the deny "mode" was to be accessed in some different way.
And for instance, if I go to IIS 8 and try to generate the script after adding a deny rule, the command line suggested is not working either.
How can I fix this?
The command you should use to add a deny rule in your example is:
Add-WebConfigurationProperty "/system.web/authorization" -PSPath "IIS:\Sites\Mysite" -Name "." -value #{users='test'} -Type "deny"
This bothered me too & I also had trouble getting appcmd to do the same thing. I did get it working in the end & found that the -Type parameter was the important one. This wasn't all that clear to me from the documentation which just says:
The type of property to add.

PowerShell modify root site properties

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 :)