简体   繁体   English

Powershell命令用于设置IIS日志记录设置

[英]Powershell command to set IIS logging settings

I'm creating a powershell script so I can create website hosting with a single command using the IIS Powershell Management Console. 我正在创建一个powershell脚本,因此我可以使用IIS Powershell管理控制台使用单个命令创建网站托管。

I have the commands I need to create the IIS Site and add bindings for the domain names etc... 我有创建IIS站点所需的命令,并为域名添加绑定等...

The one piece of the puzzle I'm missing though is how to change the default Logging directory from %SystemDrive%\\inetpub\\logs\\LogFiles to my own folder that's not on the boot drive of the server. 我缺少的一个难题是如何将默认的日志目录从%SystemDrive%\\ inetpub \\ logs \\ LogFiles更改为我自己的文件夹,该文件夹不在服务器的启动驱动器上。

After extensive searching I expected to find a command along the lines of the following pseudo powershell 经过广泛的搜索后,我希望找到一个沿着以下伪powershell的命令

New-ItemProperty IIS:\Sites\MyNewSite -name logging -value @{format='W3C';directory='d:\sites\site\logs';encoding=UTF-8}

Please could you show me with an example how you change the logging folder in the IIS Powershell Management Console 如果您在IIS Powershell管理控制台中更改日志文件夹,请告诉我一个示例

Thanks in advance 提前致谢

Import-Module WebAdministration
Set-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory -value $logdir

While testing the answer from this thread, toggling options via IIS Manager and PowerShell, I stumbled on something that has been hidden to me. 在测试这个线程的答案,通过IIS管理器和PowerShell切换选项时,我偶然发现了一些隐藏在我身上的东西。 In IIS Manager, choosing Configuration Editor and making a change, allows the IIS Manager to generate and display the script for the change in C#, JavaScript, AppCmd.exe and PowerShell. 在IIS管理器中,选择“配置编辑器”并进行更改,允许IIS管理器生成并显示C#,JavaScript,AppCmd.exe和PowerShell中的更改脚本。 Just click the Generate Script option. 只需单击“生成脚本”选项即可。

[ [ 用于IIS配置更改的自动生成的脚本 ] ]

For changing an individual web site's logFile configuration, the original post was nearly correct. 要更改单个网站的logFile配置,原始帖子几乎是正确的。 Instead of New-ItemProperty, use Set-ItemProperty, like so... 而不是New-ItemProperty,使用Set-ItemProperty,就像这样......

Set-ItemProperty "IIS:\Sites\$SiteName" -name logFile -value @{directory=$LogPath}

For changing the server-wide default settings, see Andy Schneider's answer . 有关更改服务器范围的默认设置,请参阅Andy Schneider的回答

For more information about the options available, see this IIS.net article . 有关可用选项的详细信息,请参阅此IIS.net文章

This works as well, using the WebAdministration Module 这也可以使用WebAdministration模块

Import-Module WebAdministration
$site = gi IIS:\Sites\MyNewSite
$site.Logging.format='W3C'
$site.Logging.directory='d:\sites\site\logs'
$site.Logging.encoding=UTF-8
$site | set-item
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = new-object Microsoft.Web.Administration.ServerManager
$web = $iis.Sites["test"]
#set new logpath, must be existing
$web.LogFile.Directory = "F:\Logfiles\"
$iis.CommitChanges()

If you host multiple sites on a single server and want them to all log to the same log file, the process is quite different. 如果您在单个服务器上托管多个站点并希望它们都记录到同一个日志文件,则该过程完全不同。 It took some digging to find clues here and here , so I thought I would leave a description behind for anyone else with this need. 在这里这里花了一些挖掘才能找到线索,所以我想我会为其他有这种需要的人留下描述。

The following two statements will combine logs for all of your websites into a folder e:\\log\\w3svc. 以下两个语句将所有网站的日志合并到一个文件夹e:\\ log \\ w3svc中。

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/log' -name CentralLogFileMode -Value 'CentralW3C'
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/log' -name centralW3CLogFile.directory -value 'e:\log'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM