2012-08-23 23 views
2

我目前正在使用新的自足PowerShell脚本更新和替换旧的过时夜间计划批处理文件。如何检查已停止的网站并使用PowerShell功能启动这些网站

我遇到麻烦的是网站启动。我能够使用当前的测试代码来获取网站列表并显示它们,但无法找到正确启动它们的方法(如果它们已停止)。

Set-ExecutionPolicy Unrestricted 
start-transcript -path C:\TESTSTART.TXT 
#function to get list of websites that are currently stopped 
function Get-StoppedSites { 
    Import-Module WebAdministration 
    $website = Get-ChildItem IIS:\Sites | where {$_.State -eq 'Stopped'} 
    write-host "$website is stopped" 
    } 
#function to start the stopped website 
function StartSites { 
    param(
    $websitename 
    ) 
    start-website $websitename 
    write-host "$website was started" 
    } 
$Script = Get-StoppedSites 
$Script | ForEach-Object{StartSites -websitename $website} 
stop-transcript 

当我把这种由PowerShell的,它就像它不出差错完成,但该网站没有开始,我已经停了下来。 Windows 2008 R2服务器,PS 2.0。一旦它准备好推出,我将在2003服务器上运行它。

这是我的成绩单:

Transcript started, output file is C:\TESTSTART.TXT 
is stopped 
Start-Website : Cannot validate argument on parameter 'Name'. The argument is null. Supply a non-null argument and try 
the command again. 
At C:\Users\Administrator\Desktop\test.ps1:14 char:18 
+  start-website <<<< $websitename 
    + CategoryInfo   : InvalidData: (:) [Start-Website], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.IIs.PowerShell.Provider.StartWebsiteCommand 

was started 
********************** 
Windows PowerShell Transcript End 
End time: 20120823150248 

你们可以帮我找出哪里/我在做什么错在这里?谢谢!

回答

6

试试这个:

Set-ExecutionPolicy Unrestricted 

Import-Module WebAdministration 

$sites = Get-Website | where {$_.State -eq 'Stopped'} 

$sites | ForEach-Object { 
    $sitename = $_.name 
    IF ($sitename -ne $NULL) 
    { 
     Write-Host "$sitename is stopped. Starting it..." 
     Start-Website $sitename 
    } 
} 

有你的代码一对夫妇的问题。最重要的是,Get-StoppedSites实际上并没有返回任何东西,只是打印出名字。此外,您可以使用现有的命令行程序Get-Website获取网站。

+0

谢谢!这现在对我有效。我有一种感觉,我正在考虑太多。 – bbinnebose

相关问题