• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2532
  • Last Modified:

Powershell IIS configuring single site

I am writing/copying a PowerShell script, for use in configuring an IIS 7 site.

I need to use native commands like below:
New-WebVirtualDirectory -Site $webhost -Name usage -PhysicalPath e:\sites\$websitename\usage -force

How can I re-code these snippets of code, to be PowerShell native, and use variables:?
Not using the appcmd.exe
-----
1)

# Enable 'Directory Browsing' on Virtual Directory
C:\Windows\System32\inetsrv\appcmd.exe set config "$webhost/usage" -section:system.webServer/directoryBrowse /enabled:"True" /showFlags:"Date, Time, Size, Extension"

-----

2)

# Sets logging fields under "logging" on the site.
$a = get-itemproperty IIS:\Sites\$webhost
$a.logFile.logExtFileFlags = "Date,Time,ClientIP,UserName,SiteName,ComputerName,ServerIP"
$a.logFile.directory = "E:\sites\$websitename\logs1"
$a | set-item

-----

3)

I am having difficulty finding a way to use a variable ($webhost) in this line:
Set-WebConfigurationProperty -Filter '/system.applicationHost/sites/site[@name="$webhost]/LogFile' -name localTimeRollover -value true

-----

4)

Not able to find code on how to change the log schedule to "Daily"
0
raymoyneur
Asked:
raymoyneur
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
Import-Module WebAdministration

#1) set directoryBrowse configuration
Set-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -Value $true -PSPath 'IIS:\Sites\$webhost\usage'

#2) change log directory and flags
Set-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory -value "E:\sites\$websitename\logs1"

Set-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile 
-Name LogExtFileFlags -Value "Date,Time,ClientIP,UserName,SiteName,ComputerName,ServerIP"

#3) it looks like u forgot the closing quotes, tru this one:
Set-WebConfigurationProperty -Filter '/system.applicationHost/sites/site[@name="$webhost"]/LogFile' -name localTimeRollover -value true

#4) change log file rollover scheduler (just like number 2)
Set-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.period -value "Daily"

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:
WebHost is the site under which you are creating your virtual directory.

For example, we might set $webhost to "Default Web Site", then your new Virtual Directory is created under there. i.e.
$Webhost = "Default Web Site"

Open in new window

sedgwick has already converted the appcmd parts so I'm reluctant to repeat that. Are you able to piece everything together from here? If not, just yell I'm sure we can get this into a happy state.

Cheers,

Chris
0
 
raymoyneurAuthor Commented:
Thanks very much,

I was able to get the code working as expected.

Very appreciated...
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now