2

我目前用新的自给自足的 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

你们能帮我弄清楚我在这里做错了什么吗?谢谢!

4

1 回答 1

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来获取网站。

于 2012-08-23T20:29:50.713 回答