无法在window服务中使用powershell创建网站
我想使用 powershell 脚本在 IIS 中创建一个网站。我在窗口服务中编写此代码。
protected override void OnStart(string[] args)
{
string sitename = "rajesh";
//To create a New site
//Powershell script
string script = "cd\\\n" + "import-Module WebAdministration \n" + "IIS:\n" + "New-item iis:\\Sites\\rajesh -PhysicalPath C:\\inetpub\\wwwroot\test -bindings @{Protocol='http';bindingInformation='*:8080:" + sitename + "'}" + "\n add-content C:\\Windows\\System32\\drivers\\etc\\Hosts '127.0.0.1 " + sitename + "'";
string s = RunScript(script);
}
private static string RunScript(string scriptText)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
但是当我尝试在服务列表中启动此窗口服务时...它会引发错误..定位计算机上的服务启动并停止...当我将 powershell 脚本更改为创建文件夹或一些小任务时,则此窗口服务正在工作,但是当我尝试通过此 powershell 创建网站时。它会抛出错误并且窗口服务无法启动。此 powershell 脚本在 powershell 中工作。但在窗口服务中不起作用。
I Want to create a website in IIS using powershell script.I write this code in a window service.
protected override void OnStart(string[] args)
{
string sitename = "rajesh";
//To create a New site
//Powershell script
string script = "cd\\\n" + "import-Module WebAdministration \n" + "IIS:\n" + "New-item iis:\\Sites\\rajesh -PhysicalPath C:\\inetpub\\wwwroot\test -bindings @{Protocol='http';bindingInformation='*:8080:" + sitename + "'}" + "\n add-content C:\\Windows\\System32\\drivers\\etc\\Hosts '127.0.0.1 " + sitename + "'";
string s = RunScript(script);
}
private static string RunScript(string scriptText)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
But when i try to start this window service in services listing...It throws an error.. The services on locat computer start and stopped..... When i change the powershell script to Create a folder or some small task then this window service is working but when i try to create a website through this powershell..it throws an error and window service can't be started.This powershell script is working in powershell.But not in window service..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
这里有几点需要注意,我认为您可能不了解 Windows 服务的某些方面:
OnStart
不是放置任何类型逻辑的地方。该事件存在,因此您可以初始化服务。例如,启动一个后台工作线程来运行服务的主要逻辑并响应外部请求。例如 - 启动 TCP 侦听器或初始化 .NET Remoting 或 WCF。OnStart
仅在服务启动时调用一次。OnStart
必须在 30 秒内返回,尽管您的初始化代码可以请求额外时间。
您正在调用 PowerShell 和 WebAdministration 加载项来创建网站。虽然这是可行的,但它很笨重,也许您应该考虑直接调用 IIS7 托管 API:
下面是使用托管 API 创建站点的示例:
using (ServerManager serverManager = new ServerManager())
{
Site site = serverManager.Sites.Add("My Web Site", "C:\\inetpub\\wwwroot\test", 80);
site.Bindings.Clear();
site.Bindings.Add("*:8080:", "http");
serverManager.CommitChanges();
}
还应该注意的是,在 IIS 上执行的任何配置任务都要求执行此操作的帐户具有提升的权限,即成为管理员或者运行服务在 SYSTEM
帐户下。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
产生的错误会有帮助。
站点名称也不是变量,这是这里还是代码中的错误?
您可以尝试将 \n 替换为 ;同样,powershell 也不使用它作为新行,尽管我猜 C# 是。
The error produced would be helpful.
Sitename isn't a variable either, is that atypo here or in the code?
You could try replacing the \n with ; as well, powershell doesn't use that as new line, though i guess the C# is.